List of usage examples for java.nio ByteBuffer hasRemaining
public final boolean hasRemaining()
From source file:edu.tsinghua.lumaqq.qq.Util.java
/** * buf???buf/*w w w . ja v a2 s. c o m*/ * <p> * ?buf??buf???? * ?buf???buf?? * </p> * <p> * QQ???GBK? * </p> * * @param buf * ByteBuffer * @return */ public static String getString(ByteBuffer buf) { baos.reset(); while (buf.hasRemaining()) { baos.write(buf.get()); } return getString(baos.toByteArray()); }
From source file:org.apache.usergrid.persistence.Schema.java
public static ByteBuffer encrypt(ByteBuffer clear) { if (clear == null || !clear.hasRemaining()) { return clear; }//from w w w . j a va 2s . co m try { SecretKeySpec sKeySpec = new SecretKeySpec(getRawKey(encryptionSeed), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, sKeySpec); ByteBuffer encrypted = ByteBuffer.allocate(cipher.getOutputSize(clear.remaining())); cipher.doFinal(clear, encrypted); encrypted.rewind(); return encrypted; } catch (Exception e) { throw new IllegalStateException(e); } }
From source file:edu.tsinghua.lumaqq.qq.Util.java
/** * buf???buf?lenbyte??/*from w w w . java2 s .c o m*/ * <p> * ?buf??buf???? * ?buf???len??? * </p> * <p> * QQ???GBK? * </p> * * @param buf * ByteBuffer * @return */ public static String getString(ByteBuffer buf, int len) { baos.reset(); while (buf.hasRemaining() && len-- > 0) { baos.write(buf.get()); } return getString(baos.toByteArray()); }
From source file:org.apache.usergrid.persistence.Schema.java
public static ByteBuffer decrypt(ByteBuffer encrypted) { if (encrypted == null || !encrypted.hasRemaining()) { return encrypted; }// w w w . j av a 2s . c o m try { SecretKeySpec sKeySpec = new SecretKeySpec(getRawKey(encryptionSeed), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, sKeySpec); ByteBuffer decrypted = ByteBuffer.allocate(cipher.getOutputSize(encrypted.remaining())); cipher.doFinal(encrypted, decrypted); decrypted.rewind(); return decrypted; } catch (Exception e) { throw new IllegalStateException(e); } }
From source file:edu.tsinghua.lumaqq.qq.Util.java
/** * buf???buf/*www . j a va 2 s . com*/ * <p> * ?buf??buf???? * ?buf???? * </p> * <p> * QQ???GBK? * </p> * * @param buf * ByteBuffer * @param delimit * * @return */ public static String getString(ByteBuffer buf, byte delimit) { baos.reset(); while (buf.hasRemaining()) { byte b = buf.get(); if (b == delimit) return getString(baos.toByteArray()); else baos.write(b); } return getString(baos.toByteArray()); }
From source file:edu.tsinghua.lumaqq.qq.Util.java
/** * buf???delimit?maxLenbyte/*from ww w .jav a2 s .c om*/ * ?? * <p> * ?buf??buf???? * ?buf???maxLen? * </p> * <p> * QQ???GBK? * </p> * * @param buf * ByteBuffer * @param delimit * delimit * @param maxLen * max len to read * @return String */ public static String getString(ByteBuffer buf, byte delimit, int maxLen) { baos.reset(); while (buf.hasRemaining() && maxLen-- > 0) { byte b = buf.get(); if (b == delimit) break; else baos.write(b); } while (buf.hasRemaining() && maxLen-- > 0) buf.get(); return getString(baos.toByteArray()); }
From source file:io.github.dsheirer.record.wave.WaveWriter.java
/** * Writes the buffer contents to the file. Assumes that the buffer is full * and the first byte of data is at position 0. *//*from ww w. j a v a 2 s. c o m*/ public void writeData(ByteBuffer buffer) throws IOException { buffer.position(0); openDataChunk(); /* Write the full buffer if there is room, respecting the max file size */ if (mFileChannel.size() + buffer.capacity() < mMaxSize) { while (buffer.hasRemaining()) { mDataChunkSize += mFileChannel.write(buffer); } updateTotalSize(); updateDataChunkSize(); } else { /* Split the buffer to finish filling the current file and then put * the leftover into a new file */ int remaining = (int) (mMaxSize - mFileChannel.size()); /* Ensure we write full frames to fill up the remaining size */ remaining -= (int) (remaining % mAudioFormat.getFrameSize()); byte[] bytes = buffer.array(); ByteBuffer current = ByteBuffer.wrap(Arrays.copyOf(bytes, remaining)); ByteBuffer next = ByteBuffer.wrap(Arrays.copyOfRange(bytes, remaining, bytes.length)); while (current.hasRemaining()) { mDataChunkSize += mFileChannel.write(current); } updateTotalSize(); updateDataChunkSize(); rollover(); openDataChunk(); while (next.hasRemaining()) { mDataChunkSize += mFileChannel.write(next); } updateTotalSize(); updateDataChunkSize(); } }
From source file:com.openteach.diamond.network.waverider.session.DefaultSession.java
@Override public void onRead() throws IOException, InterruptedException { logger.debug("onRead"); ByteBuffer buffer = ByteBuffer.allocate(NetWorkConstants.DEFAULT_NETWORK_BUFFER_SIZE); int ret = 0;/*from w ww .j a v a 2 s.co m*/ do { ret = channel.read(buffer); } while (ret > 0); if (ret == -1) { throw new IOException("EOF"); } buffer.flip(); if (buffer.hasRemaining()) { inputBuffer.put(buffer); synchronized (waitMoreDataLock) { waitMoreDataLock.notifyAll(); } } //logger.info("Session is onRead, read " + buffer.remaining() + " bytes"); }
From source file:io.github.dsheirer.record.wave.WaveWriter.java
/** * Writes the metadata to the end of the file if there is sufficient space without exceeding the * max file size./* ww w .ja va2s . c o m*/ */ public void writeMetadata(WaveMetadata metadata) throws IOException { ByteBuffer listChunk = metadata.getLISTChunk(); if (mFileChannel.size() + listChunk.capacity() >= mMaxSize) { throw new IOException("Cannot write LIST metadata chunk - insufficient file space remaining"); } closeDataChunk(); listChunk.position(0); while (listChunk.hasRemaining()) { mFileChannel.write(listChunk); } updateTotalSize(); ByteBuffer id3Chunk = metadata.getID3Chunk(); if (mFileChannel.size() + id3Chunk.capacity() >= mMaxSize) { throw new IOException("Cannot write ID3 metadata chunk - insufficient file space remaining"); } id3Chunk.position(0); while (id3Chunk.hasRemaining()) { mFileChannel.write(id3Chunk); } updateTotalSize(); }
From source file:com.newatlanta.appengine.nio.channels.GaeFileChannel.java
@Override public synchronized int read(ByteBuffer dst) throws IOException { checkReadOptions();/*from w w w . j a v a2 s .c o m*/ long fileLen = doGetSize(); if (position >= fileLen) { return -1; } int totalBytesRead = 0; while (dst.hasRemaining() && (position < fileLen)) { int r = dst.remaining(); initBuffer(r); if (calcBlockIndex(position + r - 1) == index) { // within current block, read until dst is full or to EOF int eofoffset = calcBlockOffset(fileLen); int limit = Math.min(buffer.position() + r, eofoffset); if (limit > buffer.capacity()) { // copy the remaining bytes in buffer to dst, then fill dst // with empty bytes until full or to the calculated limit dst.put(buffer); dst.put(new byte[Math.min(limit - buffer.capacity(), dst.remaining())]); } else { buffer.limit(limit); dst.put(buffer); buffer.limit(buffer.capacity()); // restore original limit } int bytesRead = (r - dst.remaining()); totalBytesRead += bytesRead; positionInternal(position + bytesRead, false); } else { // read to the end of the current block r = buffer.remaining(); if (r == 0) { r = (int) (blockSize - position); dst.put(new byte[r]); } else { dst.put(buffer); } totalBytesRead += r; // move position to beginning of next buffer, repeat loop positionInternal(position + r, false); } } //closeBlock(); return totalBytesRead; }