List of usage examples for io.netty.buffer ByteBuf readerIndex
public abstract int readerIndex();
From source file:de.dfki.kiara.netty.ByteBufferDecoder.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception { final byte[] array; final int offset; final int length = msg.readableBytes(); if (msg.hasArray()) { array = msg.array();/*from w ww .j a va 2s . c o m*/ offset = msg.arrayOffset() + msg.readerIndex(); } else { array = new byte[length]; msg.getBytes(msg.readerIndex(), array, 0, length); offset = 0; } out.add(ByteBuffer.wrap(array, offset, length)); }
From source file:de.felix_klauke.pegasus.protocol.util.ByteBufUtils.java
License:Apache License
/** * * Read an UTF-8 String from a ByteBuf./*from w w w .j a v a 2s .c o m*/ * * @param byteBuf the bytebuf to read from * @return the String */ public static String readUTF8String(ByteBuf byteBuf) { int length = byteBuf.readInt(); String string = byteBuf.toString(byteBuf.readerIndex(), length, Charsets.UTF_8); byteBuf.readerIndex(byteBuf.readerIndex() + length); return string; }
From source file:de.unipassau.isl.evs.ssh.core.network.handler.Decrypter.java
License:Open Source License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> decoded) throws Exception { try {//from www. j a v a 2 s. com final int encryptedLength = in.readInt(); final int decryptedLength = decryptCipher.getOutputSize(encryptedLength); final ByteBuf out = ctx.alloc().buffer(decryptedLength); //Log.v(TAG, "Decrypting " + encryptedLength + "b data to " + decryptedLength + "b of decrypted data"); decryptCipher.doFinal(in.nioBuffer(in.readerIndex(), encryptedLength), out.nioBuffer(out.writerIndex(), decryptedLength)); out.writerIndex(out.writerIndex() + decryptedLength); in.readerIndex(in.readerIndex() + encryptedLength); decoded.add(out); } catch (GeneralSecurityException | RuntimeException e) { ctx.close(); throw e; } }
From source file:de.unipassau.isl.evs.ssh.core.network.handler.Encrypter.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception { final int decryptedLength = in.readableBytes(); final int encryptedLength = encryptCipher.getOutputSize(decryptedLength); //Log.v(TAG, "Encrypting " + decryptedLength + "b data to " + encryptedLength + "b of encrypted data"); out.writeInt(encryptedLength);//from www.j a v a 2s. co m out.ensureWritable(encryptedLength); final ByteBuffer inNio = in.nioBuffer(in.readerIndex(), decryptedLength); final ByteBuffer outNio = out.nioBuffer(out.writerIndex(), encryptedLength); encryptCipher.doFinal(inNio, outNio); if (inNio.hasRemaining()) { Log.wtf(TAG, "Crypto library did not read all bytes for encryption (" + inNio.remaining() + " remaining)"); } if (outNio.hasRemaining()) { Log.wtf(TAG, "Crypto library did not write all bytes for encryption (" + outNio.remaining() + " remaining)"); } out.writerIndex(out.writerIndex() + encryptedLength); in.readerIndex(in.readerIndex() + decryptedLength); }
From source file:divconq.ctp.stream.FunnelStream.java
License:Open Source License
public ReturnOption nextMessage() { FileDescriptor curr = this.current; if (curr == null) return ReturnOption.CONTINUE; FileDescriptor blk = new FileDescriptor(); blk.copyAttributes(curr);/*from w w w . ja va 2 s . c o m*/ ByteBuf payload = this.currbuf; if ((payload != null) && payload.isReadable()) { int ramt = Math.min(this.aperture, payload.readableBytes()); ByteBuf pslice = payload.copy(payload.readerIndex(), ramt); payload.skipBytes(ramt); // TODO blk.payloadoffset = 0; blk.setEof(!payload.isReadable() && curr.isEof()); if (blk.isEof()) { payload.release(); this.current = null; this.currbuf = null; } payload = pslice; } else { blk.setEof(curr.isEof()); if (payload != null) payload.release(); payload = null; this.current = null; this.currbuf = null; } // current has been sent at least once this.relayed = true; return this.downstream.handle(blk, payload); }
From source file:divconq.ctp.stream.SplitStream.java
License:Open Source License
@Override public ReturnOption handle(FileDescriptor file, ByteBuf data) { if (file == FileDescriptor.FINAL) return this.downstream.handle(file, data); ByteBuf in = data; if (in != null) { while (in.isReadable()) { int amt = Math.min(in.readableBytes(), this.size - this.currchunk); ByteBuf out = in.copy(in.readerIndex(), amt); in.skipBytes(amt);/* w w w . j a va2 s. c o m*/ this.currchunk += amt; boolean eof = (this.currchunk == this.size) || (!in.isReadable() && file.isEof()); this.nextMessage(out, file, eof); if (eof) { this.seqnum++; this.currchunk = 0; } } in.release(); } else if (file.isEof()) { this.nextMessage(null, file, false); } // write all messages in the queue while (this.outlist.size() > 0) { ReturnOption ret = this.downstream.handle(this.outlist.remove(0), this.outbuf.remove(0)); if (ret != ReturnOption.CONTINUE) return ret; } return ReturnOption.CONTINUE; }
From source file:divconq.ctp.stream.UngzipStream.java
License:Open Source License
protected void inflate(ByteBuf in) { switch (this.gzipState) { case HEADER_START: if (in.readableBytes() < 10) return; // read magic numbers int magic0 = in.readByte(); int magic1 = in.readByte(); if (magic0 != 31) { OperationContext.get().getTaskRun().kill("Input is not in the GZIP format"); return; }/*from w w w. j a v a 2 s .c om*/ this.crc.update(magic0); this.crc.update(magic1); int method = in.readUnsignedByte(); if (method != Deflater.DEFLATED) { OperationContext.get().getTaskRun() .kill("Unsupported compression method " + method + " in the GZIP header"); return; } this.crc.update(method); this.flags = in.readUnsignedByte(); this.crc.update(this.flags); if ((this.flags & FRESERVED) != 0) { OperationContext.get().getTaskRun().kill("Reserved flags are set in the GZIP header"); return; } // mtime (int) this.crc.update(in.readByte()); this.crc.update(in.readByte()); this.crc.update(in.readByte()); this.crc.update(in.readByte()); this.crc.update(in.readUnsignedByte()); // extra flags this.crc.update(in.readUnsignedByte()); // operating system this.gzipState = GzipState.FLG_READ; case FLG_READ: if ((this.flags & FEXTRA) != 0) { if (in.readableBytes() < 2) return; int xlen1 = in.readUnsignedByte(); int xlen2 = in.readUnsignedByte(); this.crc.update(xlen1); this.crc.update(xlen2); this.xlen |= xlen1 << 8 | xlen2; } this.gzipState = GzipState.XLEN_READ; case XLEN_READ: if (this.xlen != -1) { if (in.readableBytes() < xlen) return; byte[] xtra = new byte[xlen]; in.readBytes(xtra); this.crc.update(xtra); } this.gzipState = GzipState.SKIP_FNAME; case SKIP_FNAME: if ((this.flags & FNAME) != 0) { boolean gotend = false; while (in.isReadable()) { int b = in.readUnsignedByte(); this.crc.update(b); if (b == 0x00) { gotend = true; break; } } if (!gotend) return; } this.gzipState = GzipState.SKIP_COMMENT; case SKIP_COMMENT: if ((this.flags & FCOMMENT) != 0) { boolean gotend = false; while (in.isReadable()) { int b = in.readUnsignedByte(); this.crc.update(b); if (b == 0x00) { gotend = true; break; } } if (!gotend) return; } this.gzipState = GzipState.PROCESS_FHCRC; case PROCESS_FHCRC: if ((this.flags & FHCRC) != 0) { if (in.readableBytes() < 4) return; long crcValue = 0; for (int i = 0; i < 4; ++i) crcValue |= (long) in.readUnsignedByte() << i * 8; long readCrc = crc.getValue(); if (crcValue != readCrc) { OperationContext.get().getTaskRun() .kill("CRC value missmatch. Expected: " + crcValue + ", Got: " + readCrc); return; } } this.crc.reset(); this.gzipState = GzipState.PRROCESS_CONTENT; case PRROCESS_CONTENT: int readableBytes = in.readableBytes(); if (readableBytes < 1) return; if (in.hasArray()) { this.inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), readableBytes); } else { byte[] array = new byte[readableBytes]; in.getBytes(in.readerIndex(), array); this.inflater.setInput(array); } int maxOutputLength = this.inflater.getRemaining() << 1; ByteBuf decompressed = Hub.instance.getBufferAllocator().heapBuffer(maxOutputLength); boolean readFooter = false; byte[] outArray = decompressed.array(); try { while (!this.inflater.needsInput()) { int writerIndex = decompressed.writerIndex(); int outIndex = decompressed.arrayOffset() + writerIndex; int length = decompressed.writableBytes(); if (length == 0) { // completely filled the buffer allocate a new one and start to fill it this.outlist.add(decompressed); decompressed = Hub.instance.getBufferAllocator().heapBuffer(maxOutputLength); outArray = decompressed.array(); continue; } int outputLength = this.inflater.inflate(outArray, outIndex, length); if (outputLength > 0) { decompressed.writerIndex(writerIndex + outputLength); this.crc.update(outArray, outIndex, outputLength); } else { if (this.inflater.needsDictionary()) { if (this.dictionary == null) { OperationContext.get().getTaskRun().kill( "decompression failure, unable to set dictionary as non was specified"); return; } this.inflater.setDictionary(this.dictionary); } } if (this.inflater.finished()) { readFooter = true; break; } } in.skipBytes(readableBytes - this.inflater.getRemaining()); } catch (DataFormatException x) { OperationContext.get().getTaskRun().kill("decompression failure: " + x); return; } finally { if (decompressed.isReadable()) { this.outlist.add(decompressed); } else { decompressed.release(); } } if (!readFooter) break; this.gzipState = GzipState.PROCESS_FOOTER; case PROCESS_FOOTER: if (in.readableBytes() < 8) return; long crcValue = 0; for (int i = 0; i < 4; ++i) crcValue |= (long) in.readUnsignedByte() << i * 8; long readCrc = this.crc.getValue(); if (crcValue != readCrc) { OperationContext.get().getTaskRun() .kill("CRC value missmatch. Expected: " + crcValue + ", Got: " + readCrc); return; } // read ISIZE and verify int dataLength = 0; for (int i = 0; i < 4; ++i) dataLength |= in.readUnsignedByte() << i * 8; int readLength = this.inflater.getTotalOut(); if (dataLength != readLength) { OperationContext.get().getTaskRun() .kill("Number of bytes mismatch. Expected: " + dataLength + ", Got: " + readLength); return; } this.gzipState = GzipState.DONE; case DONE: break; } }
From source file:divconq.ctp.stream.UntarStream.java
License:Open Source License
@Override public ReturnOption handle(FileDescriptor file, ByteBuf data) { if (file == FileDescriptor.FINAL) return this.downstream.handle(file, data); ByteBuf in = data; if (in != null) { while (in.isReadable()) { switch (this.tstate) { case RECORD: // starting a new record if (in.readableBytes() < TarConstants.DEFAULT_RCDSIZE - this.partialLength) { int offset = this.partialLength; this.partialLength += in.readableBytes(); in.readBytes(this.header_buffer, offset, in.readableBytes()); continue; }/*from w ww .j a v a2 s .com*/ in.readBytes(this.header_buffer, this.partialLength, TarConstants.DEFAULT_RCDSIZE - this.partialLength); this.partialLength = 0; //in.readBytes(this.header_buffer, 0, this.header_buffer.length); boolean hasHitEOF = this.isEOFRecord(this.header_buffer); // if we hit this twice in a row we are at the end - however, source will send FINAL anyway so we don't really care if (hasHitEOF) { this.currEntry = null; continue; } try { this.currEntry = new TarArchiveEntry(this.header_buffer, this.encoding); } catch (Exception x) { OperationContext.get().getTaskRun().kill("Error detected parsing the header: " + x); in.release(); return ReturnOption.DONE; } this.tstate = UntarState.XTRAS; case XTRAS: if (!in.isReadable()) continue; // TODO support long names and such - see org.apache.commons.compress.archivers.tar.TarArchiveInputStream if (this.currEntry.isGNULongLinkEntry()) { /* byte[] longLinkData = getLongNameData(); if (longLinkData == null) { // Bugzilla: 40334 // Malformed tar file - long link entry name not followed by // entry return null; } currEntry.setLinkName(encoding.decode(longLinkData)); */ OperationContext.get().getTaskRun().kill("long link currently not supported"); in.release(); return ReturnOption.DONE; } if (this.currEntry.isGNULongNameEntry()) { /* byte[] longNameData = getLongNameData(); if (longNameData == null) { // Bugzilla: 40334 // Malformed tar file - long entry name not followed by // entry return null; } currEntry.setName(encoding.decode(longNameData)); */ OperationContext.get().getTaskRun().kill("long name currently not supported"); in.release(); return ReturnOption.DONE; } if (this.currEntry.isPaxHeader()) { // Process Pax headers /* paxHeaders(); */ OperationContext.get().getTaskRun().kill("pax currently not supported"); in.release(); return ReturnOption.DONE; } if (this.currEntry.isGNUSparse()) { // Process sparse files /* readGNUSparse(); */ OperationContext.get().getTaskRun().kill("sparse currently not supported"); in.release(); return ReturnOption.DONE; } this.tstate = UntarState.PREP; case PREP: if (!in.isReadable()) continue; // TODO remove System.out.println("name: " + this.currEntry.getName()); System.out.println("size: " + this.currEntry.getSize()); System.out.println("modified: " + this.currEntry.getModTime()); // If the size of the next element in the archive has changed // due to a new size being reported in the posix header // information, we update entrySize here so that it contains // the correct value. long entrySize = this.currEntry.getSize(); this.remainContent = entrySize; long numRecords = (entrySize / this.header_buffer.length) + 1; this.remainSkip = (numRecords * this.header_buffer.length) - entrySize; // grab as much as we can from the current buffer int readSize = (int) Math.min(this.remainContent, in.readableBytes()); this.remainContent -= readSize; // handle empty files too if ((readSize > 0) || (this.remainContent == 0)) { System.out.println("reading content: " + readSize); ByteBuf out = in.copy(in.readerIndex(), readSize); int skipSize = (int) Math.min(this.remainSkip, in.readableBytes() - readSize); this.remainSkip -= skipSize; in.skipBytes(readSize + skipSize); this.nextMessage(out); } this.tstate = UntarState.CONTENT; case CONTENT: if (!in.isReadable()) continue; // check if there is still content left in the entry we were last reading from if (this.remainContent > 0) { readSize = (int) Math.min(this.remainContent, in.readableBytes()); this.remainContent -= readSize; //System.out.println("reading content: " + readSize); //ByteBuf out = Hub.instance.getBufferAllocator().heapBuffer((int) readSize); ByteBuf out = in.copy(in.readerIndex(), readSize); int skipSize = (int) Math.min(this.remainSkip, in.readableBytes() - readSize); this.remainSkip -= skipSize; //System.out.println("skipping content: " + skipSize); in.skipBytes(readSize + skipSize); this.nextMessage(out); } if (this.remainContent > 0) continue; this.currEntry = null; this.tstate = UntarState.SKIP; case SKIP: if (!in.isReadable()) continue; // check if there is still padding left in the entry we were last reading from if (this.remainSkip > 0) { int skipSize = (int) Math.min(this.remainSkip, in.readableBytes()); this.remainSkip -= skipSize; //System.out.println("skipping content: " + skipSize); in.skipBytes((int) skipSize); } if (this.remainSkip > 0) continue; this.tstate = UntarState.RECORD; } } in.release(); } // write all messages in the queue while (this.outlist.size() > 0) { ReturnOption ret = this.downstream.handle(this.outlist.remove(0), this.outbuf.remove(0)); if (ret != ReturnOption.CONTINUE) return ret; } return ReturnOption.CONTINUE; }
From source file:divconq.net.ssl.SslHandler.java
License:Apache License
public static boolean isEncrypted(ByteBuf buffer) { if (buffer.readableBytes() < 5) { throw new IllegalArgumentException("buffer must have at least 5 readable bytes"); }//from w ww .ja v a 2s . com return getEncryptedPacketLength(buffer, buffer.readerIndex()) != -1; }
From source file:divconq.net.ssl.SslHandler.java
License:Apache License
@Override protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws SSLException { final int startOffset = in.readerIndex(); final int endOffset = in.writerIndex(); int offset = startOffset; int totalLength = 0; // If we calculated the length of the current SSL record before, use that information. if (packetLength > 0) { if (endOffset - startOffset < packetLength) { return; } else {/*from w w w . j ava2 s . c om*/ offset += packetLength; totalLength = packetLength; packetLength = 0; } } boolean nonSslRecord = false; while (totalLength < OpenSslEngine.MAX_ENCRYPTED_PACKET_LENGTH) { final int readableBytes = endOffset - offset; if (readableBytes < 5) { break; } final int packetLength = getEncryptedPacketLength(in, offset); if (packetLength == -1) { nonSslRecord = true; break; } assert packetLength > 0; if (packetLength > readableBytes) { // wait until the whole packet can be read this.packetLength = packetLength; break; } int newTotalLength = totalLength + packetLength; if (newTotalLength > OpenSslEngine.MAX_ENCRYPTED_PACKET_LENGTH) { // Don't read too much. break; } // We have a whole packet. // Increment the offset to handle the next packet. offset += packetLength; totalLength = newTotalLength; } if (totalLength > 0) { // The buffer contains one or more full SSL records. // Slice out the whole packet so unwrap will only be called with complete packets. // Also directly reset the packetLength. This is needed as unwrap(..) may trigger // decode(...) again via: // 1) unwrap(..) is called // 2) wrap(...) is called from within unwrap(...) // 3) wrap(...) calls unwrapLater(...) // 4) unwrapLater(...) calls decode(...) // // See https://github.com/netty/netty/issues/1534 in.skipBytes(totalLength); final ByteBuffer inNetBuf = in.nioBuffer(startOffset, totalLength); unwrap(ctx, inNetBuf, totalLength); assert !inNetBuf.hasRemaining() || engine.isInboundDone(); } if (nonSslRecord) { // Not an SSL/TLS packet NotSslRecordException e = new NotSslRecordException( "not an SSL/TLS record: " + ByteBufUtil.hexDump(in)); in.skipBytes(in.readableBytes()); ctx.fireExceptionCaught(e); setHandshakeFailure(e); } }