List of usage examples for io.netty.buffer ByteBuf release
boolean release();
From source file:divconq.api.internal.UploadPutHandler.java
License:Open Source License
public void start(final HyperSession parent, ScatteringByteChannel src, String chanid, Map<String, Cookie> cookies, long size, long offset, final OperationCallback callback) { this.src = src; this.cookies = cookies; this.callback = callback; this.dest = this.allocateChannel(parent, callback); if (this.callback.hasErrors()) { callback.complete();// ww w.j a v a2 s. c o m return; } // send a request to get things going HttpRequest req = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "/upload/" + chanid + "/Final"); req.headers().set(Names.HOST, parent.getInfo().getHost()); req.headers().set(Names.USER_AGENT, "DivConq HyperAPI Client 1.0"); req.headers().set(Names.CONNECTION, HttpHeaders.Values.CLOSE); req.headers().set(Names.COOKIE, ClientCookieEncoder.encode(this.cookies.values())); req.headers().set(HttpHeaders.Names.CONTENT_LENGTH, size - offset); // send request headers - must flush here in case CL = 0 this.dest.writeAndFlush(req); // now start sending the file long sent = offset; callback.getContext().setAmountCompleted((int) (sent * 100 / size)); ByteBuf bb = null; try { bb = Hub.instance.getBufferAllocator().directBuffer(64 * 1024); // TODO review if direct is best here long toskip = offset; if (src instanceof SeekableByteChannel) { ((SeekableByteChannel) src).position(toskip); } else { while (toskip > 0) { int skip = (int) Math.min(bb.capacity(), toskip); toskip -= bb.writeBytes(src, skip); bb.clear(); } } // now start writing the upload int amt = bb.writeBytes(src, bb.capacity()); while (amt != -1) { bb.retain(); // this ups ref cnt to 2 - we plan to reuse the buffer this.dest.writeAndFlush(bb).sync(); sent += amt; if (size > 0) callback.getContext().setAmountCompleted((int) (sent * 100 / size)); // by the time we get here, that buffer has been used up and we can use it for the next buffer if (bb.refCnt() != 1) throw new IOException("Buffer reference count is not correct"); // stop writing if canceled if (!this.dest.isOpen()) { this.finish(); // might already be finished but to be sure (this is helpful when api.abortStream is called) break; } bb.clear(); amt = bb.writeBytes(src, bb.capacity()); } // we are now done with it bb.release(); } catch (Exception x) { try { if (bb != null) bb.release(); } catch (Exception x2) { } callback.error(1, "Local read error: " + x); this.finish(); } }
From source file:divconq.api.LocalSession.java
License:Open Source License
@Override public void sendStream(ScatteringByteChannel in, long size, long offset, final String channelid, final OperationCallback callback) { final DataStreamChannel chan = this.session.getChannel(channelid); if (chan == null) { callback.error(1, "Missing channel"); callback.complete();//ww w .j a v a 2 s. c o m return; } chan.setDriver(new IStreamDriver() { @Override public void cancel() { callback.error(1, "Transfer canceled"); chan.complete(); callback.complete(); } @Override public void message(StreamMessage msg) { if (msg.isFinal()) { System.out.println("Final on channel: " + channelid); chan.complete(); callback.complete(); } } @Override public void nextChunk() { // won't chunk so won't happen here } }); long sent = offset; int seq = 0; if (size > 0) { callback.getContext().setAmountCompleted((int) (sent * 100 / size)); chan.getContext().setAmountCompleted((int) (sent * 100 / size)); // keep the channel active so it does not timeout } try { ByteBuf bb = Hub.instance.getBufferAllocator().directBuffer(64 * 1024); long toskip = offset; if (in instanceof SeekableByteChannel) { ((SeekableByteChannel) in).position(toskip); } else { while (toskip > 0) { int skip = (int) Math.min(bb.capacity(), toskip); toskip -= bb.writeBytes(in, skip); bb.clear(); } } chan.touch(); // now start writing the upload int amt = bb.writeBytes(in, bb.capacity()); while (amt != -1) { bb.retain(); // this ups ref cnt to 2 - we plan to reuse the buffer StreamMessage b = new StreamMessage("Block", bb); b.setField("Sequence", seq); OperationResult sr = chan.send(b); if (sr.hasErrors()) { chan.close(); break; } seq++; sent += amt; if (size > 0) { callback.getContext().setAmountCompleted((int) (sent * 100 / size)); chan.getContext().setAmountCompleted((int) (sent * 100 / size)); // keep the channel active so it does not timeout } callback.touch(); chan.touch(); // by the time we get here, that buffer has been used up and we can use it for the next buffer if (bb.refCnt() != 1) throw new IOException("Buffer reference count is not correct"); // stop writing if canceled if (chan.isClosed()) break; bb.clear(); amt = bb.writeBytes(in, bb.capacity()); } // we are now done with it bb.release(); // final only if not canceled if (!chan.isClosed()) chan.send(MessageUtil.streamFinal()); } catch (IOException x) { callback.error(1, "Local read error: " + x); chan.send(MessageUtil.streamError(1, "Source read error: " + x)); chan.close(); callback.complete(); } finally { try { in.close(); } catch (IOException x) { } } }
From source file:divconq.bus.net.ServerHandler.java
License:Open Source License
public void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { // Generate an error page if response getStatus code is not OK (200). if (res.getStatus().code() != 200) { ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8); res.content().writeBytes(buf);//from w w w . j a v a 2 s .c om buf.release(); HttpHeaders.setContentLength(res, res.content().readableBytes()); } // Send the response and close the connection if necessary. ChannelFuture f = ctx.channel().writeAndFlush(res); if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) f.addListener(ChannelFutureListener.CLOSE); }
From source file:divconq.bus.net.StreamEncoder.java
License:Open Source License
@Override protected void encode(ChannelHandlerContext ctx, StreamMessage msg, ByteBuf out) throws Exception { msg.toSerial(out);//from w w w . j a v a2 s .c o m if (msg.hasData()) { ByteBuf bb = msg.getData(); try { bb.readerIndex(0); out.writeInt(bb.readableBytes()); out.writeBytes(bb); } finally { bb.release(); // buffer no longer needed } } else out.writeInt(0); }
From source file:divconq.ctp.net.CtpHandler.java
License:Open Source License
@Override public void read() { this.readLock.lock(); if (serverMode) System.out.println("Start Server requested read!"); this.readRequested = false; // meaning read is covered until further notice try {//ww w .j ava 2s .com ByteBuf rem = this.remnant; //CtpHandler.this.debug("checking remnant: " + rem); // if there are any unread bytes from the last read, check to see if we can collect a command if (rem != null) { //CtpHandler.this.debug("checking bytes: " + rem.readableBytes()); //System.out.println("Remnant ref cnt 1: " + rem.refCnt() + " for server: " + CtpHandler.this.serverMode); boolean ready = false; try { ready = this.adapter.decode(rem); } catch (Exception x) { // TODO error and close!! System.out.println("Error decoding message: " + x); return; } //System.out.println("Remnant ref cnt 2: " + rem.refCnt()); // if there are any unread bytes here we need to store them and combine with the next read if (!rem.isReadable()) { this.remnant = null; rem.release(); } //System.out.println("Remnant ref cnt 3: " + rem.refCnt()); if (!ready) { this.readRequested = true; this.chan.read(); } else this.adapter.handleCommand(); } else { this.readRequested = true; this.chan.read(); } if (serverMode) System.out.println("End Server requested read!"); } finally { this.readLock.unlock(); } }
From source file:divconq.ctp.net.CtpHandler.java
License:Open Source License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { CtpHandler.this.readLock.lock(); try {/*from w w w. j av a2 s. c om*/ ByteBuf buf = (ByteBuf) msg; if (serverMode) System.out.println("Server got network read 1: " + buf.readableBytes()); ByteBuf rem = this.remnant; // if there are any unread bytes from the last read, combine with this read this.remnant = buf; // TODO there are maybe better ways to do this - a queue of buffers? if (rem != null) { if (rem.isReadable()) { this.remnant = Hub.instance.getBufferAllocator() .heapBuffer(rem.readableBytes() + buf.readableBytes()); this.remnant.writeBytes(rem); this.remnant.writeBytes(buf); buf.release(); } rem.release(); } if (serverMode) System.out.println("Server got network read 2: " + this.remnant.readableBytes()); if (!this.readRequested) return; if (this.remnant.readableBytes() > 256 * 1024) System.out.println( "CTP Buffer getting too large - possible issue!!!! " + this.remnant.readableBytes()); // read with the updated buffer this.read(); } finally { CtpHandler.this.readLock.unlock(); } }
From source file:divconq.ctp.stream.FileDestStream.java
License:Open Source License
public ReturnOption handleLocalFile(FileDescriptor file, ByteBuf data) { if (file.isFolder()) { if (data != null) data.release(); OperationContext.get().getTaskRun().kill("Folder cannot be stored into a file"); return ReturnOption.DONE; }//from w w w . ja va2s. c o m if (data != null) { if (this.out == null) { try { Path dpath = this.file.localPath(); Files.createDirectories(dpath.getParent()); this.out = FileChannel.open(dpath, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.SYNC); } catch (IOException x) { if (data != null) data.release(); OperationContext.get().getTaskRun().kill("Problem opening destination file: " + x); return ReturnOption.DONE; } } for (ByteBuffer buff : data.nioBuffers()) { try { this.out.write(buff); } catch (IOException x) { data.release(); OperationContext.get().getTaskRun().kill("Problem writing destination file: " + x); return ReturnOption.DONE; } } data.release(); } if (file.isEof()) { try { if (this.out != null) { this.out.close(); this.out = null; } this.file.refreshProps(); } catch (IOException x) { OperationContext.get().getTaskRun().kill("Problem closing destination file: " + x); return ReturnOption.DONE; } } return ReturnOption.CONTINUE; }
From source file:divconq.ctp.stream.FileDestStream.java
License:Open Source License
public ReturnOption handleLocalFolder(FileDescriptor file, ByteBuf data) { Path folder = this.file.localPath(); if (Files.notExists(folder)) try {/*ww w . j av a 2s . c o m*/ Files.createDirectories(folder); } catch (IOException x) { if (data != null) data.release(); OperationContext.get().getTaskRun().kill("Problem making destination top folder: " + x); return ReturnOption.DONE; } String fpath = (this.userelpath) ? this.relpath + file.getPath() : "/" + file.path().getFileName(); if (file.isFolder()) { try { Files.createDirectories(folder.resolve(fpath.substring(1))); } catch (IOException x) { if (data != null) data.release(); OperationContext.get().getTaskRun().kill("Problem making destination folder: " + x); return ReturnOption.DONE; } return ReturnOption.CONTINUE; } if (this.out == null) try { Path dpath = folder.resolve(fpath.substring(1)); Files.createDirectories(dpath.getParent()); this.out = FileChannel.open(dpath, StandardOpenOption.CREATE, StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.SYNC); } catch (IOException x) { if (data != null) data.release(); OperationContext.get().getTaskRun().kill("Problem opening destination file: " + x); return ReturnOption.DONE; } if (data != null) { for (ByteBuffer buff : data.nioBuffers()) { try { this.out.write(buff); } catch (IOException x) { data.release(); OperationContext.get().getTaskRun().kill("Problem writing destination file: " + x); return ReturnOption.DONE; } } data.release(); } if (file.isEof()) { try { this.out.close(); this.out = null; this.file.refreshProps(); } catch (IOException x) { OperationContext.get().getTaskRun().kill("Problem closing destination file: " + x); return ReturnOption.DONE; } } return ReturnOption.CONTINUE; }
From source file:divconq.ctp.stream.FileSourceStream.java
License:Open Source License
public void readLocalFile() { FileSystemFile fs = (FileSystemFile) this.current; if (this.in == null) { this.insize = fs.getSize(); // As a source we are responsible for progress tracking OperationContext.get().setAmountCompleted(0); try {//from ww w .jav a 2 s.com this.in = FileChannel.open(fs.localPath(), StandardOpenOption.READ); } catch (IOException x) { OperationContext.get().getTaskRun().kill("Unable to read source file " + x); return; } } while (true) { // TODO sizing? ByteBuf data = Hub.instance.getBufferAllocator().heapBuffer(32768); ByteBuffer buffer = ByteBuffer.wrap(data.array(), data.arrayOffset(), data.capacity()); int pos = -1; try { pos = (int) this.in.read(buffer); } catch (IOException x1) { OperationContext.get().getTaskRun().kill("Problem reading source file: " + x1); data.release(); return; } FileDescriptor fref = FileDescriptor.fromFileStore(this.current); fref.setPath(this.current.path().subpath(this.source.path())); System.out.println("writing: " + fref.getPath() + " from: " + this.inprog); if (pos == -1) { try { this.in.close(); } catch (IOException x) { OperationContext.get().getTaskRun().kill("Problem closing source file: " + x); data.release(); return; } OperationContext.get().setAmountCompleted(100); fref.setEof(true); this.current = null; this.in = null; this.insize = 0; this.inprog = 0; } else { this.inprog += pos; data.writerIndex(pos); OperationContext.get().setAmountCompleted((int) (this.inprog * 100 / this.insize)); } if (this.downstream.handle(fref, data) != ReturnOption.CONTINUE) break; if (this.current == null) { // we need the next file OperationContext.get().getTaskRun().resume(); // wait on the implied request break; } } }
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 www. j a v a 2s .com*/ 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); }