List of usage examples for java.nio ByteBuffer remaining
public final int remaining()
From source file:com.offbynull.portmapper.common.ByteBufferUtils.java
/** * Copy the remaining content of a {@link ByteBuffer} in to a new non-direct {@link ByteBuffer}. * @param src buffer to copy/*from ww w . ja v a 2 s .c o m*/ * @param incrementSrc of {@code true} increments {@code src}'s position * @param incrementDst of {@code true} increments {@code dst}'s position * @return new buffer with the remaining content in {@code src} * @throws NullPointerException if any arguments are {@code null} */ public static ByteBuffer copyContents(ByteBuffer src, boolean incrementSrc, boolean incrementDst) { Validate.notNull(src); if (!incrementSrc) { src.mark(); } ByteBuffer dst = ByteBuffer.allocate(src.remaining()); dst.put(src); if (!incrementSrc) { src.reset(); } if (!incrementDst) { dst.flip(); } return dst; }
From source file:com.mcxiaoke.next.http.entity.mime.AbstractMultipartForm.java
private static ByteArrayBuffer encode(final Charset charset, final String string) { final ByteBuffer encoded = charset.encode(CharBuffer.wrap(string)); final ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining()); bab.append(encoded.array(), encoded.position(), encoded.remaining()); return bab;/*from w w w.j ava2 s . c o m*/ }
From source file:org.apache.nutch.util.StringUtil.java
/** * Get a text representation of a ByteBuffer as hexadecimal String, where each * pair of hexadecimal digits corresponds to consecutive bytes in the array. * /*from w ww .j a v a 2 s . c o m*/ * @param buf * input data * @param sep * separate every pair of hexadecimal digits with this separator, or * null if no separation is needed. * @param lineLen * break the output String into lines containing output for lineLen * bytes. */ public static String toHexString(ByteBuffer buf, String sep, int lineLen) { return toHexString(buf.array(), buf.arrayOffset() + buf.position(), buf.remaining(), sep, lineLen); }
From source file:org.apache.hadoop.hdfs.protocol.datatransfer.PacketReceiver.java
private static void readChannelFully(ReadableByteChannel ch, ByteBuffer buf) throws IOException { while (buf.remaining() > 0) { int n = ch.read(buf); if (n < 0) { throw new IOException("Premature EOF reading from " + ch); }/* w w w . j av a 2 s . co m*/ } }
From source file:org.apache.hadoop.util.NativeCrc32.java
/** * Verify the given buffers of data and checksums, and throw an exception * if any checksum is invalid. The buffers given to this function should * have their position initially at the start of the data, and their limit * set at the end of the data. The position, limit, and mark are not * modified./* www .jav a 2 s .c o m*/ * * @param bytesPerSum the chunk size (eg 512 bytes) * @param checksumType the DataChecksum type constant * @param sums the DirectByteBuffer pointing at the beginning of the * stored checksums * @param data the DirectByteBuffer pointing at the beginning of the * data to check * @param basePos the position in the file where the data buffer starts * @param fileName the name of the file being verified * @throws ChecksumException if there is an invalid checksum */ public static void verifyChunkedSums(int bytesPerSum, int checksumType, ByteBuffer sums, ByteBuffer data, String fileName, long basePos) throws ChecksumException { nativeVerifyChunkedSums(bytesPerSum, checksumType, sums, sums.position(), data, data.position(), data.remaining(), fileName, basePos); }
From source file:Main.java
public static ReadableByteChannel asReadableByteChannel(final ByteBuffer buffer) { return new ReadableByteChannel() { private boolean open = true; public int read(ByteBuffer dst) throws IOException { if (open == false) { throw new ClosedChannelException(); }/*from www. j av a 2 s .co m*/ final int p = buffer.position(); final int l = buffer.limit(); final int r = dst.remaining(); buffer.limit(p + r); dst.put(buffer); buffer.limit(l); return r; } public void close() throws IOException { open = false; } public boolean isOpen() { return open; } }; }
From source file:jp.andeb.obbutil.ObbUtilMain.java
private static boolean doRemove(String[] args) { if (args.length != 1) { printUsage(PROGNAME);/* w ww . j a va 2 s. co m*/ return false; } final File targetFile = new File(args[0]); final RandomAccessFile targetRaFile; try { targetRaFile = new RandomAccessFile(targetFile, "rw"); } catch (FileNotFoundException e) { System.err.println("????: " + targetFile.getPath()); return false; } try { final ObbInfoV1 obbInfo; try { obbInfo = ObbInfoV1.fromFile(targetRaFile); } catch (IOException e) { System.err .println("????????: " + targetFile.getPath()); return false; } catch (NotObbException e) { System.err.println( "? OBB ???????: " + targetFile.getPath()); return false; } final ByteBuffer obbInfoBytes = obbInfo.toBytes(); targetRaFile.setLength(targetRaFile.length() - obbInfoBytes.remaining()); } catch (IOException e) { System.err.println("OBB ??????: " + targetFile.getPath()); return false; } finally { try { targetRaFile.close(); } catch (IOException e) { System.err.println("OBB ??????: " + targetFile.getPath()); return false; } } System.err.println("OBB ???????: " + targetFile.getPath()); return true; }
From source file:Main.java
/** * @return//from w w w .j a v a 2 s . c om */ public static WritableByteChannel asWritableByteChannel(final ByteBuffer buffer) { return new WritableByteChannel() { private boolean open = true; public int write(ByteBuffer src) throws IOException { if (open == false) { throw new ClosedChannelException(); } final int p = buffer.position(); final int l = buffer.limit(); final int r = src.remaining(); buffer.limit(l + r); buffer.position(l); buffer.put(src); buffer.position(p); return r; } public void close() throws IOException { open = false; } public boolean isOpen() { return open; } }; }
From source file:com.ntsync.shared.ContactGroup.java
private static String readRawString(ByteBuffer src) throws UnsupportedEncodingException { int len = src != null ? src.remaining() : -1; if (len >= 0) { byte[] output = new byte[len]; src.get(output, 0, len);//from w w w .j av a 2 s.co m // Android has UTF-8 as default return new String(output, SyncDataHelper.DEFAULT_CHARSET_NAME); } return null; }
From source file:android.wulongdao.thirdparty.mime.HttpMultipart.java
private static ByteArrayBuffer encode(final Charset charset, final String string) { ByteBuffer encoded = charset.encode(CharBuffer.wrap(string)); ByteArrayBuffer bab = new ByteArrayBuffer(encoded.remaining()); bab.append(encoded.array(), encoded.position(), encoded.remaining()); return bab;//from www . j a v a2 s . c om }