List of usage examples for java.nio ByteBuffer compact
public abstract ByteBuffer compact();
From source file:org.apache.axis2.transport.nhttp.ServerHandler.java
/** * Process ready input by writing it into the Pipe * @param conn the connection being processed * @param decoder the content decoder in use */// www . j a va2 s . co m public void inputReady(final NHttpServerConnection conn, final ContentDecoder decoder) { HttpContext context = conn.getContext(); Pipe.SinkChannel sink = (Pipe.SinkChannel) context.getAttribute(REQUEST_SINK_CHANNEL); ByteBuffer inbuf = (ByteBuffer) context.getAttribute(REQUEST_BUFFER); try { while (decoder.read(inbuf) > 0) { inbuf.flip(); sink.write(inbuf); inbuf.compact(); } if (decoder.isCompleted()) { sink.close(); } } catch (IOException e) { handleException("I/O Error : " + e.getMessage(), e, conn); } }
From source file:com.doplgangr.secrecy.FileSystem.File.java
public java.io.File readFile(CryptStateListener listener) { decrypting = true;/* www .j av a2 s.c o m*/ InputStream is = null; OutputStream out = null; java.io.File outputFile = null; try { outputFile = java.io.File.createTempFile("tmp" + name, "." + FileType, storage.getTempFolder()); outputFile.mkdirs(); outputFile.createNewFile(); AES_Encryptor enc = new AES_Encryptor(key); is = new CipherInputStream(new FileInputStream(file), enc.decryptstream()); listener.setMax((int) file.length()); ReadableByteChannel inChannel = Channels.newChannel(is); FileChannel outChannel = new FileOutputStream(outputFile).getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate(Config.bufferSize); while (inChannel.read(byteBuffer) >= 0 || byteBuffer.position() > 0) { byteBuffer.flip(); outChannel.write(byteBuffer); byteBuffer.compact(); listener.updateProgress((int) outChannel.size()); } inChannel.close(); outChannel.close(); Util.log(outputFile.getName(), outputFile.length()); return outputFile; } catch (FileNotFoundException e) { listener.onFailed(2); Util.log("Encrypted File is missing", e.getMessage()); } catch (IOException e) { Util.log("IO Exception while decrypting", e.getMessage()); if (e.getMessage().contains("pad block corrupted")) listener.onFailed(1); else e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } finally { listener.Finished(); decrypting = false; try { if (is != null) { is.close(); } if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } // An error occured. Too Bad if (outputFile != null) storage.purgeFile(outputFile); return null; }
From source file:org.apache.axis2.transport.nhttp.ClientHandler.java
/** * Process ready input (i.e. response from remote server) * @param conn connection being processed * @param decoder the content decoder in use *//* w ww . ja va 2s . c o m*/ public void inputReady(final NHttpClientConnection conn, final ContentDecoder decoder) { HttpContext context = conn.getContext(); HttpResponse response = conn.getHttpResponse(); Pipe.SinkChannel sink = (Pipe.SinkChannel) context.getAttribute(RESPONSE_SINK_CHANNEL); ByteBuffer inbuf = (ByteBuffer) context.getAttribute(REQUEST_BUFFER); try { while (decoder.read(inbuf) > 0) { inbuf.flip(); sink.write(inbuf); inbuf.compact(); } if (decoder.isCompleted()) { sink.close(); if (!connStrategy.keepAlive(response, context)) { conn.close(); } else { ConnectionPool.release(conn); } } } catch (IOException e) { handleException("I/O Error : " + e.getMessage(), e, conn); } }
From source file:org.apache.axis2.transport.nhttp.ServerHandler.java
/** * Process ready output by writing into the channel * @param conn the connection being processed * @param encoder the content encoder in use *//*from w w w .j a va 2 s. co m*/ public void outputReady(final NHttpServerConnection conn, final ContentEncoder encoder) { HttpContext context = conn.getContext(); HttpResponse response = conn.getHttpResponse(); Pipe.SourceChannel source = (Pipe.SourceChannel) context.getAttribute(RESPONSE_SOURCE_CHANNEL); ByteBuffer outbuf = (ByteBuffer) context.getAttribute(RESPONSE_BUFFER); try { int bytesRead = source.read(outbuf); if (bytesRead == -1) { encoder.complete(); } else { outbuf.flip(); encoder.write(outbuf); outbuf.compact(); } if (encoder.isCompleted()) { source.close(); if (!connStrategy.keepAlive(response, context)) { conn.close(); } } } catch (IOException e) { handleException("I/O Error : " + e.getMessage(), e, conn); } }
From source file:org.alfresco.cacheserver.dropwizard.resources.CacheServerResource.java
private void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (src.read(buffer) != -1) { // prepare the buffer to be drained buffer.flip();//from w w w .j a va2 s .c om // write to the channel, may block dest.write(buffer); // If partial transfer, shift remainder down // If buffer is empty, same as doing clear() buffer.compact(); } // EOF will leave buffer in fill state buffer.flip(); // make sure the buffer is fully drained. while (buffer.hasRemaining()) { dest.write(buffer); } }
From source file:org.apache.axis2.transport.nhttp.ClientHandler.java
/** * Process ready output (i.e. write request to remote server) * @param conn the connection being processed * @param encoder the encoder in use/*from w w w .j a va2s. c o m*/ */ public void outputReady(final NHttpClientConnection conn, final ContentEncoder encoder) { HttpContext context = conn.getContext(); HttpResponse response = conn.getHttpResponse(); Pipe.SourceChannel source = (Pipe.SourceChannel) context.getAttribute(REQUEST_SOURCE_CHANNEL); ByteBuffer outbuf = (ByteBuffer) context.getAttribute(RESPONSE_BUFFER); try { int bytesRead = source.read(outbuf); if (bytesRead == -1) { encoder.complete(); } else { outbuf.flip(); encoder.write(outbuf); outbuf.compact(); } if (encoder.isCompleted()) { source.close(); } } catch (IOException e) { handleException("I/O Error : " + e.getMessage(), e, conn); } }
From source file:com.github.jinahya.verbose.codec.BinaryCodecTest.java
protected final void encodeDecode(final ReadableByteChannel expectedChannel) throws IOException { if (expectedChannel == null) { throw new NullPointerException("null expectedChannel"); }/*from ww w . j a va2 s. c o m*/ final Path encodedPath = Files.createTempFile("test", null); getRuntime().addShutdownHook(new Thread(() -> { try { Files.delete(encodedPath); } catch (final IOException ioe) { ioe.printStackTrace(System.err); } })); final WritableByteChannel encodedChannel = FileChannel.open(encodedPath, StandardOpenOption.WRITE); final ByteBuffer decodedBuffer = ByteBuffer.allocate(128); final ByteBuffer encodedBuffer = ByteBuffer.allocate(decodedBuffer.capacity() << 1); while (expectedChannel.read(decodedBuffer) != -1) { decodedBuffer.flip(); // limit -> position; position -> zero encoder.encode(decodedBuffer, encodedBuffer); encodedBuffer.flip(); encodedChannel.write(encodedBuffer); encodedBuffer.compact(); // position -> n + 1; limit -> capacity decodedBuffer.compact(); } decodedBuffer.flip(); while (decodedBuffer.hasRemaining()) { encoder.encode(decodedBuffer, encodedBuffer); encodedBuffer.flip(); encodedChannel.write(encodedBuffer); encodedBuffer.compact(); } encodedBuffer.flip(); while (encodedBuffer.hasRemaining()) { encodedChannel.write(encodedBuffer); } }
From source file:com.bennavetta.appsite.file.ResourceService.java
public Resource create(String path, MediaType mime, ReadableByteChannel src) throws IOException { String normalized = PathUtils.normalize(path); log.debug("Creating resource {}", normalized); AppEngineFile file = fs.createNewBlobFile(mime.toString(), normalized); FileWriteChannel channel = fs.openWriteChannel(file, true); MessageDigest digest = null;//from ww w.jav a 2s . c o m try { digest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("MD5 not available", e); } ByteBuffer buf = ByteBuffer.allocateDirect(bufferSize.get()); while (src.read(buf) != -1 || buf.position() != 0) { buf.flip(); int read = channel.write(buf); if (read != 0) { int origPos = buf.position(); int origLimit = buf.limit(); buf.position(origPos - read); buf.limit(origPos); digest.update(buf); buf.limit(origLimit); buf.position(origPos); } buf.compact(); } channel.closeFinally(); Resource resource = new Resource(normalized, fs.getBlobKey(file).getKeyString(), digest.digest(), mime); resource.save(); return resource; }