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(); }
/** * 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; } }
/** * 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; } }
@Test public void gunzip_withHCRC() throws Exception { CRC32 hcrc = new CRC32(); ByteString gzipHeader = gzipHeaderWithFlags((byte) 0x02); hcrc.update(gzipHeader.toByteArray()); Buffer gzipped = new Buffer(); gzipped.write(gzipHeader); gzipped.writeShort(TestUtil.reverseBytes((short) hcrc.getValue())); // little endian gzipped.write(deflated); gzipped.write(gzipTrailer); assertGzipped(gzipped); }
b1 |= PAYLOAD_SHORT; sinkBuffer.writeByte(b1); sinkBuffer.writeShort((int) byteCount); } else { b1 |= PAYLOAD_LONG;
@Test public void gunzip_withExtra() throws Exception { Buffer gzipped = new Buffer(); gzipped.write(gzipHeaderWithFlags((byte) 0x04)); gzipped.writeShort(TestUtil.reverseBytes((short) 7)); // little endian extra length gzipped.write("blubber".getBytes(UTF_8), 0, 7); gzipped.write(deflated); gzipped.write(gzipTrailer); assertGzipped(gzipped); }
/** * Note that you cannot test this with old versions of gzip, as they interpret flag bit 1 as * CONTINUATION, not HCRC. For example, this is the case with the default gzip on osx. */ @Test public void gunzipWhenHeaderCRCIncorrect() { Buffer gzipped = new Buffer(); gzipped.write(gzipHeaderWithFlags((byte) 0x02)); gzipped.writeShort((short) 0); // wrong HCRC! gzipped.write(deflated); gzipped.write(gzipTrailer); try { gunzip(gzipped); fail(); } catch (IOException e) { assertEquals("FHCRC: actual 0x0000261d != expected 0x00000000", e.getMessage()); } }
/** * For portability, it is a good idea to export the gzipped bytes and try running gzip. Ex. * {@code echo gzipped | base64 --decode | gzip -l -v} */ @Test public void gunzip_withAll() throws Exception { Buffer gzipped = new Buffer(); gzipped.write(gzipHeaderWithFlags((byte) 0x1c)); gzipped.writeShort(TestUtil.reverseBytes((short) 7)); // little endian extra length gzipped.write("blubber".getBytes(UTF_8), 0, 7); gzipped.write("foo.txt".getBytes(UTF_8), 0, 7); gzipped.writeByte(0); // zero-terminated gzipped.write("rubbish".getBytes(UTF_8), 0, 7); gzipped.writeByte(0); // zero-terminated gzipped.write(deflated); gzipped.write(gzipTrailer); assertGzipped(gzipped); }
b1 |= PAYLOAD_SHORT; sinkBuffer.writeByte(b1); sinkBuffer.writeShort((int) byteCount); } else { b1 |= PAYLOAD_LONG;
@Override public BufferedSink writeShort(int s) throws IOException { if (closed) throw new IllegalStateException("closed"); buffer.writeShort(s); return emitCompleteSegments(); }
@Override public Buffer writeShortLe(int s) { return writeShort(Util.reverseBytesShort((short) s)); }
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(); }
/** * 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}. */ public void writeClose(int code, String reason) throws IOException { Buffer payload = null; if (code != 0 || reason != null) { if (code != 0 && (code < 1000 || code >= 5000)) { throw new IllegalArgumentException("Code must be in range [1000,5000)."); } payload = new Buffer(); payload.writeShort(code); if (reason != null) { payload.writeUtf8(reason); } } synchronized (this) { writeControlFrameSynchronized(OPCODE_CONTROL_CLOSE, payload); writerClosed = true; } }
/** * 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}. */ public void writeClose(int code, String reason) throws IOException { Buffer payload = null; if (code != 0 || reason != null) { if (code != 0) { validateCloseCode(code, true); } payload = new Buffer(); payload.writeShort(code); if (reason != null) { payload.writeUtf8(reason); } } synchronized (this) { writeControlFrameSynchronized(OPCODE_CONTROL_CLOSE, payload); writerClosed = true; } }
/** * 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; } }
/** * 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; } }
private void writeHeader() { // Write the Gzip header directly into the buffer for the sink to avoid handling IOException. Buffer buffer = this.sink.buffer(); buffer.writeShort(0x1f8b); // Two-byte Gzip ID. buffer.writeByte(0x08); // 8 == Deflate compression method. buffer.writeByte(0x00); // No flags. buffer.writeInt(0x00); // No modification time. buffer.writeByte(0x00); // No extra flags. buffer.writeByte(0x00); // No OS. }
b1 |= PAYLOAD_SHORT; sinkBuffer.writeByte(b1); sinkBuffer.writeShort((int) byteCount); } else { b1 |= PAYLOAD_LONG;
switch (type) { case I16: buffer.writeShort(Short.parseShort(number)); break; case I32: