List of usage examples for io.netty.buffer ByteBuf release
boolean release();
From source file:divconq.ctp.stream.FunnelStream.java
License:Open Source License
@Override public void close() { ByteBuf curr = this.currbuf; if (curr != null) curr.release(); this.currbuf = null; this.current = null; super.close(); }
From source file:divconq.ctp.stream.GzipStream.java
License:Open Source License
@Override public ReturnOption handle(FileDescriptor file, ByteBuf data) { if (file == FileDescriptor.FINAL) return this.downstream.handle(file, data); // we don't know what to do with a folder at this stage - gzip is for file content only // folder scanning is upstream in the FileSourceStream and partners if (file.isFolder()) return ReturnOption.CONTINUE; // init if not set for this round of processing if (this.deflater == null) { this.deflater = new Deflater(this.compressionLevel, true); this.crc.reset(); this.writeHeader = true; }// w w w. j a v a 2 s . com ByteBuf in = data; ByteBuf out = null; if (in != null) { byte[] inAry = in.array(); // always allow for a header (10) plus footer (8) plus extra (12) // in addition to content int sizeEstimate = (int) Math.ceil(in.readableBytes() * 1.001) + 30; out = Hub.instance.getBufferAllocator().heapBuffer(sizeEstimate); if (this.writeHeader) { this.writeHeader = false; out.writeBytes(gzipHeader); } this.crc.update(inAry, in.arrayOffset(), in.writerIndex()); this.deflater.setInput(inAry, in.arrayOffset(), in.writerIndex()); while (!this.deflater.needsInput()) deflate(out); } else out = Hub.instance.getBufferAllocator().heapBuffer(30); FileDescriptor blk = new FileDescriptor(); if (StringUtil.isEmpty(this.lastpath)) { if (StringUtil.isNotEmpty(this.nameHint)) this.lastpath = "/" + this.nameHint; else if (file.getPath() != null) this.lastpath = "/" + GzipUtils.getCompressedFilename(file.path().getFileName()); else this.lastpath = "/" + FileUtil.randomFilename("gz"); } blk.setPath(this.lastpath); file.setModTime(System.currentTimeMillis()); if (file.isEof()) { this.deflater.finish(); while (!this.deflater.finished()) deflate(out); int crcValue = (int) this.crc.getValue(); out.writeByte(crcValue); out.writeByte(crcValue >>> 8); out.writeByte(crcValue >>> 16); out.writeByte(crcValue >>> 24); int uncBytes = this.deflater.getTotalIn(); out.writeByte(uncBytes); out.writeByte(uncBytes >>> 8); out.writeByte(uncBytes >>> 16); out.writeByte(uncBytes >>> 24); this.deflater.end(); this.deflater = null; // cause a reset for next time we use stream blk.setEof(true); } if (in != null) in.release(); return this.downstream.handle(blk, out); }
From source file:divconq.ctp.stream.NullStream.java
License:Open Source License
@Override public ReturnOption handle(FileDescriptor file, ByteBuf data) { if (file == FileDescriptor.FINAL) { OperationContext.get().setAmountCompleted(100); OperationContext.get().info("Null got " + this.bytes + " bytes and " + this.files + " files/folders."); OperationContext.get().getTaskRun().complete(); return ReturnOption.DONE; }/* w ww.j av a 2 s .c om*/ if (file.isEof()) { this.files++; System.out.println("--- " + file.getPath() + " " + file.getSize() + " " + (file.isFolder() ? "FOLDER" : "FILE")); } if (data != null) { this.bytes += data.readableBytes(); data.release(); } return ReturnOption.CONTINUE; }
From source file:divconq.ctp.stream.PgpEncryptStream.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.needInit) { this.pgp.setFileName(file.path().getFileName()); try {//from w w w . j a v a 2 s . co m this.pgp.init(); } catch (Exception x) { OperationContext.get().getTaskRun().kill("PGP init failed: " + x); return ReturnOption.DONE; } this.initializeFileValues(file); this.needInit = false; } // inflate the payload into 1 or more outgoing buffers set in a queue ByteBuf in = data; if (in != null) { this.pgp.writeData(in); in.release(); if (OperationContext.get().getTaskRun().isKilled()) return ReturnOption.DONE; } // write all buffers in the queue ByteBuf buf = this.pgp.nextReadyBuffer(); while (buf != null) { ReturnOption ret = this.nextMessage(buf); if (ret != ReturnOption.CONTINUE) return ret; buf = this.pgp.nextReadyBuffer(); } if (file.isEof()) { try { this.pgp.close(); } catch (PGPException x) { OperationContext.get().getTaskRun().kill("PGP close failed: " + x); return ReturnOption.DONE; } // write all buffers in the queue buf = this.pgp.nextReadyBuffer(); while (buf != null) { ReturnOption ret = this.nextMessage(buf); if (ret != ReturnOption.CONTINUE) return ret; buf = this.pgp.nextReadyBuffer(); } ReturnOption ret = this.lastMessage(); if (ret != ReturnOption.CONTINUE) return ret; } // otherwise we need more data return ReturnOption.CONTINUE; }
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);//from www . ja va 2 s.c om 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.TarStream.java
License:Open Source License
@Override public ReturnOption handle(FileDescriptor file, ByteBuf data) { if (file == FileDescriptor.FINAL) { if (this.tstream == null) return this.downstream.handle(file, data); this.finalflag = true; }/*from w w w .java 2s. c o m*/ // I don't think tar cares about folder entries at this stage - tar is for file content only // folder scanning is upstream in the FileSourceStream and partners // TODO try with ending / to file name if (file.isFolder()) return ReturnOption.CONTINUE; // init if not set for this round of processing if (this.tstream == null) { this.bstream = new CyclingByteBufferOutputStream(); this.tstream = new TarArchiveOutputStream(this.bstream); this.tstream.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU); } ByteBuf in = data; ByteBuf out = null; // always allow for a header (512) and/or footer (1024) in addition to content int sizeEstimate = (in != null) ? in.readableBytes() + 2048 : 2048; out = Hub.instance.getBufferAllocator().heapBuffer(sizeEstimate); this.bstream.installBuffer(out); // TODO if there is no output available to send and not EOF then just request more, // no need to send a message that is empty and not EOF FileDescriptor blk = new FileDescriptor(); if (StringUtil.isNotEmpty(this.lastpath)) { blk.setPath(this.lastpath); } else { if (file.hasPath()) this.lastpath = "/" + (StringUtil.isNotEmpty(this.nameHint) ? this.nameHint : file.path().getFileName()) + ".tar"; else if (StringUtil.isNotEmpty(this.nameHint)) this.lastpath = "/" + this.nameHint + ".tar"; else this.lastpath = "/" + FileUtil.randomFilename() + ".tar"; blk.setPath(this.lastpath); } blk.setModTime(System.currentTimeMillis()); if (!this.archiveopenflag && !this.finalflag) { TarArchiveEntry tentry = new TarArchiveEntry(file.getPath().toString().substring(1), true); tentry.setSize(file.getSize()); tentry.setModTime(file.getModTime()); try { this.tstream.putArchiveEntry(tentry); } catch (IOException x) { if (in != null) in.release(); out.release(); OperationContext.get().getTaskRun().kill("Problem writing tar entry: " + x); return ReturnOption.DONE; } this.archiveopenflag = true; } if (in != null) try { this.tstream.write(in.array(), in.arrayOffset(), in.writerIndex()); } catch (IOException x) { in.release(); out.release(); OperationContext.get().getTaskRun().kill("Problem writing tar body: " + x); return ReturnOption.DONE; } if (file.isEof()) { try { this.tstream.closeArchiveEntry(); } catch (IOException x) { if (in != null) in.release(); out.release(); OperationContext.get().getTaskRun().kill("Problem closing tar entry: " + x); return ReturnOption.DONE; } this.archiveopenflag = false; } if (in != null) in.release(); if (file == FileDescriptor.FINAL) { blk.setEof(true); try { this.tstream.close(); } catch (IOException x) { //in.release(); out.release(); OperationContext.get().getTaskRun().kill("Problem closing tar stream: " + x); return ReturnOption.DONE; } this.tstream = null; this.bstream = null; } else this.bstream.uninstallBuffer(); // we are done with out forever, don't reference it System.out.println("tar sending: " + out.readableBytes()); ReturnOption v = this.downstream.handle(blk, out); if (!this.finalflag) return v; if (v == ReturnOption.CONTINUE) return this.downstream.handle(FileDescriptor.FINAL, null); return ReturnOption.DONE; }
From source file:divconq.ctp.stream.UngzipStream.java
License:Open Source License
@Override public void close() { Inflater inf = this.inflater; if (inf != null) inf.end();//from w ww . ja v a2 s .co m this.inflater = null; ByteBuf rem = this.remnant; if (rem != null) { rem.release(); this.remnant = null; } // not truly thread safe, consider for (ByteBuf bb : this.outlist) bb.release(); this.outlist.clear(); super.close(); }
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; 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();// www. j a va 2 s . c o m 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; }/* w ww . 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 void close() { this.currEntry = null; // not truly thread safe, consider for (ByteBuf bb : this.outbuf) bb.release(); this.outlist.clear(); this.outbuf.clear(); super.close(); }