@Override public RequestBody convert(final T value) throws IOException { Buffer buffer = new Buffer(); try { Marshaller marshaller = context.createMarshaller(); XMLStreamWriter xmlWriter = xmlOutputFactory.createXMLStreamWriter( buffer.outputStream(), JaxbConverterFactory.XML.charset().name()); marshaller.marshal(value, xmlWriter); } catch (JAXBException | XMLStreamException e) { throw new RuntimeException(e); } return RequestBody.create(JaxbConverterFactory.XML, buffer.readByteString()); } }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); JsonWriter writer = JsonWriter.of(buffer); adapter.toJson(writer, value); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); try { OutputStreamWriter osw = new OutputStreamWriter(buffer.outputStream(), CHARSET); serializer.write(value, osw); osw.flush(); } catch (RuntimeException | IOException e) { throw e; } catch (Exception e) { throw new RuntimeException(e); } return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
private ByteString serialize(Object o) throws IOException { Buffer buffer = new Buffer(); try (ObjectOutputStream objectOut = new ObjectOutputStream(buffer.outputStream())) { objectOut.writeObject(o); } return buffer.readByteString(); }
@Override public RequestBody convert(T value) throws IOException { Buffer buffer = new Buffer(); Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8); JsonWriter jsonWriter = gson.newJsonWriter(writer); adapter.write(jsonWriter, value); jsonWriter.close(); return RequestBody.create(MEDIA_TYPE, buffer.readByteString()); } }
/** * Creates a relay that reads a recorded stream from {@code file}. * * <p><strong>Warning:</strong> callers to this method must immediately call {@link #newSource} to * create a source and close that when they're done. Otherwise a handle to {@code file} will be * leaked. */ public static Relay read(File file) throws IOException { RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); FileOperator fileOperator = new FileOperator(randomAccessFile.getChannel()); // Read the header. Buffer header = new Buffer(); fileOperator.read(0, header, FILE_HEADER_SIZE); ByteString prefix = header.readByteString(PREFIX_CLEAN.size()); if (!prefix.equals(PREFIX_CLEAN)) throw new IOException("unreadable cache file"); long upstreamSize = header.readLong(); long metadataSize = header.readLong(); // Read the metadata. Buffer metadataBuffer = new Buffer(); fileOperator.read(FILE_HEADER_SIZE + upstreamSize, metadataBuffer, metadataSize); ByteString metadata = metadataBuffer.readByteString(); // Return the result. return new Relay(randomAccessFile, null, upstreamSize, metadata, 0L); }
private void readMessageFrame() throws IOException { int opcode = this.opcode; if (opcode != OPCODE_TEXT && opcode != OPCODE_BINARY) { throw new ProtocolException("Unknown opcode: " + toHexString(opcode)); } readMessage(); if (opcode == OPCODE_TEXT) { frameCallback.onReadMessage(messageFrameBuffer.readUtf8()); } else { frameCallback.onReadMessage(messageFrameBuffer.readByteString()); } }
/** * Send a close frame with optional code and reason. * * @param code Status code as defined by <a * href="http://tools.ietf.org/html/rfc6455#section-7.4">Section 7.4 of RFC 6455</a> or {@code 0}. * @param reason Reason for shutting down or {@code null}. */ void writeClose(int code, ByteString reason) throws IOException { ByteString payload = ByteString.EMPTY; if (code != 0 || reason != null) { if (code != 0) { validateCloseCode(code); } Buffer buffer = new Buffer(); buffer.writeShort(code); if (reason != null) { buffer.write(reason); } payload = buffer.readByteString(); } try { writeControlFrame(OPCODE_CONTROL_CLOSE, payload); } finally { writerClosed = true; } }
public static ByteString encodeQuery(String host, int type) { Buffer buf = new Buffer(); buf.writeShort(0); // query id buf.writeShort(256); // flags with recursion buf.writeShort(1); // question count buf.writeShort(0); // answerCount buf.writeShort(0); // authorityResourceCount buf.writeShort(0); // additional Buffer nameBuf = new Buffer(); final String[] labels = host.split("\\."); for (String label : labels) { long utf8ByteCount = Utf8.size(label); if (utf8ByteCount != label.length()) { throw new IllegalArgumentException("non-ascii hostname: " + host); } nameBuf.writeByte((byte) utf8ByteCount); nameBuf.writeUtf8(label); } nameBuf.writeByte(0); // end nameBuf.copyTo(buf, 0, nameBuf.size()); buf.writeShort(type); buf.writeShort(1); // CLASS_IN return buf.readByteString(); }
void writeByteString(ByteString data) throws IOException { if (useCompression && Huffman.get().encodedLength(data) < data.size()) { Buffer huffmanBuffer = new Buffer(); Huffman.get().encode(data, huffmanBuffer); ByteString huffmanBytes = huffmanBuffer.readByteString(); writeInt(huffmanBytes.size(), PREFIX_7_BITS, 0x80); out.write(huffmanBytes); } else { writeInt(data.size(), PREFIX_7_BITS, 0); out.write(data); } }
private void assertCodePointEncoded(String hex, int... codePoints) throws Exception { Buffer buffer = new Buffer(); for (int codePoint : codePoints) { buffer.writeUtf8CodePoint(codePoint); } assertEquals(buffer.readByteString(), ByteString.decodeHex(hex)); }
frameCallback.onReadPing(controlFrameBuffer.readByteString()); break; case OPCODE_CONTROL_PONG: frameCallback.onReadPong(controlFrameBuffer.readByteString()); break; case OPCODE_CONTROL_CLOSE:
@Test public void writeSubstringWithCharset() throws IOException { sink.writeString("təˈranəˌsôr", 3, 7, Charset.forName("utf-32be")); sink.flush(); assertEquals(ByteString.decodeHex("00000072000000610000006e00000259"), data.readByteString()); }
@Test public void deflatePoorlyCompressed() throws IOException { ByteString original = randomBytes(1024 * 1024); Buffer data = new Buffer(); data.write(original); Buffer sink = new Buffer(); DeflaterSink deflaterSink = new DeflaterSink(sink, new Deflater()); deflaterSink.write(data, data.size()); deflaterSink.close(); Buffer inflated = inflate(sink); assertEquals(original, inflated.readByteString()); }
@Test public void writeStringWithCharset() throws IOException { sink.writeString("təˈranəˌsôr", Charset.forName("utf-32be")); sink.flush(); assertEquals(ByteString.decodeHex("0000007400000259000002c800000072000000610000006e00000259" + "000002cc00000073000000f400000072"), data.readByteString()); }
@Test public void writeUtf8SubstringWithCharset() throws IOException { sink.writeString("təˈranəˌsôr", 3, 7, Charset.forName("utf-8")); sink.flush(); assertEquals(ByteString.encodeUtf8("ranə"), data.readByteString()); }
@Test public void writeStringUtf8() throws IOException { sink.writeUtf8("təˈranəˌsôr"); sink.flush(); assertEquals(ByteString.decodeHex("74c999cb8872616ec999cb8c73c3b472"), data.readByteString()); }
@Test public void writeSubstringUtf8() throws IOException { sink.writeUtf8("təˈranəˌsôr", 3, 7); sink.flush(); assertEquals(ByteString.decodeHex("72616ec999"), data.readByteString()); }
@Test public void inflatePoorlyCompressed() throws Exception { ByteString original = randomBytes(1024 * 1024); Buffer deflated = deflate(original); Buffer inflated = inflate(deflated); assertEquals(original, inflated.readByteString()); }
@Test public void bufferHashIsNotDestructive() { Buffer buffer = new Buffer(); buffer.writeUtf8("abc"); assertEquals(SHA256_abc, buffer.sha256()); assertEquals("abc", buffer.readUtf8()); buffer.writeUtf8("def"); assertEquals(SHA256_def, buffer.sha256()); assertEquals("def", buffer.readUtf8()); buffer.write(r32k); assertEquals(SHA256_r32k, buffer.sha256()); assertEquals(r32k, buffer.readByteString()); } }