okio.Buffer.writeShort()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(8.9k)|赞(0)|评价(0)|浏览(100)

本文整理了Java中okio.Buffer.writeShort()方法的一些代码示例,展示了Buffer.writeShort()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Buffer.writeShort()方法的具体详情如下:
包路径:okio.Buffer
类名称:Buffer
方法名:writeShort

Buffer.writeShort介绍

暂无

代码示例

代码示例来源:origin: square/okhttp

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();
}

代码示例来源:origin: square/okhttp

/**
 * 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;
 }
}

代码示例来源:origin: com.squareup.okhttp3/okhttp

/**
 * 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;
 }
}

代码示例来源:origin: square/okio

@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);
}

代码示例来源:origin: square/okhttp

b1 |= PAYLOAD_SHORT;
 sinkBuffer.writeByte(b1);
 sinkBuffer.writeShort((int) byteCount);
} else {
 b1 |= PAYLOAD_LONG;

代码示例来源:origin: square/okio

@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);
}

代码示例来源:origin: square/okio

/**
 * 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());
 }
}

代码示例来源:origin: square/okio

/**
 * 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);
}

代码示例来源:origin: com.squareup.okhttp3/okhttp

b1 |= PAYLOAD_SHORT;
 sinkBuffer.writeByte(b1);
 sinkBuffer.writeShort((int) byteCount);
} else {
 b1 |= PAYLOAD_LONG;

代码示例来源:origin: huxq17/tractor

@Override public BufferedSink writeShort(int s) throws IOException {
 if (closed) throw new IllegalStateException("closed");
 buffer.writeShort(s);
 return emitCompleteSegments();
}

代码示例来源:origin: huxq17/tractor

@Override public Buffer writeShortLe(int s) {
 return writeShort(Util.reverseBytesShort((short) s));
}

代码示例来源:origin: com.squareup.okhttp3/okhttp-dnsoverhttps

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();
}

代码示例来源:origin: com.squareup.okhttp/okhttp-ws

/**
 * 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;
 }
}

代码示例来源:origin: com.squareup.okhttp3/okhttp-ws

/**
 * 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;
 }
}

代码示例来源:origin: apache/servicemix-bundles

/**
 * 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;
 }
}

代码示例来源:origin: com.github.ljun20160606/okhttp

/**
 * 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;
 }
}

代码示例来源:origin: huxq17/tractor

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.
}

代码示例来源:origin: apache/servicemix-bundles

b1 |= PAYLOAD_SHORT;
 sinkBuffer.writeByte(b1);
 sinkBuffer.writeShort((int) byteCount);
} else {
 b1 |= PAYLOAD_LONG;

代码示例来源:origin: io.zipkin.java/zipkin-storage-elasticsearch-http

switch (type) {
 case I16:
  buffer.writeShort(Short.parseShort(number));
  break;
 case I32:

相关文章