private static ByteBuffer allocate(int capacity, boolean direct) { return (direct ? ByteBuffer.allocateDirect(capacity) : ByteBuffer.allocate(capacity)); }
public static ByteBuffer newByteBuffer (int numBytes) { ByteBuffer buffer = ByteBuffer.allocateDirect(numBytes); buffer.order(ByteOrder.nativeOrder()); return buffer; }
/** * Returns a new {@link ByteBuffer} which has the same {@link ByteOrder} as the native order of the machine. */ public static ByteBuffer allocateDirectWithNativeOrder(int capacity) { return ByteBuffer.allocateDirect(capacity).order( PlatformDependent.BIG_ENDIAN_NATIVE_ORDER ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN); }
void setup (byte[] pcm, int channels, int sampleRate) { int bytes = pcm.length - (pcm.length % (channels > 1 ? 4 : 2)); int samples = bytes / (2 * channels); duration = samples / (float)sampleRate; ByteBuffer buffer = ByteBuffer.allocateDirect(bytes); buffer.order(ByteOrder.nativeOrder()); buffer.put(pcm, 0, bytes); buffer.flip(); if (bufferID == -1) { bufferID = alGenBuffers(); alBufferData(bufferID, channels > 1 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, buffer.asShortBuffer(), sampleRate); } }
/** {@inheritDoc} */ @Override public ByteBuffer encode(GridNioSession ses, Object msg) throws IOException, IgniteCheckedException { byte[] msg0 = (byte[])msg; int cap = msg0.length + delim.length; ByteBuffer res = directBuf ? ByteBuffer.allocateDirect(cap) : ByteBuffer.allocate(cap); res.put(msg0); res.put(delim); res.flip(); return res; }
/** * Clone an input bytes array as direct ByteBuffer. */ static ByteBuffer cloneAsDirectByteBuffer(byte[] input, int offset, int len) { if (input == null) { // an input can be null, if erased or not to read return null; } ByteBuffer directBuffer = ByteBuffer.allocateDirect(len); directBuffer.put(input, offset, len); directBuffer.flip(); return directBuffer; }
@Override public ChannelBuffer copy(int index, int length) { ByteBuffer src; try { src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length); } catch (IllegalArgumentException e) { throw new IndexOutOfBoundsException(); } ByteBuffer dst = buffer.isDirect() ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length); dst.put(src); dst.clear(); return new ByteBufferBackedChannelBuffer(dst); }
private void resizeByteBuffer( int newSize ) { ByteBuffer newBuffer = ByteBuffer.allocateDirect( newSize ); // Increase by 50% newBuffer.position( 0 ); newBuffer.put( bb ); bb = newBuffer; }
@Override public void queueInput(ByteBuffer inputBuffer) { Assertions.checkState(outputChannels != null); int position = inputBuffer.position(); int limit = inputBuffer.limit(); int frameCount = (limit - position) / (2 * channelCount); int outputSize = frameCount * outputChannels.length * 2; if (buffer.capacity() < outputSize) { buffer = ByteBuffer.allocateDirect(outputSize).order(ByteOrder.nativeOrder()); } else { buffer.clear(); } while (position < limit) { for (int channelIndex : outputChannels) { buffer.putShort(inputBuffer.getShort(position + 2 * channelIndex)); } position += channelCount * 2; } inputBuffer.position(limit); buffer.flip(); outputBuffer = buffer; }
throw new IOException("Unable to read PKM file header."); final ByteBuffer headerBuffer = ByteBuffer.allocateDirect(ETC1.ETC_PKM_HEADER_SIZE) .order(ByteOrder.BIG_ENDIAN); headerBuffer.put(ioBuffer, 0, ETC1.ETC_PKM_HEADER_SIZE).position(0); if (!ETC2.isValid(headerBuffer)) { throw new IOException("Not a PKM file."); format = ETC2.getETC2CompressionType(headerBuffer); final int encodedSize = ETC2.getEncodedDataSize(width, height); final ByteBuffer dataBuffer = ByteBuffer.allocateDirect(encodedSize).order(ByteOrder.BIG_ENDIAN); for (int i = 0; i < encodedSize; ) { int chunkSize = Math.min(ioBuffer.length, encodedSize - i); throw new IOException("Unable to read PKM file data."); dataBuffer.put(ioBuffer, 0, chunkSize); i += chunkSize;
public NioBufferedFileInputStream(File file, int bufferSizeInBytes) throws IOException { byteBuffer = ByteBuffer.allocateDirect(bufferSizeInBytes); fileChannel = FileChannel.open(file.toPath(), StandardOpenOption.READ); byteBuffer.flip(); }
void setup (byte[] pcm, int channels, int sampleRate) { int bytes = pcm.length - (pcm.length % (channels > 1 ? 4 : 2)); int samples = bytes / (2 * channels); duration = samples / (float)sampleRate; ByteBuffer buffer = ByteBuffer.allocateDirect(bytes); buffer.order(ByteOrder.nativeOrder()); buffer.put(pcm, 0, bytes); buffer.flip(); if (bufferID == -1) { bufferID = alGenBuffers(); alBufferData(bufferID, channels > 1 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, buffer.asShortBuffer(), sampleRate); } }
/** {@inheritDoc} */ @Override public ByteBuffer encode(GridNioSession ses, Object msg) throws IOException, IgniteCheckedException { byte[] msg0 = (byte[])msg; ByteBuffer res = directBuf ? ByteBuffer.allocateDirect(msg0.length + 4) : ByteBuffer.allocate(msg0.length + 4); //res.order(order); // TODO: GG-6460 res.putInt(msg0.length); res.put(msg0); res.flip(); return res; }
private ByteBuffer deepCopy(ByteBuffer source) { int sourceP = source.position(); int sourceL = source.limit(); ByteBuffer target = ByteBuffer.allocateDirect(source.remaining()); target.put(source); target.flip(); source.position(sourceP); source.limit(sourceL); return target; }
public ByteBufferOutputStream(int initialCapacity, boolean directBuffer) { this(directBuffer ? ByteBuffer.allocateDirect(initialCapacity) : ByteBuffer.allocate(initialCapacity)); }
public static ByteBuffer newByteBuffer (int numBytes) { ByteBuffer buffer = ByteBuffer.allocateDirect(numBytes); buffer.order(ByteOrder.nativeOrder()); return buffer; }
@Override public ChannelBuffer copy(int index, int length) { ByteBuffer src; try { src = (ByteBuffer) buffer.duplicate().position(index).limit(index + length); } catch (IllegalArgumentException e) { throw new IndexOutOfBoundsException(); } ByteBuffer dst = buffer.isDirect() ? ByteBuffer.allocateDirect(length) : ByteBuffer.allocate(length); dst.put(src); dst.clear(); return new ByteBufferBackedChannelBuffer(dst); }
@NonNull public static ByteBuffer fromStream(@NonNull InputStream stream) throws IOException { ByteArrayOutputStream outStream = new ByteArrayOutputStream(BUFFER_SIZE); byte[] buffer = BUFFER_REF.getAndSet(null); if (buffer == null) { buffer = new byte[BUFFER_SIZE]; } int n; while ((n = stream.read(buffer)) >= 0) { outStream.write(buffer, 0, n); } BUFFER_REF.set(buffer); byte[] bytes = outStream.toByteArray(); // Some resource decoders require a direct byte buffer. Prefer allocateDirect() over wrap() return (ByteBuffer) ByteBuffer.allocateDirect(bytes.length).put(bytes).position(0); }
AbstractLogVisitor(long from, FileChannel fileChannel, int workingSize) { this.fileChannel = fileChannel; this.visited = new AtomicLong(from); this.visitedSnapshot = new AtomicLong(from); buffer = ByteBuffer.allocateDirect(workingSize); try { fileChannel.read(buffer, visited.get()); buffer.flip(); } catch (IOException e) { LOGGER.error("load dispatch log visitor error", e); } }
void setup (byte[] pcm, int channels, int sampleRate) { int bytes = pcm.length - (pcm.length % (channels > 1 ? 4 : 2)); int samples = bytes / (2 * channels); duration = samples / (float)sampleRate; ByteBuffer buffer = ByteBuffer.allocateDirect(bytes); buffer.order(ByteOrder.nativeOrder()); buffer.put(pcm, 0, bytes); buffer.flip(); if (bufferID == -1) { bufferID = alGenBuffers(); alBufferData(bufferID, channels > 1 ? AL_FORMAT_STEREO16 : AL_FORMAT_MONO16, buffer.asShortBuffer(), sampleRate); } }