Java Code Examples for io.netty.buffer.ByteBuf#setByte()
The following examples show how to use
io.netty.buffer.ByteBuf#setByte() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DremioStringUtils.java From dremio-oss with Apache License 2.0 | 6 votes |
/** * Parses a hex encoded binary string and write to an output buffer. * * This function does not modify the {@code readerIndex} and {@code writerIndex} * of the byte buffer. * * @return Index in the byte buffer just after the last written byte. */ public static int parseBinaryString(ByteBuf str, int strStart, int strEnd, ByteBuf out) { int dstEnd = 0; for (int i = strStart; i < strEnd; i++) { byte b = str.getByte(i); if (b == '\\' && strEnd > i+3 && (str.getByte(i+1) == 'x' || str.getByte(i+1) == 'X')) { // ok, take next 2 hex digits. byte hd1 = str.getByte(i+2); byte hd2 = str.getByte(i+3); if (isHexDigit(hd1) && isHexDigit(hd2)) { // [a-fA-F0-9] // turn hex ASCII digit -> number b = (byte) ((toBinaryFromHex(hd1) << 4) + toBinaryFromHex(hd2)); i += 3; // skip 3 } } out.setByte(dstEnd++, b); } return dstEnd; }
Example 2
Source File: ZstdEncoder.java From x-pipe with Apache License 2.0 | 6 votes |
private ChannelFuture finishEncode(final ChannelHandlerContext ctx, ChannelPromise promise) { if (finished) { promise.setSuccess(); return promise; } finished = true; final ByteBuf footer = ctx.alloc().ioBuffer( (int) Zstd.compressBound(buffer.readableBytes()) + HEADER_LENGTH); flushBufferedData(footer); final int idx = footer.writerIndex(); footer.setInt(idx, MAGIC_NUMBER); footer.setByte(idx + TOKEN_OFFSET, (byte) (BLOCK_TYPE_NON_COMPRESSED | compressionLevel)); footer.setInt(idx + COMPRESSED_LENGTH_OFFSET, 0); footer.setInt(idx + DECOMPRESSED_LENGTH_OFFSET, 0); footer.setInt(idx + CHECKSUM_OFFSET, 0); footer.writerIndex(idx + HEADER_LENGTH); return ctx.writeAndFlush(footer, promise); }
Example 3
Source File: ViewHandler.java From couchbase-jvm-core with Apache License 2.0 | 5 votes |
/** * Parse out the info portion from the header part of the query response. * * This includes the total rows, but also debug info if attached. */ private void parseViewInfo() { int rowsStart = -1; for (int i = responseContent.readerIndex(); i < responseContent.writerIndex() - 2; i++) { byte curr = responseContent.getByte(i); byte f1 = responseContent.getByte(i + 1); byte f2 = responseContent.getByte(i + 2); if (curr == '"' && f1 == 'r' && f2 == 'o') { rowsStart = i; break; } } if (rowsStart == -1) { return; } ByteBuf info = responseContent.readBytes(rowsStart - responseContent.readerIndex()); int closingPointer = info.forEachByteDesc(new ByteBufProcessor() { @Override public boolean process(byte value) throws Exception { return value != ','; } }); if (closingPointer > 0) { info.setByte(closingPointer, '}'); viewInfoObservable.onNext(info); } else { //JVMCBC-360 don't forget to release the now unused info ByteBuf info.release(); viewInfoObservable.onNext(Unpooled.EMPTY_BUFFER); } viewInfoObservable.onCompleted(); viewParsingState = QUERY_STATE_ROWS; }
Example 4
Source File: NumberFormat.java From pulsar with Apache License 2.0 | 5 votes |
static void format(ByteBuf out, long num) { if (num == 0) { out.writeByte('0'); return; } // Long.MIN_VALUE needs special handling since abs(Long.MIN_VALUE) = abs(Long.MAX_VALUE) + 1 boolean encounteredMinValue = (num == Long.MIN_VALUE); if (num < 0) { out.writeByte('-'); num += encounteredMinValue ? 1 : 0; num *= -1; } // Putting the number in bytebuf in reverse order int start = out.writerIndex(); formatHelper(out, num); int end = out.writerIndex(); if (encounteredMinValue) { out.setByte(start, out.getByte(start) + 1); } // Reversing the digits end--; for (int i = 0; i <= (end - start) / 2; i++) { byte tmp = out.getByte(end - i); out.setByte(end - i, out.getByte(start + i)); out.setByte(start + i, tmp); } }
Example 5
Source File: EnvelopedEntryWriter.java From distributedlog with Apache License 2.0 | 5 votes |
private ByteBuf finalizeBuffer() { if (!envelopeBeforeTransmit) { return buffer.retain(); } int dataOffset = HEADER_LENGTH; int dataLen = buffer.readableBytes() - HEADER_LENGTH; if (Type.NONE == codec) { // update version buffer.setByte(VERSION_OFFSET, CURRENT_VERSION); // update the flags buffer.setInt(FLAGS_OFFSET, flags); // update data len buffer.setInt(DECOMPRESSED_SIZE_OFFSET, dataLen); buffer.setInt(COMPRESSED_SIZE_OFFSET, dataLen); return buffer.retain(); } // compression CompressionCodec compressor = CompressionUtils.getCompressionCodec(codec); ByteBuf uncompressedBuf = buffer.slice(dataOffset, dataLen); ByteBuf compressedBuf = compressor.compress(uncompressedBuf, HEADER_LENGTH); // update version compressedBuf.setByte(VERSION_OFFSET, CURRENT_VERSION); // update the flags compressedBuf.setInt(FLAGS_OFFSET, flags); // update data len compressedBuf.setInt(DECOMPRESSED_SIZE_OFFSET, dataLen); compressedBuf.setInt(COMPRESSED_SIZE_OFFSET, compressedBuf.readableBytes() - HEADER_LENGTH); return compressedBuf; }
Example 6
Source File: WebSocket08FrameDecoder.java From netty4.0.27Learn with Apache License 2.0 | 5 votes |
private void unmask(ByteBuf frame) { int i = frame.readerIndex(); int end = frame.writerIndex(); ByteOrder order = frame.order(); // Remark: & 0xFF is necessary because Java will do signed expansion from // byte to int which we don't want. int intMask = ((maskingKey[0] & 0xFF) << 24) | ((maskingKey[1] & 0xFF) << 16) | ((maskingKey[2] & 0xFF) << 8) | (maskingKey[3] & 0xFF); // If the byte order of our buffers it little endian we have to bring our mask // into the same format, because getInt() and writeInt() will use a reversed byte order if (order == ByteOrder.LITTLE_ENDIAN) { intMask = Integer.reverseBytes(intMask); } for (; i + 3 < end; i += 4) { int unmasked = frame.getInt(i) ^ intMask; frame.setInt(i, unmasked); } for (; i < end; i++) { frame.setByte(i, frame.getByte(i) ^ maskingKey[i % 4]); } }
Example 7
Source File: StringFunctionUtil.java From dremio-oss with Apache License 2.0 | 5 votes |
/** * Copy the UTF8 symbols from 'in' to 'out'. Any non-UTF8 symbols in 'in' will be replaced by 'replacement' * @param in source of the copy * @param start index in 'in' where the copy will begin * @param end one past the index in 'in' where the copy will end * @param out the destination buffer, that will receive copy/replace. The buffer is assumed to be properly sized * The buffer's reader- and writer-index will be set appropriately. * @param replacement replacement value for non-UTF8 symbols in 'in' * @return the number of bytes in 'out' that were populated */ public static int copyReplaceUtf8(ByteBuf in, final int start, final int end, ByteBuf out, byte replacement) { int i = 0; while (start + i < end) { // Optimize for long runs of ASCII sequences byte b = in.getByte(start + i); if (b >= 0) { out.setByte(i, b); i++; continue; } int seqLen = utf8CharLenNoThrow(in, start + i); if (seqLen == 0 || (start + i + seqLen) > end || !GuavaUtf8.isUtf8(in, start + i, start + i + seqLen)) { // Replace the non-UTF-8 character with the replacement character out.setByte(i, replacement); i++; } else { for (int j = i; j < i + seqLen; j++) { out.setByte(j, in.getByte(start + j)); } i += seqLen; } } return end - start; }
Example 8
Source File: Base64.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
public static ByteBuf encode( ByteBuf src, int off, int len, boolean breakLines, Base64Dialect dialect, ByteBufAllocator allocator) { if (src == null) { throw new NullPointerException("src"); } if (dialect == null) { throw new NullPointerException("dialect"); } ByteBuf dest = allocator.buffer(encodedBufferSize(len, breakLines)).order(src.order()); byte[] alphabet = alphabet(dialect); int d = 0; int e = 0; int len2 = len - 2; int lineLength = 0; for (; d < len2; d += 3, e += 4) { encode3to4(src, d + off, 3, dest, e, alphabet); lineLength += 4; if (breakLines && lineLength == MAX_LINE_LENGTH) { dest.setByte(e + 4, NEW_LINE); e ++; lineLength = 0; } // end if: end of line } // end for: each piece of array if (d < len) { encode3to4(src, d + off, len - d, dest, e, alphabet); e += 4; } // end if: some padding needed // Remove last byte if it's a newline if (e > 1 && dest.getByte(e - 1) == NEW_LINE) { e--; } return dest.slice(0, e); }
Example 9
Source File: TsiTest.java From grpc-java with Apache License 2.0 | 5 votes |
/** Test corrupted ciphertext. */ public static void corruptedCiphertextTest(Handshakers handshakers, RegisterRef ref) throws GeneralSecurityException { performHandshake(DEFAULT_TRANSPORT_BUFFER_SIZE, handshakers); TsiFrameProtector sender = handshakers.getClient().createFrameProtector(alloc); TsiFrameProtector receiver = handshakers.getServer().createFrameProtector(alloc); String message = "hello world"; ByteBuf plaintextBuffer = Unpooled.wrappedBuffer(message.getBytes(UTF_8)); final List<ByteBuf> protectOut = new ArrayList<>(); List<Object> unprotectOut = new ArrayList<>(); sender.protectFlush( Collections.singletonList(plaintextBuffer), new Consumer<ByteBuf>() { @Override public void accept(ByteBuf buf) { protectOut.add(buf); } }, alloc); assertThat(protectOut.size()).isEqualTo(1); ByteBuf protect = ref.register(protectOut.get(0)); int ciphertextIdx = protect.writerIndex() - FakeChannelCrypter.getTagBytes() - 2; protect.setByte(ciphertextIdx, protect.getByte(ciphertextIdx) + 1); try { receiver.unprotect(protect, unprotectOut, alloc); fail("Exception expected"); } catch (AEADBadTagException ex) { assertThat(ex).hasMessageThat().containsMatch(DECRYPTION_FAILURE_RE); } sender.destroy(); receiver.destroy(); }
Example 10
Source File: WebSocket08FrameDecoder.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
private void unmask(ByteBuf frame) { int i = frame.readerIndex(); int end = frame.writerIndex(); ByteOrder order = frame.order(); // Remark: & 0xFF is necessary because Java will do signed expansion from // byte to int which we don't want. int intMask = ((maskingKey[0] & 0xFF) << 24) | ((maskingKey[1] & 0xFF) << 16) | ((maskingKey[2] & 0xFF) << 8) | (maskingKey[3] & 0xFF); // If the byte order of our buffers it little endian we have to bring our mask // into the same format, because getInt() and writeInt() will use a reversed byte order if (order == ByteOrder.LITTLE_ENDIAN) { intMask = Integer.reverseBytes(intMask); } for (; i + 3 < end; i += 4) { int unmasked = frame.getInt(i) ^ intMask; frame.setInt(i, unmasked); } for (; i < end; i++) { frame.setByte(i, frame.getByte(i) ^ maskingKey[i % 4]); } }
Example 11
Source File: CallFrame.java From tchannel-java with MIT License | 5 votes |
public final ByteBuf encodePayload(@NotNull ByteBufAllocator allocator, @NotNull List<ByteBuf> args) { ByteBuf payload = CodecUtils.writeArgs(allocator, encodeHeader(allocator), args); if (args.isEmpty()) { this.flags = 0; payload.setByte(0, 0); } else { this.flags = 1; payload.setByte(0, 1); } this.payload = payload; return payload; }
Example 12
Source File: HttpDataTest.java From armeria with Apache License 2.0 | 5 votes |
@Test void copyOfByteBuf() { final ByteBuf payload = Unpooled.copiedBuffer(new byte[] { 1, 2, 3, 4 }); final HttpData data = HttpData.copyOf(payload); payload.setByte(1, 5); assertThat(ByteBufUtil.getBytes(payload)).containsExactly(1, 5, 3, 4); assertThat(data.array()).containsExactly(1, 2, 3, 4); payload.release(); }
Example 13
Source File: Packet.java From riiablo with Apache License 2.0 | 4 votes |
static void setFragmentId(ByteBuf bb, int value) { bb.setByte(FRAGID_OFFSET, value); }
Example 14
Source File: AsciiHeadersEncoder.java From netty-4.1.22 with Apache License 2.0 | 4 votes |
public void encode(Entry<CharSequence, CharSequence> entry) { final CharSequence name = entry.getKey(); final CharSequence value = entry.getValue(); final ByteBuf buf = this.buf; final int nameLen = name.length(); final int valueLen = value.length(); final int entryLen = nameLen + valueLen + 4; int offset = buf.writerIndex(); buf.ensureWritable(entryLen); writeAscii(buf, offset, name); offset += nameLen; switch (separatorType) { case COLON: buf.setByte(offset ++, ':'); break; case COLON_SPACE: buf.setByte(offset ++, ':'); buf.setByte(offset ++, ' '); break; default: throw new Error(); } writeAscii(buf, offset, value); offset += valueLen; switch (newlineType) { case LF: buf.setByte(offset ++, '\n'); break; case CRLF: buf.setByte(offset ++, '\r'); buf.setByte(offset ++, '\n'); break; default: throw new Error(); } buf.writerIndex(offset); }
Example 15
Source File: Packet.java From riiablo with Apache License 2.0 | 4 votes |
static void setProtocol(ByteBuf bb, int value) { bb.setByte(PROTOCOL_OFFSET, value); }
Example 16
Source File: Packet.java From riiablo with Apache License 2.0 | 4 votes |
static void setNumFragments(ByteBuf bb, int value) { bb.setByte(NUMFRAG_OFFSET, value); }
Example 17
Source File: RpcEncoder.java From spring-boot-protocol with Apache License 2.0 | 4 votes |
protected void encodePacket(RequestPacket packet,ByteBuf out){ int writeCurrentLength; int writeTotalLength = FIXED_REQUEST_LENGTH; //(8 byte) protocol head out.writeBytes(PROTOCOL_HEADER); //(1 byte Unsigned) RPC packet type out.writeByte(RpcPacket.TYPE_REQUEST); //(1 byte Unsigned) RPC packet ack out.writeByte(packet.getAck()); //(4 byte Unsigned) total length int writerTotalLengthIndex = out.writerIndex(); out.writerIndex(writerTotalLengthIndex + INT_LENGTH); //(4 byte) Request ID out.writeInt(packet.getRequestId()); //(length byte) service name out.writerIndex(out.writerIndex() + BYTE_LENGTH); writeCurrentLength = out.writeCharSequence(packet.getRequestMappingName(), RPC_CHARSET); //(1 byte Unsigned) service name length out.setByte(out.writerIndex() - writeCurrentLength - BYTE_LENGTH,writeCurrentLength); writeTotalLength += writeCurrentLength; //(length byte) service version out.writerIndex(out.writerIndex() + BYTE_LENGTH); writeCurrentLength = out.writeCharSequence(packet.getVersion(), RPC_CHARSET); //(1 byte Unsigned) service version length out.setByte(out.writerIndex() - writeCurrentLength - BYTE_LENGTH,writeCurrentLength); writeTotalLength += writeCurrentLength; //(length byte Unsigned) method name out.writerIndex(out.writerIndex() + BYTE_LENGTH); writeCurrentLength = out.writeCharSequence(packet.getMethodName(), RPC_CHARSET); //(1 byte Unsigned) method length out.setByte(out.writerIndex() - writeCurrentLength - BYTE_LENGTH,writeCurrentLength); writeTotalLength += writeCurrentLength; //(4 byte Unsigned) data length byte[] data = packet.getData(); out.writeInt(data.length); if(data.length > 0){ //(length byte) data out.writeBytes(data); writeTotalLength += data.length; } //set total length Unsigned out.setInt(writerTotalLengthIndex,writeTotalLength); }
Example 18
Source File: RpcEncoder.java From spring-boot-protocol with Apache License 2.0 | 4 votes |
protected void encodePacket(ResponsePacket packet,ByteBuf out){ int writeCurrentLength; int writeTotalLength = FIXED_RESPONSE_LENGTH; //(8 byte) protocol head out.writeBytes(PROTOCOL_HEADER); //(1 byte Unsigned) RPC packet type out.writeByte(RpcPacket.TYPE_RESPONSE); //(1 byte Unsigned) RPC packet ack out.writeByte(packet.getAck()); //(4 byte Unsigned) total length int writerTotalLengthIndex = out.writerIndex(); out.writerIndex(writerTotalLengthIndex + INT_LENGTH); //(4 byte) Request ID out.writeInt(packet.getRequestId()); //(2 byte Unsigned) Response status out.writeShort(packet.getStatus()); //(1 byte Unsigned) Whether the data has been encoded out.writeByte(packet.getEncode().getCode()); //(length byte) Response information out.writerIndex(out.writerIndex() + BYTE_LENGTH); writeCurrentLength = out.writeCharSequence(packet.getMessage(), RPC_CHARSET); //(1 byte Unsigned) Response information length out.setByte(out.writerIndex() - writeCurrentLength - BYTE_LENGTH,writeCurrentLength); writeTotalLength += writeCurrentLength; //(4 byte Unsigned) data length byte[] data = packet.getData(); writeCurrentLength = data == null? 0: data.length; out.writeInt(writeCurrentLength); if(writeCurrentLength > 0) { out.writeBytes(data); //(length byte) data writeTotalLength += writeCurrentLength; } //set total length out.setInt(writerTotalLengthIndex,writeTotalLength); }
Example 19
Source File: EchoClientTest.java From netty-learning with MIT License | 4 votes |
/** * set 方法 */ @Test public void setByteTest() { ByteBuf byteBuf = Unpooled.copiedBuffer("netty", CharsetUtil.UTF_8); System.out.println((char) byteBuf.getByte(0)); int readerIndex = byteBuf.readerIndex(); LOGGER.info("readerIndex={}", readerIndex); int writerIndex = byteBuf.writerIndex(); LOGGER.info("writerIndex={}", writerIndex); byteBuf.setByte(0, (byte) 'b'); System.out.println((char) byteBuf.getByte(0)); readerIndex = byteBuf.readerIndex(); LOGGER.info("readerIndex={}", readerIndex); writerIndex = byteBuf.writerIndex(); LOGGER.info("writerIndex={}", writerIndex); }
Example 20
Source File: NonBlockingRouterTest.java From ambry with Apache License 2.0 | 4 votes |
/** * Test that operations succeed even in the presence of responses that are corrupt and fail to deserialize. * @param opHelper the {@link OperationHelper} * @param networkClient the {@link SocketNetworkClient} * @param blobId the id of the blob to get/delete. For puts, this will be null. * @throws Exception */ private void testResponseDeserializationError(OperationHelper opHelper, SocketNetworkClient networkClient, BlobId blobId) throws Exception { mockSelectorState.set(MockSelectorState.Good); FutureResult futureResult = opHelper.submitOperation(blobId); int requestParallelism = opHelper.requestParallelism; List<RequestInfo> allRequests = new ArrayList<>(); Set<Integer> allDropped = new HashSet<>(); long loopStartTimeMs = SystemTime.getInstance().milliseconds(); while (allRequests.size() < requestParallelism) { if (loopStartTimeMs + AWAIT_TIMEOUT_MS < SystemTime.getInstance().milliseconds()) { Assert.fail("Waited too long for requests."); } opHelper.pollOpManager(allRequests, allDropped); } List<ResponseInfo> responseInfoList = new ArrayList<>(); loopStartTimeMs = SystemTime.getInstance().milliseconds(); do { if (loopStartTimeMs + AWAIT_TIMEOUT_MS < SystemTime.getInstance().milliseconds()) { Assert.fail("Waited too long for the response."); } responseInfoList.addAll(networkClient.sendAndPoll(allRequests, allDropped, 10)); allRequests.clear(); } while (responseInfoList.size() < requestParallelism); // corrupt the first response. ByteBuf response = responseInfoList.get(0).content(); byte b = response.getByte(response.writerIndex() - 1); response.setByte(response.writerIndex() - 1, (byte) ~b); for (ResponseInfo responseInfo : responseInfoList) { opHelper.handleResponse(responseInfo); } responseInfoList.forEach(ResponseInfo::release); allRequests.clear(); if (testEncryption) { opHelper.awaitOpCompletionOrTimeOut(futureResult); } else { opHelper.pollOpManager(allRequests, allDropped); } try { futureResult.get(AWAIT_TIMEOUT_MS, TimeUnit.MILLISECONDS); } catch (ExecutionException e) { Assert.fail("Operation should have succeeded with one corrupt response"); } }