public static void main(String[] args) { ByteBuffer bb = ByteBuffer.wrap(new byte[] {0, 0, 0, 0, 0, 0, 0, 'a'}); bb.rewind(); printnb("Byte Buffer "); while (bb.hasRemaining()) printnb(bb.position() + " -> " + bb.get() + ", "); print(); CharBuffer cb = ((ByteBuffer) bb.rewind()).asCharBuffer(); printnb("Char Buffer "); while (cb.hasRemaining()) printnb(cb.position() + " -> " + cb.get() + ", "); print(); FloatBuffer fb = ((ByteBuffer) bb.rewind()).asFloatBuffer(); printnb("Float Buffer "); while (fb.hasRemaining()) printnb(fb.position() + " -> " + fb.get() + ", "); print(); IntBuffer ib = ((ByteBuffer) bb.rewind()).asIntBuffer(); printnb("Int Buffer "); while (ib.hasRemaining()) printnb(ib.position() + " -> " + ib.get() + ", "); print(); LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer(); printnb("Long Buffer "); while (lb.hasRemaining()) printnb(lb.position() + " -> " + lb.get() + ", "); print(); ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer(); printnb("Short Buffer "); while (sb.hasRemaining()) printnb(sb.position() + " -> " + sb.get() + ", "); print(); DoubleBuffer db = ((ByteBuffer) bb.rewind()).asDoubleBuffer(); printnb("Double Buffer "); while (db.hasRemaining()) printnb(db.position() + " -> " + db.get() + ", "); }
public static void main(String args[]) throws Exception { FloatBuffer buffer = FloatBuffer.allocate(10); for (int i = 0; i < buffer.capacity(); ++i) { float f = (float) Math.sin((((float) i) / 10) * (2 * Math.PI)); buffer.put(f); } /** flip()源码: */ // public final Buffer flip() { // limit = position; // position = 0; // mark = -1; // return this; // } /** * 反转缓冲区。首先将限制设置为当前位置,然后将位置设置为 0。 如果已定义了标记,则丢弃该标记。 常与compact方法一起使用。 通常情况下,在准备从缓冲区中读取数据时调用flip方法。 */ buffer.flip(); while (buffer.hasRemaining()) { float f = buffer.get(); System.out.println(f); } }
private void switchBuffers(int offset, float[] audioArray, SyncBuffer oldBuffer) { int toBePadded = offset; FloatBuffer oldFloatBuffer = oldBuffer.asFloatBuffer(); // 1) use the remaining space in the current buffer // ...pad the current buffer as much as fits. while ((toBePadded > 0) && (oldFloatBuffer.hasRemaining())) { oldFloatBuffer.put(0F); toBePadded--; samplesProcessed++; } // ...put as much as fits from the audioArray int audioArrayWrittenToOld = 0; if (oldFloatBuffer.hasRemaining() && toBePadded == 0) { audioArrayWrittenToOld = oldFloatBuffer.remaining(); oldFloatBuffer.put(audioArray, 0, audioArrayWrittenToOld); samplesProcessed += audioArrayWrittenToOld; } // 2) put the rest in a new buffer if (!startBufferDone) { // setup the first file buffer bussyBufferProvider = new AlwaysStreamedBuffer(new SyncBuffer(requestedFileBufferSizeFloat)); } else { if (!firstFileBufferDone) { // setup the second file buffer bussyBufferProvider = new AlwaysStreamedBuffer(new SyncBuffer(requestedFileBufferSizeFloat)); } } if (bussyBufferProvider == null) { logger.severe("Next buffer is null!!"); return; } if (!bussyBufferProvider.isDone()) { logger.warning("Next buffer not ready."); overflowCount++; return; } SyncBuffer newBuffer; FileChannel channel; try { FileWriteTaskResult result = bussyBufferProvider.get(); newBuffer = result.getbuffer(); channel = result.getChannel(); } catch (InterruptedException | ExecutionException ex) { logger.log(Level.SEVERE, null, ex); return; } FloatBuffer newFloatBuffer = newBuffer.asFloatBuffer(); // int remainingInNewBuffer = newFloatBuffer.remaining(); int toBeWrittenToNew = toBePadded + audioArray.length - audioArrayWrittenToOld; if (toBeWrittenToNew > remainingInNewBuffer) { logger.severe("File buffer too small for this offset."); } else { for (int i = 0; i < toBePadded; i++) { newFloatBuffer.put(0F); samplesProcessed++; } int restAudioArray = audioArray.length - audioArrayWrittenToOld; newFloatBuffer.put(audioArray, audioArrayWrittenToOld, restAudioArray); samplesProcessed += restAudioArray; } if (startBufferDone) { FileWriteTask fileWriteTask = new FileWriteTask(outputFile, channel, oldBuffer); currentBufferProvider = bussyBufferProvider; bussyBufferProvider = executor.submit(fileWriteTask); firstFileBufferDone = true; } else { // we just have processed the first buffer: currentBufferProvider = bussyBufferProvider; bussyBufferProvider = null; startBufferDone = true; } }