@Override public LinkedBuffer writeStrUTF8FixedDelimited(final CharSequence value, final boolean littleEndian, final WriteSession session, LinkedBuffer lb) throws IOException { return StringSerializer.writeUTF8FixedDelimited(value, littleEndian, session, lb); } },
/** * The length of the utf8 bytes is written first (big endian) before the string - which is fixed 2-bytes. Same * behavior as {@link java.io.DataOutputStream#writeUTF(String)}. */ public static LinkedBuffer writeUTF8FixedDelimited(final CharSequence str, final WriteSession session, LinkedBuffer lb) { return writeUTF8FixedDelimited(str, false, session, lb); }
static void checkFixedDelimited(CharSequence str) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(getShortStringLengthInBytes(str)); OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8"); writer.write(str.toString(), 0, str.length()); writer.close(); LinkedBuffer lb = new LinkedBuffer(512); WriteSession session = new WriteSession(lb); StringSerializer.writeUTF8FixedDelimited(str, session, lb); byte[] b1 = out.toByteArray(); byte[] b2 = session.toByteArray(); assertEquals(b1, b2); }
public void testDataInputStreamDecoding() throws Exception { // Unfortuneatley, DataInputStream uses Modified UTF-8, // which does not support 4-byte characters, as used in the // 'Standard UTF-8'. This is a sacrifice of generating // standard conformant UTF-8 Strings. // // Note: this only happens for surrogate pairs (e.g. emoji's) LinkedBuffer lb = new LinkedBuffer(256); WriteSession session = new WriteSession(lb); StringSerializer.writeUTF8FixedDelimited(surrogatePairs, session, lb); byte[] buffered = session.toByteArray(); ByteArrayInputStream in = new ByteArrayInputStream(buffered); DataInputStream din = new DataInputStream(in); try { String dinResult = din.readUTF(); fail(); } catch (IOException ex) { // Decoding failed of 4-byte format. } }