List of usage examples for io.netty.buffer ByteBuf copy
public abstract ByteBuf copy();
From source file:com.couchbase.client.core.endpoint.kv.KeyValueHandlerTest.java
License:Apache License
@Test public void shouldDecodeObserveResponseDuringRebalance() throws Exception { ByteBuf content = Unpooled.copiedBuffer("{someconfig...}", CharsetUtil.UTF_8); FullBinaryMemcacheResponse response = new DefaultFullBinaryMemcacheResponse(new byte[] {}, Unpooled.EMPTY_BUFFER, content.copy()); response.setStatus(KeyValueStatus.ERR_NOT_MY_VBUCKET.code()); ObserveRequest requestMock = mock(ObserveRequest.class); requestQueue.add(requestMock);/* www . j a va 2 s. c o m*/ channel.writeInbound(response); assertEquals(1, eventSink.responseEvents().size()); ObserveResponse event = (ObserveResponse) eventSink.responseEvents().get(0).getMessage(); assertEquals(ResponseStatus.RETRY, event.status()); assertEquals(ObserveResponse.ObserveStatus.UNKNOWN, event.observeStatus()); }
From source file:com.couchbase.client.core.endpoint.query.QueryHandler.java
License:Apache License
/** * Parse the signature section in the N1QL response. *//* w w w. j a v a 2 s.c om*/ private void parseQuerySignature(boolean lastChunk) { ByteBufProcessor processor = null; //signature can be any valid JSON item, which get tricky to detect //let's try to find out what's the boundary character int openPos = responseContent.forEachByte(new WhitespaceSkipper()) - responseContent.readerIndex(); if (openPos < 0) { //only whitespace left in the buffer, need more data return; } char openChar = (char) responseContent.getByte(responseContent.readerIndex() + openPos); if (openChar == '{') { processor = new ClosingPositionBufProcessor('{', '}', true); } else if (openChar == '[') { processor = new ClosingPositionBufProcessor('[', ']', true); } else if (openChar == '"') { processor = new StringClosingPositionBufProcessor(); } //else this should be a scalar, skip processor int closePos; if (processor != null) { closePos = responseContent.forEachByte(processor) - responseContent.readerIndex(); } else { closePos = findNextChar(responseContent, ',') - 1; } if (closePos > 0) { responseContent.skipBytes(openPos); int length = closePos - openPos + 1; ByteBuf signature = responseContent.readSlice(length); querySignatureObservable.onNext(signature.copy()); } else { //wait for more data return; } //note: the signature section could be absent, so we'll make sure to complete the observable // when receiving status since this is in every well-formed response. sectionDone(); queryParsingState = transitionToNextToken(lastChunk); }
From source file:com.couchbase.client.core.endpoint.query.QueryHandler.java
License:Apache License
/** * Parses the query rows from the content stream as long as there is data to be found. *//*from ww w .ja va 2 s. co m*/ private void parseQueryRows(boolean lastChunk) { while (true) { int openBracketPos = findNextChar(responseContent, '{'); if (isEmptySection(openBracketPos) || (lastChunk && openBracketPos < 0)) { sectionDone(); queryParsingState = transitionToNextToken(lastChunk); break; } int closeBracketPos = findSectionClosingPosition(responseContent, '{', '}'); if (closeBracketPos == -1) { break; } int length = closeBracketPos - openBracketPos - responseContent.readerIndex() + 1; responseContent.skipBytes(openBracketPos); ByteBuf resultSlice = responseContent.readSlice(length); queryRowObservable.onNext(resultSlice.copy()); responseContent.discardSomeReadBytes(); } }
From source file:com.couchbase.client.core.endpoint.query.QueryHandler.java
License:Apache License
/** * Parses the query raw results from the content stream as long as there is data to be found. *///from w ww. ja v a 2 s. c o m private void parseQueryRowsRaw(boolean lastChunk) { while (responseContent.isReadable()) { int splitPos = findSplitPosition(responseContent, ','); int arrayEndPos = findSplitPosition(responseContent, ']'); boolean doSectionDone = false; if (splitPos == -1 && arrayEndPos == -1) { //need more data break; } else if (arrayEndPos > 0 && (arrayEndPos < splitPos || splitPos == -1)) { splitPos = arrayEndPos; doSectionDone = true; } int length = splitPos - responseContent.readerIndex(); ByteBuf resultSlice = responseContent.readSlice(length); queryRowObservable.onNext(resultSlice.copy()); responseContent.skipBytes(1); responseContent.discardReadBytes(); if (doSectionDone) { sectionDone(); queryParsingState = transitionToNextToken(lastChunk); break; } } }
From source file:com.couchbase.client.core.endpoint.query.QueryHandler.java
License:Apache License
/** * Parses the errors and warnings from the content stream as long as there are some to be found. *//*from www. j av a 2s.c o m*/ private void parseQueryError(boolean lastChunk) { while (true) { int openBracketPos = findNextChar(responseContent, '{'); if (isEmptySection(openBracketPos) || (lastChunk && openBracketPos < 0)) { sectionDone(); queryParsingState = transitionToNextToken(lastChunk); //warnings or status break; } int closeBracketPos = findSectionClosingPosition(responseContent, '{', '}'); if (closeBracketPos == -1) { break; } int length = closeBracketPos - openBracketPos - responseContent.readerIndex() + 1; responseContent.skipBytes(openBracketPos); ByteBuf resultSlice = responseContent.readSlice(length); queryErrorObservable.onNext(resultSlice.copy()); } }
From source file:com.couchbase.client.core.endpoint.view.ViewCodec.java
License:Open Source License
private void handleViewQueryResponse(ChannelHandlerContext ctx, HttpObject msg, List<Object> in) { switch (currentState) { case INITIAL: if (msg instanceof HttpResponse) { HttpResponse response = (HttpResponse) msg; // Todo: error handling or retry based on the http response code. currentState = ParsingState.PREAMBLE; return; } else {/*from w w w. ja va 2 s . c om*/ throw new IllegalStateException("Only expecting HttpResponse in INITIAL"); } case PREAMBLE: if (msg instanceof HttpContent) { HttpContent content = (HttpContent) msg; if (content.content().readableBytes() > 0) { currentChunk.writeBytes(content.content()); content.content().clear(); } int pos = currentChunk.bytesBefore((byte) ','); if (pos > 0) { String[] rowsInfo = currentChunk.readBytes(pos + 1).toString(CharsetUtil.UTF_8).split(":"); currentTotalRows = Integer.parseInt(rowsInfo[1].substring(0, rowsInfo[1].length() - 1)); } else { return; } if (currentChunk.readableBytes() >= 8) { currentChunk.readerIndex(currentChunk.readerIndex() + 8); } else { return; } } else { throw new IllegalStateException("Only expecting HttpContent in PREAMBLE"); } currentState = ParsingState.ROWS; case ROWS: if (msg instanceof HttpContent) { HttpContent content = (HttpContent) msg; if (content.content().readableBytes() > 0) { currentChunk.writeBytes(content.content()); content.content().clear(); } MarkerProcessor processor = new MarkerProcessor(); currentChunk.forEachByte(processor); boolean last = msg instanceof LastHttpContent; ResponseStatus status = last ? ResponseStatus.SUCCESS : ResponseStatus.CHUNKED; ByteBuf returnContent = currentChunk.readBytes(processor.marker()); if (processor.marker() > 0 || last) { in.add(new ViewQueryResponse(status, currentTotalRows, returnContent.copy())); currentChunk.discardSomeReadBytes(); } returnContent.release(); if (last) { currentRequest = null; currentChunk.release(); currentChunk = null; currentState = ParsingState.INITIAL; } } else { throw new IllegalStateException("Only expecting HttpContent in ROWS"); } } }
From source file:com.digitalpetri.opcua.stack.core.channel.ChunkEncoder.java
License:Apache License
private List<ByteBuf> encode(Delegate delegate, SecureChannel channel, MessageType messageType, ByteBuf messageBuffer, long requestId) throws UaException { List<ByteBuf> chunks = new ArrayList<>(); boolean encrypted = delegate.isEncryptionEnabled(channel); int securityHeaderSize = delegate.getSecurityHeaderSize(channel); int cipherTextBlockSize = delegate.getCipherTextBlockSize(channel); int plainTextBlockSize = delegate.getPlainTextBlockSize(channel); int signatureSize = delegate.getSignatureSize(channel); int maxChunkSize = parameters.getLocalSendBufferSize(); int headerSizes = SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE + securityHeaderSize; int paddingOverhead = encrypted ? (cipherTextBlockSize > 256 ? 2 : 1) : 0; int maxBlockCount = (maxChunkSize - headerSizes - signatureSize - paddingOverhead) / cipherTextBlockSize; int maxBodySize = (plainTextBlockSize * maxBlockCount - SequenceHeader.SEQUENCE_HEADER_SIZE); while (messageBuffer.readableBytes() > 0) { int bodySize = Math.min(messageBuffer.readableBytes(), maxBodySize); int paddingSize = encrypted ? plainTextBlockSize//from ww w . ja v a 2 s .c o m - (SequenceHeader.SEQUENCE_HEADER_SIZE + bodySize + signatureSize + paddingOverhead) % plainTextBlockSize : 0; int plainTextContentSize = SequenceHeader.SEQUENCE_HEADER_SIZE + bodySize + signatureSize + paddingSize + paddingOverhead; assert (plainTextContentSize % plainTextBlockSize == 0); int chunkSize = SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE + securityHeaderSize + (plainTextContentSize / plainTextBlockSize) * cipherTextBlockSize; ByteBuf chunkBuffer = BufferUtil.buffer(chunkSize); /* Message Header */ SecureMessageHeader messageHeader = new SecureMessageHeader(messageType, messageBuffer.readableBytes() > bodySize ? 'C' : 'F', chunkSize, channel.getChannelId()); SecureMessageHeader.encode(messageHeader, chunkBuffer); /* Security Header */ delegate.encodeSecurityHeader(channel, chunkBuffer); /* Sequence Header */ SequenceHeader sequenceHeader = new SequenceHeader(sequenceNumber.getAndIncrement(), requestId); SequenceHeader.encode(sequenceHeader, chunkBuffer); /* Message Body */ chunkBuffer.writeBytes(messageBuffer, bodySize); /* Padding and Signature */ if (encrypted) { writePadding(cipherTextBlockSize, paddingSize, chunkBuffer); } if (delegate.isSigningEnabled(channel)) { ByteBuffer chunkNioBuffer = chunkBuffer.nioBuffer(0, chunkBuffer.writerIndex()); byte[] signature = delegate.signChunk(channel, chunkNioBuffer); chunkBuffer.writeBytes(signature); } /* Encryption */ if (encrypted) { chunkBuffer.readerIndex(SecureMessageHeader.SECURE_MESSAGE_HEADER_SIZE + securityHeaderSize); assert (chunkBuffer.readableBytes() % plainTextBlockSize == 0); try { int blockCount = chunkBuffer.readableBytes() / plainTextBlockSize; ByteBuffer chunkNioBuffer = chunkBuffer.nioBuffer(chunkBuffer.readerIndex(), blockCount * cipherTextBlockSize); ByteBuf copyBuffer = chunkBuffer.copy(); ByteBuffer plainTextNioBuffer = copyBuffer.nioBuffer(); Cipher cipher = delegate.getAndInitializeCipher(channel); if (delegate instanceof AsymmetricDelegate) { for (int blockNumber = 0; blockNumber < blockCount; blockNumber++) { int position = blockNumber * plainTextBlockSize; int limit = (blockNumber + 1) * plainTextBlockSize; plainTextNioBuffer.position(position).limit(limit); int bytesWritten = cipher.doFinal(plainTextNioBuffer, chunkNioBuffer); assert (bytesWritten == cipherTextBlockSize); } } else { cipher.doFinal(plainTextNioBuffer, chunkNioBuffer); } copyBuffer.release(); } catch (GeneralSecurityException e) { throw new UaException(StatusCodes.Bad_SecurityChecksFailed, e); } } chunkBuffer.readerIndex(0).writerIndex(chunkSize); chunks.add(chunkBuffer); } lastRequestId = requestId; return chunks; }
From source file:com.eightkdata.mongowp.bson.netty.pool.GuavaStringPool.java
License:Open Source License
@Override protected String retrieveFromPool(@Tight @ConservesIndexes ByteBuf stringBuf) { String result = cache.getIfPresent(stringBuf); if (result == null) { result = cache.getUnchecked(stringBuf.copy()); }// www . j a v a 2s . c o m return result; }
From source file:com.github.subalakr.yasjl.ByteBufJsonParser.java
License:Apache License
private void readValue(JsonLevel level) throws Exception { int readerIndex = this.content.readerIndex(); ByteBufProcessor processor = null;/* w w w . jav a2 s .co m*/ Mode mode = level.peekMode(); switch (mode) { case JSON_ARRAY_VALUE: arProcessor.reset(); processor = arProcessor; break; case JSON_OBJECT_VALUE: obProcessor.reset(); processor = obProcessor; break; case JSON_STRING_VALUE: case JSON_STRING_HASH_KEY: processor = stProcessor; break; case JSON_NULL_VALUE: nullProcessor.reset(); processor = nullProcessor; break; case JSON_BOOLEAN_TRUE_VALUE: processor = trueProcessor; break; case JSON_BOOLEAN_FALSE_VALUE: processor = falseProcessor; break; case JSON_NUMBER_VALUE: processor = numProcessor; break; } int length; boolean shouldSaveValue = this.tree.isTerminalPath(level.jsonPointer()) || mode == Mode.JSON_STRING_HASH_KEY; int lastValidIndex = this.content.forEachByte(processor); if (lastValidIndex == -1) { if (mode != JSON_NUMBER_VALUE) { throw new EOFException("Needs more input (Level: " + level.jsonPointer().toString() + ")"); } else { length = 1; ByteBuf slice = this.content.slice(readerIndex - 1, length); level.setCurrentValue(slice.copy(), length); //no need to skip here level.emitJsonPointerValue(); } } else { if (mode == Mode.JSON_OBJECT_VALUE || mode == Mode.JSON_ARRAY_VALUE || mode == Mode.JSON_STRING_VALUE || mode == Mode.JSON_STRING_HASH_KEY || mode == Mode.JSON_NULL_VALUE || mode == Mode.JSON_BOOLEAN_TRUE_VALUE || mode == Mode.JSON_BOOLEAN_FALSE_VALUE) { length = lastValidIndex - readerIndex + 1; if (shouldSaveValue) { ByteBuf slice = this.content.slice(readerIndex - 1, length + 1); level.setCurrentValue(slice.copy(), length); level.emitJsonPointerValue(); } this.content.skipBytes(length); } else { //special handling for number as they don't need structural tokens intact //and the processor returns only on an unacceptable value rather than a finite state automaton length = lastValidIndex - readerIndex; if (length > 0) { if (shouldSaveValue) { ByteBuf slice = this.content.slice(readerIndex - 1, length + 1); level.setCurrentValue(slice.copy(), length); level.emitJsonPointerValue(); } this.content.skipBytes(length); } else { length = 1; if (shouldSaveValue) { ByteBuf slice = this.content.slice(readerIndex - 1, length); level.setCurrentValue(slice.copy(), length); level.emitJsonPointerValue(); } } } } //DONT THIS HERE: it should be app to do it after calling parse, or releasing the input //bytebuf //this.content.discardReadBytes(); if (mode != Mode.JSON_STRING_HASH_KEY) { level.removeLastTokenFromJsonPointer(); } level.popMode(); }
From source file:com.hazelcast.openshift.ProxyForwardHandler.java
License:Open Source License
@Override protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception { ByteBuf copy = msg.copy(); forward.writeAndFlush(copy);//w w w . j a v a2 s. c o m }