@Test public void testWriteString16() throws IOException { String shortest = StringUtils.getRandomString(Byte.MAX_VALUE + 1); String shortHex = StringUtils.toHexString(shortest.getBytes("UTF-8")); String longest = StringUtils.getRandomString(Short.MAX_VALUE); String longHex = StringUtils.toHexString(longest.getBytes("UTF-8")); w.writeString(shortest); w.writeString(longest); checkContents("42" + "0080" + shortHex + "42" + "7FFF" + longHex); }
@Override public void writeString(String s) throws IOException { byte[] b = s.getBytes("UTF-8"); if (b.length <= Byte.MAX_VALUE) { out.write(STRING_8); out.write((byte) b.length); } else if (b.length <= Short.MAX_VALUE) { out.write(STRING_16); writeInt16((short) b.length); } else { out.write(STRING_32); writeInt32(b.length); } out.write(b); }
BdfWriterImpl implements BdfWriter { @Override public void writeString(String s) throws IOException { byte[] b = s.getBytes("UTF-8"); if (b.length <= Byte.MAX_VALUE) { out.write(STRING_8); out.write((byte) b.length); } else if (b.length <= Short.MAX_VALUE) { out.write(STRING_16); writeInt16((short) b.length); } else { out.write(STRING_32); writeInt32(b.length); } out.write(b); } }
BdfWriterImpl implements BdfWriter { @Override public void writeString(String s) throws IOException { byte[] b = s.getBytes("UTF-8"); if (b.length <= Byte.MAX_VALUE) { out.write(STRING_8); out.write((byte) b.length); } else if (b.length <= Short.MAX_VALUE) { out.write(STRING_16); writeInt16((short) b.length); } else { out.write(STRING_32); writeInt32(b.length); } out.write(b); } BdfWriterImpl(OutputStream out); }
BdfWriterImpl implements BdfWriter { @Override public void writeString(String s) throws IOException { byte[] b = s.getBytes("UTF-8"); if (b.length <= Byte.MAX_VALUE) { out.write(STRING_8); out.write((byte) b.length); } else if (b.length <= Short.MAX_VALUE) { out.write(STRING_16); writeInt16((short) b.length); } else { out.write(STRING_32); writeInt32(b.length); } out.write(b); } BdfWriterImpl(OutputStream out); @Override void flush(); @Override void close(); @Override void writeNull(); @Override void writeBoolean(boolean b); @Override void writeLong(long i); @Override void writeDouble(double d); @Override void writeString(String s); @Override void writeRaw(byte[] b); @Override void writeList(Collection<?> c); @Override void writeListStart(); @Override void writeListEnd(); @Override void writeDictionary(Map<?, ?> m); @Override void writeDictionaryStart(); @Override void writeDictionaryEnd(); }
BdfWriterImpl implements BdfWriter { @Override public void writeString(String s) throws IOException { byte[] b = s.getBytes("UTF-8"); if (b.length <= Byte.MAX_VALUE) { out.write(STRING_8); out.write((byte) b.length); } else if (b.length <= Short.MAX_VALUE) { out.write(STRING_16); writeInt16((short) b.length); } else { out.write(STRING_32); writeInt32(b.length); } out.write(b); } BdfWriterImpl(OutputStream out); @Override void flush(); @Override void close(); @Override void writeNull(); @Override void writeBoolean(boolean b); @Override void writeLong(long i); @Override void writeDouble(double d); @Override void writeString(String s); @Override void writeRaw(byte[] b); @Override void writeList(Collection<?> c); @Override void writeListStart(); @Override void writeListEnd(); @Override void writeDictionary(Map<?, ?> m); @Override void writeDictionaryStart(); @Override void writeDictionaryEnd();  }