/** * Fill a buffer with a repeated value. * * @param buffer the buffer to fill * @param value the value to fill * @param count the number of shorts to fill * @return the buffer instance */ public static ShortBuffer fill(ShortBuffer buffer, int value, int count) { if (count > buffer.remaining()) { throw msg.bufferUnderflow(); } if (buffer.hasArray()) { final int offs = buffer.arrayOffset(); Arrays.fill(buffer.array(), offs + buffer.position(), offs + buffer.limit(), (short) value); skip(buffer, count); } else { for (int i = count; i > 0; i--) { buffer.put((short)value); } } return buffer; }
/** * Take a certain number of shorts from the buffer and return them in an array. * * @param buffer the buffer to read * @param cnt the number of shorts to take * @return the shorts */ public static short[] take(ShortBuffer buffer, int cnt) { if (cnt < 0) { throw msg.parameterOutOfRange("cnt"); } if (buffer.hasArray()) { final int pos = buffer.position(); final int lim = buffer.limit(); if (lim - pos < cnt) { throw new BufferUnderflowException(); } final short[] array = buffer.array(); final int offset = buffer.arrayOffset(); buffer.position(pos + cnt); final int start = offset + pos; return Arrays.copyOfRange(array, start, start + cnt); } final short[] shorts = new short[cnt]; buffer.get(shorts); return shorts; }
/** * For direct buffers, calls {@link Pointer#Pointer(Buffer)}, while for buffers * backed with an array, allocates enough memory for the array and copies it. * * @param buffer the Buffer to reference or copy * @see #put(short[]) */ public ShortPointer(ShortBuffer buffer) { super(buffer); if (buffer != null && !buffer.isDirect() && buffer.hasArray()) { short[] array = buffer.array(); allocateArray(array.length - buffer.arrayOffset()); put(array, buffer.arrayOffset(), array.length - buffer.arrayOffset()); position(buffer.position()); limit(buffer.limit()); } } /**
protected static void getShortArray(ShortBuffer buf, short[] arr) { if (!buf.hasArray() || buf.array() != arr) { buf.position(0); buf.get(arr); buf.rewind(); } }
protected static void putShortArray(ShortBuffer buf, short[] arr) { if (!buf.hasArray() || buf.array() != arr) { buf.position(0); buf.put(arr); buf.rewind(); } }
/** * ByteChannelからshort配列を読み込む * @param channel * @return * @throws IOException */ public static short[] readShortArray(@NonNull final ByteChannel channel) throws IOException { final int n = readInt(channel); final ByteBuffer buf = ByteBuffer.allocate(n * 2).order(ByteOrder.BIG_ENDIAN); final int readBytes = channel.read(buf); if (readBytes != n * 2) throw new IOException(); buf.clear(); final ShortBuffer result = buf.asShortBuffer(); if (result.hasArray()) { return result.array(); } else { final short[] b = new short[n]; result.get(b); return b; } }
protected static void updateShortBuffer(ShortBuffer buf, short[] arr, int offset, int size) { if (USE_DIRECT_BUFFERS || (buf.hasArray() && buf.array() != arr)) { buf.position(offset); buf.put(arr, offset, size); buf.rewind(); } }
protected static void getShortArray(ShortBuffer buf, short[] arr) { if (!buf.hasArray() || buf.array() != arr) { buf.position(0); buf.get(arr); buf.rewind(); } }
protected static void putShortArray(ShortBuffer buf, short[] arr) { if (!buf.hasArray() || buf.array() != arr) { buf.position(0); buf.put(arr); buf.rewind(); } }
protected static void updateShortBuffer(ShortBuffer buf, short[] arr, int offset, int size) { if (USE_DIRECT_BUFFERS || (buf.hasArray() && buf.array() != arr)) { buf.position(offset); buf.put(arr, offset, size); buf.rewind(); } }
public Int16Array copy(ShortBuffer buffer) { buffer = buffer.duplicate(); Int16Array result = Int16Array.create(buffer.remaining()); short[] tmp; if (buffer.hasArray()) { tmp = buffer.array(); } else { tmp = new short[buffer.remaining()]; buffer.get(tmp); } for (int i = 0; i < tmp.length; ++i) { result.set(i, tmp[i]); } return result; }
/** * Fill a buffer with a repeated value. * * @param buffer the buffer to fill * @param value the value to fill * @param count the number of shorts to fill * @return the buffer instance */ public static ShortBuffer fill(ShortBuffer buffer, int value, int count) { if (count > buffer.remaining()) { throw msg.bufferUnderflow(); } if (buffer.hasArray()) { final int offs = buffer.arrayOffset(); Arrays.fill(buffer.array(), offs + buffer.position(), offs + buffer.limit(), (short) value); skip(buffer, count); } else { for (int i = count; i > 0; i--) { buffer.put((short)value); } } return buffer; }
/** * Take a certain number of shorts from the buffer and return them in an array. * * @param buffer the buffer to read * @param cnt the number of shorts to take * @return the shorts */ public static short[] take(ShortBuffer buffer, int cnt) { if (cnt < 0) { throw msg.parameterOutOfRange("cnt"); } if (buffer.hasArray()) { final int pos = buffer.position(); final int lim = buffer.limit(); if (lim - pos < cnt) { throw new BufferUnderflowException(); } final short[] array = buffer.array(); final int offset = buffer.arrayOffset(); buffer.position(pos + cnt); final int start = offset + pos; return Arrays.copyOfRange(array, start, start + cnt); } final short[] shorts = new short[cnt]; buffer.get(shorts); return shorts; }
/** * For direct buffers, calls {@link Pointer#Pointer(Buffer)}, while for buffers * backed with an array, allocates enough memory for the array and copies it. * * @param buffer the Buffer to reference or copy * @see #put(short[]) */ public ShortPointer(ShortBuffer buffer) { super(buffer); if (buffer != null && buffer.hasArray()) { short[] array = buffer.array(); allocateArray(array.length); put(array); position(buffer.position()); limit(buffer.limit()); } } /**
/** * For direct buffers, calls {@link Pointer#Pointer(Buffer)}, while for buffers * backed with an array, allocates enough memory for the array and copies it. * * @param buffer the Buffer to reference or copy * @see #put(short[]) */ public ShortPointer(ShortBuffer buffer) { super(buffer); if (buffer != null && !buffer.isDirect() && buffer.hasArray()) { short[] array = buffer.array(); allocateArray(array.length - buffer.arrayOffset()); put(array, buffer.arrayOffset(), array.length - buffer.arrayOffset()); position(buffer.position()); limit(buffer.limit()); } } /**
buffer.position(oldPosition); else if (buffer.hasArray())
/** * Creates a new Pointer to the given buffer, taking into * account the position and array offset of the given buffer. * The buffer is assumed to be non-<code>null</code>, and * be either direct or have a backing array. * * @param buffer The buffer * @return The pointer */ private static Pointer computePointer(ShortBuffer buffer) { Pointer result = null; if (buffer.isDirect()) { int oldPosition = buffer.position(); buffer.position(0); result = Pointer.to(buffer.slice()).withByteOffset( oldPosition * Sizeof.SHORT); buffer.position(oldPosition); } else if (buffer.hasArray()) { ShortBuffer t = ShortBuffer.wrap(buffer.array()); int elementOffset = buffer.position() + buffer.arrayOffset(); result = Pointer.to(t).withByteOffset( elementOffset * Sizeof.SHORT); } return result; }
/** * Creates a new Pointer to the given buffer, taking into * account the position and array offset of the given buffer. * The buffer is assumed to be non-<code>null</code>, and * be either direct or have a backing array. * * @param buffer The buffer * @return The pointer */ private static Pointer computePointer(ShortBuffer buffer) { Pointer result = null; if (buffer.isDirect()) { int oldPosition = buffer.position(); buffer.position(0); result = Pointer.to(buffer.slice()).withByteOffset( oldPosition * Sizeof.SHORT); buffer.position(oldPosition); } else if (buffer.hasArray()) { ShortBuffer t = ShortBuffer.wrap(buffer.array()); int elementOffset = buffer.position() + buffer.arrayOffset(); result = Pointer.to(t).withByteOffset( elementOffset * Sizeof.SHORT); } return result; }
/** * Creates a new Pointer to the given buffer, taking into * account the position and array offset of the given buffer. * The buffer is assumed to be non-<code>null</code>, and * be either direct or have a backing array. * * @param buffer The buffer * @return The pointer */ private static Pointer computePointer(ShortBuffer buffer) { Pointer result = null; if (buffer.isDirect()) { int oldPosition = buffer.position(); buffer.position(0); result = Pointer.to(buffer.slice()).withByteOffset( oldPosition * Sizeof.SHORT); buffer.position(oldPosition); } else if (buffer.hasArray()) { ShortBuffer t = ShortBuffer.wrap(buffer.array()); int elementOffset = buffer.position() + buffer.arrayOffset(); result = Pointer.to(t).withByteOffset( elementOffset * Sizeof.SHORT); } return result; }
/** * Creates a new Pointer to the given buffer, taking into * account the position and array offset of the given buffer. * The buffer is assumed to be non-<code>null</code>, and * be either direct or have a backing array. * * @param buffer The buffer * @return The pointer */ private static Pointer computePointer(ShortBuffer buffer) { Pointer result = null; if (buffer.isDirect()) { int oldPosition = buffer.position(); buffer.position(0); result = Pointer.to(buffer.slice()).withByteOffset( oldPosition * Sizeof.SHORT); buffer.position(oldPosition); } else if (buffer.hasArray()) { ShortBuffer t = ShortBuffer.wrap(buffer.array()); int elementOffset = buffer.position() + buffer.arrayOffset(); result = Pointer.to(t).withByteOffset( elementOffset * Sizeof.SHORT); } return result; }