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

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

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

Buffer.readAndWriteUnsafe介绍

暂无

代码示例

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

@Override public long read(Buffer sink, long byteCount) throws IOException {
 if (!channel.isOpen()) throw new IllegalStateException("closed");
 try (Buffer.UnsafeCursor ignored = sink.readAndWriteUnsafe(cursor)) {
  timeout.throwIfReached();
  long oldSize = sink.size();
  int length = (int) Math.min(8192, byteCount);
  cursor.expandBuffer(length);
  int read = channel.read(ByteBuffer.wrap(cursor.data, cursor.start, length));
  if (read == -1) {
   cursor.resizeBuffer(oldSize);
   return -1;
  } else {
   cursor.resizeBuffer(oldSize + read);
   return read;
  }
 }
}

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

/**
  * Reads a message body into across one or more frames. Control frames that occur between
  * fragments will be processed. If the message payload is masked this will unmask as it's being
  * processed.
  */
 private void readMessage() throws IOException {
  while (true) {
   if (closed) throw new IOException("closed");

   if (frameLength > 0) {
    source.readFully(messageFrameBuffer, frameLength);

    if (!isClient) {
     messageFrameBuffer.readAndWriteUnsafe(maskCursor);
     maskCursor.seek(messageFrameBuffer.size() - frameLength);
     toggleMask(maskCursor, maskKey);
     maskCursor.close();
    }
   }

   if (isFinalFrame) break; // We are exhausted and have no continuations.

   readUntilNonControlFrame();
   if (opcode != OPCODE_CONTINUATION) {
    throw new ProtocolException("Expected continuation opcode. Got: " + toHexString(opcode));
   }
  }
 }
}

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

private void writeControlFrame(int opcode, ByteString payload) throws IOException {
 if (writerClosed) throw new IOException("closed");
 int length = payload.size();
 if (length > PAYLOAD_BYTE_MAX) {
  throw new IllegalArgumentException(
    "Payload size must be less than or equal to " + PAYLOAD_BYTE_MAX);
 }
 int b0 = B0_FLAG_FIN | opcode;
 sinkBuffer.writeByte(b0);
 int b1 = length;
 if (isClient) {
  b1 |= B1_FLAG_MASK;
  sinkBuffer.writeByte(b1);
  random.nextBytes(maskKey);
  sinkBuffer.write(maskKey);
  if (length > 0) {
   long payloadStart = sinkBuffer.size();
   sinkBuffer.write(payload);
   sinkBuffer.readAndWriteUnsafe(maskCursor);
   maskCursor.seek(payloadStart);
   toggleMask(maskCursor, maskKey);
   maskCursor.close();
  }
 } else {
  sinkBuffer.writeByte(b1);
  sinkBuffer.write(payload);
 }
 sink.flush();
}

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

/**
  * Reads a message body into across one or more frames. Control frames that occur between
  * fragments will be processed. If the message payload is masked this will unmask as it's being
  * processed.
  */
 private void readMessage() throws IOException {
  while (true) {
   if (closed) throw new IOException("closed");

   if (frameLength > 0) {
    source.readFully(messageFrameBuffer, frameLength);

    if (!isClient) {
     messageFrameBuffer.readAndWriteUnsafe(maskCursor);
     maskCursor.seek(messageFrameBuffer.size() - frameLength);
     toggleMask(maskCursor, maskKey);
     maskCursor.close();
    }
   }

   if (isFinalFrame) break; // We are exhausted and have no continuations.

   readUntilNonControlFrame();
   if (opcode != OPCODE_CONTINUATION) {
    throw new ProtocolException("Expected continuation opcode. Got: " + toHexString(opcode));
   }
  }
 }
}

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

sinkBuffer.write(buffer, byteCount);
sinkBuffer.readAndWriteUnsafe(maskCursor);
maskCursor.seek(bufferStart);
toggleMask(maskCursor, maskKey);

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

