List of usage examples for io.netty.buffer ByteBuf isReadable
public abstract boolean isReadable();
From source file:divconq.ctp.stream.FunnelStream.java
License:Open Source License
public boolean hasMore() { FileDescriptor curr = this.current; if (curr == null) return false; if (!this.relayed) // TODO what about EOF, we need to send that along, so even first is not enough? return true; ByteBuf payload = this.currbuf; return (payload != null) && payload.isReadable(); }
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);// ww w . ja v a 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 .jav a 2 s . co 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
@Override public ReturnOption handle(FileDescriptor file, ByteBuf data) { if (file == FileDescriptor.FINAL) return this.downstream.handle(file, data); if (this.ufile == null) this.initializeFileValues(file); // inflate the payload into 1 or more outgoing buffers set in a queue ByteBuf in = data;/* w w w . ja v a 2 s .c o m*/ if (in != null) { ByteBuf rem = this.remnant; ByteBuf src = ((rem != null) && rem.isReadable()) ? Unpooled.copiedBuffer(rem, in) : in; this.inflate(src); // if there are any unread bytes here we need to store them and combine with the next "handle" // this would be rare since the header and footer are small, but it is possible and should be handled // file content has its own "in progress" buffer so no need to worry about that this.remnant = src.isReadable() ? src.copy() : null; // TODO wrap or slice here? we need copy above if (in != null) in.release(); if (rem != null) rem.release(); if (OperationContext.get().getTaskRun().isKilled()) return ReturnOption.DONE; } // write all buffers in the queue while (this.outlist.size() > 0) { ReturnOption ret = this.nextMessage(); if (ret != ReturnOption.CONTINUE) return ret; } // if we reached done and we wrote all the buffers, then send the EOF marker if not already if ((this.gzipState == GzipState.DONE) && !this.eofsent) return this.nextMessage(); // otherwise we need more data 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 a2 s . c o m 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 w w . ja va 2 s . c o m*/ 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.http.multipart.MixedFileUpload.java
License:Apache License
@Override public void addContent(ByteBuf buffer, boolean last) throws IOException { if (fileUpload instanceof MemoryFileUpload) { checkSize(fileUpload.length() + buffer.readableBytes()); if (fileUpload.length() + buffer.readableBytes() > limitSize) { DiskFileUpload diskFileUpload = new DiskFileUpload(fileUpload.getName(), fileUpload.getFilename(), fileUpload.getContentType(), fileUpload.getContentTransferEncoding(), fileUpload.getCharset(), definedSize); diskFileUpload.setMaxSize(maxSize); ByteBuf data = fileUpload.getByteBuf(); if (data != null && data.isReadable()) { diskFileUpload.addContent(data.retain(), false); }// ww w . ja va 2 s. co m // release old upload fileUpload.release(); fileUpload = diskFileUpload; } } fileUpload.addContent(buffer, last); }
From source file:divconq.net.ByteToMessageDecoder.java
License:Apache License
@Override public final void handlerRemoved(ChannelHandlerContext ctx) throws Exception { ByteBuf buf = internalBuffer(); int readable = buf.readableBytes(); if (buf.isReadable()) { ByteBuf bytes = buf.readBytes(readable); buf.release();//from w w w . j a va 2s. co m ctx.fireChannelRead(bytes); } else { buf.release(); } this.cumulation = null; ctx.fireChannelReadComplete(); this.handlerRemoved0(ctx); }
From source file:divconq.net.ByteToMessageDecoder.java
License:Apache License
protected void callDecode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) { try {/*from w ww . ja v a 2 s . com*/ while (in.isReadable()) { int outSize = out.size(); int oldInputLength = in.readableBytes(); this.decode(ctx, in, out); // Check if this handler was removed before continuing the loop. // If it was removed, it is not safe to continue to operate on the buffer. // // See https://github.com/netty/netty/issues/1664 if (ctx.isRemoved()) { break; } if (outSize == out.size()) { if (oldInputLength == in.readableBytes()) { break; } else { continue; } } if (oldInputLength == in.readableBytes()) { throw new DecoderException(StringUtil.simpleClassName(getClass()) + ".decode() did not read anything but decoded a message."); } if (this.isSingleDecode()) { break; } } } catch (DecoderException e) { throw e; } catch (Throwable cause) { throw new DecoderException(cause); } }
From source file:divconq.net.ssl.SslHandler.java
License:Apache License
private void wrap(ChannelHandlerContext ctx, boolean inUnwrap) throws SSLException { ByteBuf out = null;//from w w w .j a va 2 s . c o m ChannelPromise promise = null; try { for (;;) { Object msg = pendingUnencryptedWrites.current(); if (msg == null) { break; } if (!(msg instanceof ByteBuf)) { pendingUnencryptedWrites.removeAndWrite(); continue; } ByteBuf buf = (ByteBuf) msg; if (out == null) { out = allocateOutNetBuf(ctx, buf.readableBytes()); } SSLEngineResult result = wrap(engine, buf, out); if (!buf.isReadable()) { promise = pendingUnencryptedWrites.remove(); } else { promise = null; } if (result.getStatus() == Status.CLOSED) { // SSLEngine has been closed already. // Any further write attempts should be denied. pendingUnencryptedWrites.removeAndFailAll(SSLENGINE_CLOSED); return; } else { switch (result.getHandshakeStatus()) { case NEED_TASK: runDelegatedTasks(); break; case FINISHED: setHandshakeSuccess(); // deliberate fall-through case NOT_HANDSHAKING: setHandshakeSuccessIfStillHandshaking(); // deliberate fall-through case NEED_WRAP: finishWrap(ctx, out, promise, inUnwrap); promise = null; out = null; break; case NEED_UNWRAP: return; default: throw new IllegalStateException("Unknown handshake status: " + result.getHandshakeStatus()); } } } } catch (SSLException e) { setHandshakeFailure(e); throw e; } finally { finishWrap(ctx, out, promise, inUnwrap); } }