public void testLong() throws Exception { for (long i : long_targets) { ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer lb = new LinkedBuffer(BUF_SIZE); WriteSession session = new WriteSession(lb, out); StreamedStringSerializer.writeLong(i, session, lb); LinkedBuffer.writeTo(out, lb); ByteArrayOutputStream out2 = new ByteArrayOutputStream(); LinkedBuffer lb2 = new LinkedBuffer(NUM_BUF_SIZE); WriteSession session2 = new WriteSession(lb2, out2); StreamedStringSerializer.writeLong(i, session2, lb2); LinkedBuffer.writeTo(out2, lb2); byte[] buffered = out.toByteArray(); byte[] buffered_needed_to_flush = out2.toByteArray(); byte[] builtin = STRING.ser(Long.toString(i)); assertEquals(builtin, buffered); assertEquals(builtin, buffered_needed_to_flush); } }
public void testDouble() throws Exception { for (double i : double_targets) { ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer lb = new LinkedBuffer(BUF_SIZE); WriteSession session = new WriteSession(lb, out); StreamedStringSerializer.writeDouble(i, session, lb); LinkedBuffer.writeTo(out, lb); ByteArrayOutputStream out2 = new ByteArrayOutputStream(); LinkedBuffer lb2 = new LinkedBuffer(NUM_BUF_SIZE); WriteSession session2 = new WriteSession(lb2, out2); StreamedStringSerializer.writeDouble(i, session2, lb2); LinkedBuffer.writeTo(out2, lb2); byte[] buffered = out.toByteArray(); byte[] buffered_needed_to_flush = out2.toByteArray(); byte[] builtin = STRING.ser(Double.toString(i)); assertEquals(builtin, buffered); assertEquals(builtin, buffered_needed_to_flush); } }
/** * Write a {@code string} field to the stream. */ public void writeStringNoTag(final CharSequence value) throws IOException { // Unfortunately there does not appear to be any way to tell Java to encode // UTF-8 directly into our buffer, so we have to let it create its own byte // array and then copy. final byte[] bytes = STRING.ser(value.toString()); writeRawVarint32(bytes.length); writeRawBytes(bytes); }
@Test public void testUnknownScalarFields() throws Exception { String[] regularMessages = new String[] { "<?xml version='1.0' encoding='UTF-8'?>" + "<Baz><int>1</int><string>string</string>" + "<double>555.444</double><id>1</id></Baz>", "<?xml version='1.0' encoding='UTF-8'?>" + "<Baz><int>1</int><string>string</string>" + "<id>2</id><double>555.444</double></Baz>", "<?xml version='1.0' encoding='UTF-8'?>" + "<Baz><id>3</id><int>1</int>" + "<string>string</string><double>555.444</double>" + "<bytes><![CDATA[b2]]></bytes></Baz>" }; for (int i = 0; i < regularMessages.length; i++) { Baz b = new Baz(); XmlIOUtil.mergeFrom(STRING.ser(regularMessages[i]), b, b.cachedSchema()); assertTrue(i + 1 == b.getId()); } }
public void testFloat() throws Exception { for (float i : float_targets) { ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer lb = new LinkedBuffer(BUF_SIZE); WriteSession session = new WriteSession(lb, out); StreamedStringSerializer.writeFloat(i, session, lb); LinkedBuffer.writeTo(out, lb); ByteArrayOutputStream out2 = new ByteArrayOutputStream(); LinkedBuffer lb2 = new LinkedBuffer(NUM_BUF_SIZE); WriteSession session2 = new WriteSession(lb2, out2); StreamedStringSerializer.writeFloat(i, session2, lb2); LinkedBuffer.writeTo(out2, lb2); byte[] buffered = out.toByteArray(); byte[] buffered_needed_to_flush = out2.toByteArray(); byte[] builtin = STRING.ser(Float.toString(i)); assertEquals(builtin, buffered); assertEquals(builtin, buffered_needed_to_flush); } }
public void testInt() throws Exception { for (int i : int_targets) { ByteArrayOutputStream out = new ByteArrayOutputStream(); LinkedBuffer lb = new LinkedBuffer(BUF_SIZE); WriteSession session = new WriteSession(lb, out); StreamedStringSerializer.writeInt(i, session, lb); LinkedBuffer.writeTo(out, lb); ByteArrayOutputStream out2 = new ByteArrayOutputStream(); LinkedBuffer lb2 = new LinkedBuffer(NUM_BUF_SIZE); WriteSession session2 = new WriteSession(lb2, out2); StreamedStringSerializer.writeInt(i, session2, lb2); LinkedBuffer.writeTo(out2, lb2); byte[] buffered = out.toByteArray(); byte[] buffered_needed_to_flush = out2.toByteArray(); byte[] builtin = STRING.ser(Integer.toString(i)); assertEquals(builtin, buffered); assertEquals(builtin, buffered_needed_to_flush); } }
public void testDouble() throws Exception { for (double i : double_targets) { LinkedBuffer lb = new LinkedBuffer(256); WriteSession session = new WriteSession(lb); StringSerializer.writeDouble(i, session, lb); LinkedBuffer lb2 = new LinkedBuffer(1); WriteSession session2 = new WriteSession(lb2); StringSerializer.writeDouble(i, session2, lb2); byte[] buffered = session.toByteArray(); byte[] buffered_needed_to_grow = session2.toByteArray(); byte[] builtin = STRING.ser(Double.toString(i)); assertEquals(builtin, buffered); assertEquals(builtin, buffered_needed_to_grow); } }
public void testFloat() throws Exception { for (float i : float_targets) { LinkedBuffer lb = new LinkedBuffer(256); WriteSession session = new WriteSession(lb); StringSerializer.writeFloat(i, session, lb); LinkedBuffer lb2 = new LinkedBuffer(1); WriteSession session2 = new WriteSession(lb2); StringSerializer.writeFloat(i, session2, lb2); byte[] buffered = session.toByteArray(); byte[] buffered_needed_to_grow = session2.toByteArray(); byte[] builtin = STRING.ser(Float.toString(i)); assertEquals(builtin, buffered); assertEquals(builtin, buffered_needed_to_grow); } }
public void testInt() throws Exception { for (int i : int_targets) { LinkedBuffer lb = new LinkedBuffer(256); WriteSession session = new WriteSession(lb); StringSerializer.writeInt(i, session, lb); LinkedBuffer lb2 = new LinkedBuffer(1); WriteSession session2 = new WriteSession(lb2); StringSerializer.writeInt(i, session2, lb2); byte[] buffered = session.toByteArray(); byte[] buffered_needed_to_grow = session2.toByteArray(); byte[] builtin = STRING.ser(Integer.toString(i)); assertEquals(builtin, buffered); assertEquals(builtin, buffered_needed_to_grow); } }
public void testLong() throws Exception { for (long i : long_targets) { LinkedBuffer lb = new LinkedBuffer(256); WriteSession session = new WriteSession(lb); StringSerializer.writeLong(i, session, lb); LinkedBuffer lb2 = new LinkedBuffer(1); WriteSession session2 = new WriteSession(lb2); StringSerializer.writeLong(i, session2, lb2); byte[] buffered = session.toByteArray(); byte[] buffered_needed_to_grow = session2.toByteArray(); byte[] builtin = STRING.ser(Long.toString(i)); assertEquals(builtin, buffered); assertEquals(builtin, buffered_needed_to_grow); } }
static byte[] ba(String text) { return STRING.ser(text); }
/** * Encodes {@code text} into a sequence of UTF-8 bytes and returns the result as a {@code ByteString}. */ public static ByteString copyFromUtf8(final String text) { return new ByteString(STRING.ser(text)); /* * @try { return new ByteString(text.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { throw new * RuntimeException("UTF-8 not supported?", e); } */ }
@Override public void writeString(int fieldNumber, CharSequence value, boolean repeated) throws IOException { byte[] bytes = STRING.ser(value.toString()); int tag = WireFormat.makeTag(fieldNumber, WireFormat.WIRETYPE_LENGTH_DELIMITED); byte[] delimited = CodedOutput.getTagAndRawVarInt32Bytes(tag, bytes.length); size += delimited.length + bytes.length; current = new ByteArrayNode(bytes, new ByteArrayNode(delimited, current)); }