@Test public void testEmptyBinaryNonEmptyArrayWithOffset() { byte[] bytes = new byte[] {(byte) 0, (byte) 0, (byte) 0}; Binary bin = new Binary(bytes, 1, 0); assertEquals("unexpected result", "\"\"", StringUtils.toQuotedString(bin, 10, true)); }
public static String toQuotedString(final Binary bin,final int stringLength,final boolean appendIfTruncated) { if(bin == null) { return "\"\""; } final byte[] binData = bin.getArray(); final int binLength = bin.getLength(); final int offset = bin.getArrayOffset(); StringBuilder str = new StringBuilder(); str.append("\""); int size = 0; boolean truncated = false; for (int i = 0; i < binLength; i++) { byte c = binData[offset + i]; if (c > 31 && c < 127 && c != '\\') { if (size + 1 <= stringLength) { size += 1; str.append((char) c); } else { truncated = true; break; } } else { if (size + 4 <= stringLength) { size += 4; str.append(String.format("\\x%02x", c)); } else { truncated = true; break; } } } str.append("\""); if (truncated && appendIfTruncated) { str.append("...(truncated)"); } return str.toString(); }
StringUtils { public static String toQuotedString(final Binary bin,final int stringLength,final boolean appendIfTruncated) { if(bin == null) { return "\"\""; } final byte[] binData = bin.getArray(); final int binLength = bin.getLength(); final int offset = bin.getArrayOffset(); StringBuilder str = new StringBuilder(); str.append("\""); int size = 0; boolean truncated = false; for (int i = 0; i < binLength; i++) { byte c = binData[offset + i]; if (c > 31 && c < 127 && c != '\\') { if (size + 1 <= stringLength) { size += 1; str.append((char) c); } else { truncated = true; break; } } else { if (size + 4 <= stringLength) { size += 4; str.append(String.format("\\x%02x", c)); } else { truncated = true; break; } } } str.append("\""); if (truncated && appendIfTruncated) { str.append("...(truncated)"); } return str.toString(); } }
StringUtils { public static String toQuotedString(final Binary bin,final int stringLength,final boolean appendIfTruncated) { if(bin == null) { return "\"\""; } final byte[] binData = bin.getArray(); final int binLength = bin.getLength(); final int offset = bin.getArrayOffset(); StringBuilder str = new StringBuilder(); str.append("\""); int size = 0; boolean truncated = false; for (int i = 0; i < binLength; i++) { byte c = binData[offset + i]; if (c > 31 && c < 127 && c != '\\') { if (size + 1 <= stringLength) { size += 1; str.append((char) c); } else { truncated = true; break; } } else { if (size + 4 <= stringLength) { size += 4; str.append(String.format("\\x%02x", c)); } else { truncated = true; break; } } } str.append("\""); if (truncated && appendIfTruncated) { str.append("...(truncated)"); } return str.toString(); }  }
StringUtils { public static String toQuotedString(final Binary bin,final int stringLength,final boolean appendIfTruncated) { if(bin == null) { return "\"\""; } final byte[] binData = bin.getArray(); final int binLength = bin.getLength(); final int offset = bin.getArrayOffset(); StringBuilder str = new StringBuilder(); str.append("\""); int size = 0; boolean truncated = false; for (int i = 0; i < binLength; i++) { byte c = binData[offset + i]; if (c > 31 && c < 127 && c != '\\') { if (size + 1 <= stringLength) { size += 1; str.append((char) c); } else { truncated = true; break; } } else { if (size + 4 <= stringLength) { size += 4; str.append(String.format("\\x%02x", c)); } else { truncated = true; break; } } } str.append("\""); if (truncated && appendIfTruncated) { str.append("...(truncated)"); } return str.toString(); }  static String toQuotedString(final Binary bin,final int stringLength,final boolean appendIfTruncated); }
StringUtils { public static String toQuotedString(final Binary bin,final int stringLength,final boolean appendIfTruncated) { if(bin == null) { return "\"\""; } final byte[] binData = bin.getArray(); final int binLength = bin.getLength(); final int offset = bin.getArrayOffset(); StringBuilder str = new StringBuilder(); str.append("\""); int size = 0; boolean truncated = false; for (int i = 0; i < binLength; i++) { byte c = binData[offset + i]; if (c > 31 && c < 127 && c != '\\') { if (size + 1 <= stringLength) { size += 1; str.append((char) c); } else { truncated = true; break; } } else { if (size + 4 <= stringLength) { size += 4; str.append(String.format("\\x%02x", c)); } else { truncated = true; break; } } } str.append("\""); if (truncated && appendIfTruncated) { str.append("...(truncated)"); } return str.toString(); }  static String toQuotedString(final Binary bin,final int stringLength,final boolean appendIfTruncated);  }