List of usage examples for java.nio ByteBuffer hasRemaining
public final boolean hasRemaining()
From source file:org.mcisb.util.math.MathUtils.java
/** * //from www . ja v a 2 s . co m * @param encoded * @param bigEndian * @param doublePrecision * @return double[] */ public static double[] decode(final byte[] encoded, final boolean bigEndian, final boolean doublePrecision) { final byte[] bytes = Base64.decode(encoded); ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); byteBuffer = byteBuffer.order(bigEndian ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN); final int limit = byteBuffer.limit(); double[] decoded = new double[((doublePrecision) ? limit / DOUBLE_LENGTH : limit / FLOAT_LENGTH)]; int i = 0; while (byteBuffer.hasRemaining()) { if (doublePrecision) { decoded[i++] = byteBuffer.getDouble(); } else { decoded[i++] = byteBuffer.getFloat(); } } return decoded; }
From source file:com.mgmtp.perfload.perfalyzer.util.IoUtilities.java
/** * Copies the content from one channel to another. * * @param srcChannel// www .j a va 2 s.c o m * the source channel to copy from * @param destChannel * the destination channel to copy to */ public static void copy(final ReadableByteChannel srcChannel, final WritableByteChannel destChannel) throws IOException { final ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER_SIZE); while (srcChannel.read(buffer) != -1) { // flip the buffer so it can be written to the destination channel buffer.flip(); // write to the destination channel destChannel.write(buffer); // If partial transfer, shift remainder down so it does not get lost // If buffer is empty, this is the same as calling clear() buffer.compact(); } // EOF will leave buffer in fill state buffer.flip(); // make sure the buffer is fully drained while (buffer.hasRemaining()) { destChannel.write(buffer); } }
From source file:schemacrawler.test.utility.TestUtility.java
private static 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 ww .j ava 2s . 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.geoserver.rest.util.IOUtils.java
/** * Copies the content of the source channel onto the destination channel. * /*www. j av a 2 s. c o m*/ * @param bufferSize size of the temp buffer to use for this copy. * @param source the source {@link ReadableByteChannel}. * @param destination the destination {@link WritableByteChannel};. * @throws IOException in case something bad happens. */ public static void copyChannel(int bufferSize, ReadableByteChannel source, WritableByteChannel destination) throws IOException { inputNotNull(source, destination); if (!source.isOpen() || !destination.isOpen()) throw new IllegalStateException("Source and destination channels must be open."); final java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocateDirect(bufferSize); while (source.read(buffer) != -1) { //prepare the buffer for draining buffer.flip(); //write to destination while (buffer.hasRemaining()) destination.write(buffer); //clear buffer.clear(); } }
From source file:org.mhisoft.common.util.FileUtils.java
/** * @param source/* w w w . j a v a2 s . c om*/ * @param target * @throws IOException */ public static void copyFile(final File source, final File target) throws IOException { FileChannel in = null; FileChannel out = null; // long totalFileSize = 0; try { in = new FileInputStream(source).getChannel(); out = new FileOutputStream(target).getChannel(); //totalFileSize = in.size(); ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER); int readSize = in.read(buffer); long totalRead = 0; //int progress = 0; //long startTime, endTime ; while (readSize != -1) { //startTime = System.currentTimeMillis(); totalRead = totalRead + readSize; //progress = (int) (totalRead * 100 / totalFileSize); buffer.flip(); while (buffer.hasRemaining()) { out.write(buffer); //System.out.printf("."); //showPercent(rdProUI, totalSize/size ); } buffer.clear(); readSize = in.read(buffer); //endTime = System.currentTimeMillis(); } } finally { close(in); close(out); } }
From source file:it.unimi.di.big.mg4j.index.DiskBasedIndex.java
/** Commodity method for loading from a channel a big list of binary longs with specified endianness into a {@linkplain LongBigArrays long big array}. * /*w w w. ja v a2s . c o m*/ * @param channel the channel. * @param byteOrder the endianness of the longs. * @return a big list of longs containing the longs returned by <code>channel</code>. */ public static LongBigArrayBigList loadLongBigList(final ReadableByteChannel channel, final long length, final ByteOrder byteOrder) throws IOException { final ByteBuffer byteBuffer = ByteBuffer.allocateDirect(BUFFER_SIZE).order(byteOrder); LongBigArrayBigList list = new LongBigArrayBigList(length); while (channel.read(byteBuffer) > 0) { byteBuffer.flip(); while (byteBuffer.hasRemaining()) list.add(byteBuffer.getLong()); byteBuffer.clear(); } return list; }
From source file:org.apache.nifi.processor.util.put.sender.DatagramChannelSender.java
@Override protected void write(byte[] data) throws IOException { ByteBuffer buffer = ByteBuffer.wrap(data); while (buffer.hasRemaining()) { channel.write(buffer);//from www . j ava 2 s .c o m } }
From source file:org.wso2.carbon.inbound.endpoint.protocol.http2.http2Encoder.java
@Override public int write(ByteBuffer src) throws IOException { while (src.hasRemaining()) { byte[] b; b = new byte[src.remaining()]; src.get(b);/*from w ww . j av a 2 s . co m*/ if (src.hasRemaining()) encoder.writeData(chContext, streamId, Unpooled.wrappedBuffer(b), 0, false, promise); else { encoder.writeData(chContext, streamId, Unpooled.wrappedBuffer(b), 0, true, promise); isComplete = true; } } return src.position(); }
From source file:MainClass.java
public void run() { ByteBuffer sizeb = ByteBuffer.allocate(4); try {//w w w . j av a2s . co m while (sizeb.hasRemaining()) in.read(sizeb); sizeb.flip(); int howMany = sizeb.getInt(); sizeb.clear(); for (int i = 0; i < howMany; i++) { while (sizeb.hasRemaining()) in.read(sizeb); sizeb.flip(); int length = sizeb.getInt(); sizeb.clear(); ByteBuffer data = ByteBuffer.allocate(length); while (data.hasRemaining()) in.read(data); BigInteger result = new BigInteger(data.array()); System.out.println(result); } } catch (IOException ex) { System.err.println(ex); } finally { try { in.close(); } catch (Exception ex) { // We tried } } }
From source file:org.apache.tika.parser.html.charsetdetector.charsets.XUserDefinedCharset.java
public CharsetDecoder newDecoder() { return new CharsetDecoder(this, 1, 1) { @Override/*w w w .ja v a2 s. c o m*/ protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) { while (true) { if (!in.hasRemaining()) return CoderResult.UNDERFLOW; if (!out.hasRemaining()) return CoderResult.OVERFLOW; byte b = in.get(); out.append((char) ((b >= 0) ? b : 0xF700 + (b & 0xFF))); } } }; }