private void writeControlFrame(int opcode, ByteString payload) throws IOException {
 if (writerClosed) throw new IOException("closed");
 int length = payload.size();
 if (length > PAYLOAD_BYTE_MAX) {
  throw new IllegalArgumentException(
    "Payload size must be less than or equal to " + PAYLOAD_BYTE_MAX);
 }
 int b0 = B0_FLAG_FIN | opcode;
 sinkBuffer.writeByte(b0);
 int b1 = length;
 if (isClient) {
  b1 |= B1_FLAG_MASK;
  sinkBuffer.writeByte(b1);
  random.nextBytes(maskKey);
  sinkBuffer.write(maskKey);
  if (length > 0) {
   long payloadStart = sinkBuffer.size();
   sinkBuffer.write(payload);
   sinkBuffer.readAndWriteUnsafe(maskCursor);
   maskCursor.seek(payloadStart);
   toggleMask(maskCursor, maskKey);
   maskCursor.close();
  }
 } else {
  sinkBuffer.writeByte(b1);
  sinkBuffer.write(payload);
 }
 sink.flush();
}

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

controlFrameBuffer.readAndWriteUnsafe(maskCursor);
maskCursor.seek(0);
toggleMask(maskCursor, maskKey);

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

@Test public void shrink() throws Exception {
 Buffer buffer = bufferFactory.newBuffer();
 assumeTrue(buffer.size() > 3);
 long originalSize = buffer.size();
 Buffer expected = new Buffer();
 deepCopy(buffer).copyTo(expected, 0, originalSize - 3);
 try (UnsafeCursor cursor = buffer.readAndWriteUnsafe()) {
  assertEquals(originalSize, cursor.resizeBuffer(originalSize - 3));
 }
 assertEquals(expected, buffer);
}

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

sinkBuffer.write(buffer, byteCount);
sinkBuffer.readAndWriteUnsafe(maskCursor);
maskCursor.seek(bufferStart);
toggleMask(maskCursor, maskKey);

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

@Test public void expandNewSegment() throws Exception {
 Buffer buffer = bufferFactory.newBuffer();
 long originalSize = buffer.size();
 try (UnsafeCursor cursor = buffer.readAndWriteUnsafe()) {
  long addedByteCount = cursor.expandBuffer(SEGMENT_SIZE);
  assertEquals(SEGMENT_SIZE, addedByteCount);
  assertEquals(originalSize, cursor.offset);
  assertEquals(0, cursor.start);
  assertEquals(SEGMENT_SIZE, cursor.end);
 }
}

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

@Test public void expand() throws Exception {
 Buffer buffer = bufferFactory.newBuffer();
 long originalSize = buffer.size();
 Buffer expected = deepCopy(buffer);
 expected.writeUtf8("abcde");
 try (UnsafeCursor cursor = buffer.readAndWriteUnsafe()) {
  cursor.expandBuffer(5);
  for (int i = 0; i < 5; i++) {
   cursor.data[cursor.start + i] = (byte) ('a' + i);
  }
  cursor.resizeBuffer(originalSize + 5);
 }
 assertEquals(expected, buffer);
}

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

controlFrameBuffer.readAndWriteUnsafe(maskCursor);
maskCursor.seek(0);
toggleMask(maskCursor, maskKey);

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

@Test public void expandMovesOffsetToOldSize() throws Exception {
  Buffer buffer = bufferFactory.newBuffer();
  long originalSize = buffer.size();

  try (UnsafeCursor cursor = buffer.readAndWriteUnsafe()) {
   cursor.seek(buffer.size() / 2);
   assertEquals(originalSize, buffer.size());
   long addedByteCount = cursor.expandBuffer(5);
   assertEquals(originalSize + addedByteCount, buffer.size());
   assertEquals(originalSize, cursor.offset);
  }
 }
}

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

@Test public void shrinkAdjustOffset() throws Exception {
 Buffer buffer = bufferFactory.newBuffer();
 assumeTrue(buffer.size() > 4);
 try (UnsafeCursor cursor = buffer.readAndWriteUnsafe()) {
  cursor.seek(buffer.size() - 1);
  cursor.resizeBuffer(3);
  assertEquals(3, cursor.offset);
  assertEquals(null, cursor.data);
  assertEquals(-1, cursor.start);
  assertEquals(-1, cursor.end);
 }
}

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

