/** * Returns a copy of the specified buffer, with the specified new size. The new size must be greater than or equal * to the specified buffer's size. If the new size is greater than the specified buffer's size, this returns a new * buffer which is partially filled with the contents of the specified buffer. The returned buffer is a backed by a * direct ByteBuffer if and only if the specified buffer is direct. * * @param buffer the buffer to copy. * @param newSize the new buffer's size, in shorts. * * @return the new buffer, with the specified size. * * @throws ArgumentException if the buffer is null, if the new size is negative, or if the new size is less * than the buffer's remaing elements. */ public static ShortBuffer copyOf(ShortBuffer buffer, int newSize) { if (newSize < 0 || newSize < buffer.remaining()) { String message = Logging.getMessage("generic.SizeOutOfRange", newSize); Logging.logger().severe(message); throw new ArgumentException(message); } ShortBuffer newBuffer = newShortBuffer(newSize, buffer.isDirect()); int pos = buffer.position(); // Save the input buffer's current position. try { newBuffer.put(buffer); newBuffer.rewind(); } finally { buffer.position(pos); // Restore the input buffer's original position. } return(newBuffer); }