List of usage examples for io.netty.buffer ByteBuf nioBuffers
public abstract ByteBuffer[] nioBuffers();
From source file:com.titilink.camel.rest.common.RestletServerCall.java
License:LGPL
@Override public InputStream getRequestEntityStream(long size) { ByteBuf buf = ((FullHttpRequest) request).content(); // notice:new byte ?intsizenetty????int byte[] allBytes = new byte[(int) size]; ByteBuffer[] allBufs = buf.nioBuffers(); ByteArrayInputStream result = null; int index = 0; if (null != allBufs) { for (ByteBuffer oneBuf : allBufs) { if (null != oneBuf) { System.arraycopy(oneBuf.array(), 0, allBytes, index, oneBuf.capacity()); index += oneBuf.capacity(); }//from w w w.j a v a 2 s.c o m } } result = new ByteArrayInputStream(allBytes); return result; }
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();/* w ww. ja v a2 s .c o m*/ OperationContext.get().getTaskRun().kill("Folder cannot be stored into a file"); return ReturnOption.DONE; } 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 {//w w w .ja v a 2 s .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:io.advantageous.conekt.file.impl.AsyncFileImpl.java
License:Open Source License
private synchronized AsyncFile doWrite(Buffer buffer, long position, Handler<AsyncResult<Void>> handler) { Objects.requireNonNull(buffer, "buffer"); Arguments.require(position >= 0, "position must be >= 0"); check();// w w w . j a v a 2s . com Handler<AsyncResult<Void>> wrapped = ar -> { if (ar.succeeded()) { checkContext(); checkDrained(); if (writesOutstanding == 0 && closedDeferred != null) { closedDeferred.run(); } if (handler != null) { handler.handle(ar); } } else { if (handler != null) { handler.handle(ar); } else { handleException(ar.cause()); } } }; ByteBuf buf = buffer.getByteBuf(); if (buf.nioBufferCount() > 1) { doWrite(buf.nioBuffers(), position, wrapped); } else { ByteBuffer bb = buf.nioBuffer(); doWrite(bb, position, bb.limit(), wrapped); } return this; }
From source file:io.gatling.http.client.body.multipart.impl.PartImpl.java
License:Apache License
long transferTo(ByteBuf source, WritableByteChannel target, PartImplState sourceFullyWrittenState) throws IOException { int transferred = 0; if (target instanceof GatheringByteChannel) { transferred = source.readBytes((GatheringByteChannel) target, source.readableBytes()); } else {/* w ww . java2 s .c om*/ for (ByteBuffer byteBuffer : source.nioBuffers()) { int len = byteBuffer.remaining(); int written = target.write(byteBuffer); transferred += written; if (written != len) { // couldn't write full buffer, exit loop break; } } // assume this is a basic single ByteBuf source.readerIndex(source.readerIndex() + transferred); } if (source.isReadable()) { slowTarget = true; } else { state = sourceFullyWrittenState; } return transferred; }
From source file:io.gatling.netty.util.ahc.Utf8ByteBufCharsetDecoder.java
License:Apache License
private void decodeHeap0(ByteBuf buf) { int length = buf.readableBytes(); ensureCapacity(length);/* ww w .j av a 2 s . c o m*/ if (buf.nioBufferCount() == 1) { decodeSingleNioBuffer(buf.internalNioBuffer(buf.readerIndex(), length).duplicate()); } else { decode(buf.nioBuffers()); } charBuffer.flip(); }
From source file:io.gatling.netty.util.ahc.Utf8ByteBufCharsetDecoder.java
License:Apache License
private void decodeHeap0(ByteBuf[] bufs) { ByteBuffer[] nioBuffers = new ByteBuffer[totalNioBuffers]; int i = 0;/*ww w. j a v a 2s . com*/ for (ByteBuf buf : bufs) { for (ByteBuffer nioBuffer : buf.nioBuffers()) { nioBuffers[i++] = nioBuffer; } } ensureCapacity(totalSize); decode(nioBuffers); charBuffer.flip(); }
From source file:io.grpc.alts.internal.BufUnwrapper.java
License:Apache License
/** * Optimized accessor for obtaining the underlying NIO buffers for a Netty {@link ByteBuf}. Based * on code from Netty's {@code SslHandler}. This method returns NIO buffers that span the readable * region of the {@link ByteBuf}.//w ww . j ava 2s . c om */ private static ByteBuffer[] nioBuffers(ByteBuf buf, ByteBuffer[] singleBuffer) { // As CompositeByteBuf.nioBufferCount() can be expensive (as it needs to check all composed // ByteBuf to calculate the count) we will just assume a CompositeByteBuf contains more than 1 // ByteBuf. The worst that can happen is that we allocate an extra ByteBuffer[] in // CompositeByteBuf.nioBuffers() which is better than walking the composed ByteBuf in most // cases. if (!(buf instanceof CompositeByteBuf) && buf.nioBufferCount() == 1) { // We know its only backed by 1 ByteBuffer so use internalNioBuffer to keep object // allocation to a minimum. singleBuffer[0] = buf.internalNioBuffer(buf.readerIndex(), buf.readableBytes()); return singleBuffer; } return buf.nioBuffers(); }
From source file:io.jsync.file.impl.DefaultAsyncFile.java
License:Open Source License
@Override public AsyncFile write(Buffer buffer, long position, final Handler<AsyncResult<Void>> handler) { check();// ww w . j av a 2 s. c om final ByteBuf buf = buffer.getByteBuf(); if (buf.nioBufferCount() > 1) { final Iterator<ByteBuffer> buffers = Arrays.asList(buf.nioBuffers()).iterator(); doWrite(buffers, position, handler); } else { ByteBuffer bb = buf.nioBuffer(); doWrite(bb, position, bb.limit(), handler); } return this; }
From source file:io.jsync.file.impl.DefaultAsyncFile.java
License:Open Source License
@Override public AsyncFile write(Buffer buffer) { check();//from ww w .java 2 s . c om final int length = buffer.length(); Handler<AsyncResult<Void>> handler = new Handler<AsyncResult<Void>>() { public void handle(AsyncResult<Void> deferred) { if (deferred.succeeded()) { checkContext(); checkDrained(); if (writesOutstanding == 0 && closedDeferred != null) { closedDeferred.run(); } } else { handleException(deferred.cause()); } } }; ByteBuf buf = buffer.getByteBuf(); if (buf.nioBufferCount() > 1) { final Iterator<ByteBuffer> buffers = Arrays.asList(buf.nioBuffers()).iterator(); doWrite(buffers, writePos, handler); } else { ByteBuffer bb = buf.nioBuffer(); doWrite(bb, writePos, bb.limit(), handler); } writePos += length; return this; }