@Test public void enlarge() throws Exception {
 Buffer buffer = bufferFactory.newBuffer();
 long originalSize = buffer.size();
 Buffer expected = deepCopy(buffer);
 expected.writeUtf8("abc");
 try (UnsafeCursor cursor = buffer.readAndWriteUnsafe()) {
  assertEquals(originalSize, cursor.resizeBuffer(originalSize + 3));
  cursor.seek(originalSize);
  cursor.data[cursor.start] = 'a';
  cursor.seek(originalSize + 1);
  cursor.data[cursor.start] = 'b';
  cursor.seek(originalSize + 2);
  cursor.data[cursor.start] = 'c';
 }
 assertEquals(expected, buffer);
}

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

@Test public void resizeToSameSizeSeeksToEnd() throws Exception {
 Buffer buffer = bufferFactory.newBuffer();
 long originalSize = buffer.size();
 try (UnsafeCursor cursor = buffer.readAndWriteUnsafe()) {
  cursor.seek(buffer.size() / 2);
  assertEquals(originalSize, buffer.size());
  cursor.resizeBuffer(originalSize);
  assertEquals(originalSize, buffer.size());
  assertEquals(originalSize, cursor.offset);
  assertNull(cursor.data);
  assertEquals(-1, cursor.start);
  assertEquals(-1, cursor.end);
 }
}

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

@Test public void resizeShrinkMovesCursorToEnd() throws Exception {
 Buffer buffer = bufferFactory.newBuffer();
 assumeTrue(buffer.size() > 0);
 long originalSize = buffer.size();
 try (UnsafeCursor cursor = buffer.readAndWriteUnsafe()) {
  cursor.seek(buffer.size() / 2);
  assertEquals(originalSize, buffer.size());
  cursor.resizeBuffer(originalSize - 1);
  assertEquals(originalSize - 1, cursor.offset);
  assertNull(cursor.data);
  assertEquals(-1, cursor.start);
  assertEquals(-1, cursor.end);
 }
}

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

@Test public void expandSameSegment() throws Exception {
 Buffer buffer = bufferFactory.newBuffer();
 long originalSize = buffer.size();
 assumeTrue(originalSize > 0);
 try (UnsafeCursor cursor = buffer.readAndWriteUnsafe()) {
  cursor.seek(originalSize - 1);
  int originalEnd = cursor.end;
  assumeTrue(originalEnd < SEGMENT_SIZE);
  long addedByteCount = cursor.expandBuffer(1);
  assertEquals(SEGMENT_SIZE - originalEnd, addedByteCount);
  assertEquals(originalSize + addedByteCount, buffer.size());
  assertEquals(originalSize, cursor.offset);
  assertEquals(originalEnd, cursor.start);
  assertEquals(SEGMENT_SIZE, cursor.end);
 }
}

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

@Test public void resizeEnlargeMovesCursorToOldSize() throws Exception {
 Buffer buffer = bufferFactory.newBuffer();
 long originalSize = buffer.size();
 Buffer expected = deepCopy(buffer);
 expected.writeUtf8("a");
 try (UnsafeCursor cursor = buffer.readAndWriteUnsafe()) {
  cursor.seek(buffer.size() / 2);
  assertEquals(originalSize, buffer.size());
  cursor.resizeBuffer(originalSize + 1);
  assertEquals(originalSize, cursor.offset);
  assertNotNull(cursor.data);
  assertNotEquals(-1, cursor.start);
  assertEquals(cursor.start + 1, cursor.end);
  cursor.data[cursor.start] = 'a';
 }
 assertEquals(expected, buffer);
}

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

/**
  * Reads a message body into across one or more frames. Control frames that occur between
  * fragments will be processed. If the message payload is masked this will unmask as it's being
  * processed.
  */
 private void readMessage() throws IOException {
  while (true) {
   if (closed) throw new IOException("closed");

   if (frameLength > 0) {
    source.readFully(messageFrameBuffer, frameLength);

    if (!isClient) {
     messageFrameBuffer.readAndWriteUnsafe(maskCursor);
     maskCursor.seek(messageFrameBuffer.size() - frameLength);
     toggleMask(maskCursor, maskKey);
     maskCursor.close();
    }
   }

   if (isFinalFrame) break; // We are exhausted and have no continuations.

   readUntilNonControlFrame();
   if (opcode != OPCODE_CONTINUATION) {
    throw new ProtocolException("Expected continuation opcode. Got: " + toHexString(opcode));
   }
  }
 }
}

相关文章