Convert an ndarray message chunk to a buffer.
@param chunk the chunk to convert
@return an {@link ByteBuffer} based on the
passed in message chunk.
503844
public static ByteBuffer toBuffer(NDArrayMessageChunk chunk) {
        ByteBuffer ret = ByteBuffer.allocateDirect(sizeForMessage(chunk)).order(ByteOrder.nativeOrder());
        //the messages opType enum as an int
        ret.putInt(chunk.getMessageType().ordinal());
        //the number of chunks this chunk is apart of
        ret.putInt(chunk.getNumChunks());
        //the chunk size
        ret.putInt(chunk.getChunkSize());
        //the length of the id (for self describing purposes)
        ret.putInt(chunk.getId().getBytes().length);
        // the actual id as a string
        ret.put(chunk.getId().getBytes());
        //the chunk index
        ret.putInt(chunk.getChunkIndex());
        //the actual data
        ret.put(chunk.getData());
        return ret;
    }
220579
public void setBuffer (byte[] bytes, int offset, int count) {
		if (bytes == null) throw new IllegalArgumentException("bytes cannot be null.");
		buffer = bytes;
		position = offset;
		limit = offset + count;
		capacity = bytes.length;
		total = 0;
		inputStream = null;
	}
281688
protected byte[] getBuffer(int size) {
    byte[] buffer;
    if (size > SERIALIZER_BUFFER_SIZE) {
      buffer = new byte[size];
    } else {
      buffer = serializerBuffers.get();
      serializerBuffers.remove();
    }
    return buffer;
  }
124297
@Override
    public byte[] readBuffer(final ChannelBuffer buffer) {
        int len = getLength();
        byte[] matched = new byte[len];
        buffer.readBytes(matched);
        return matched;
    }
1180694
public static byte[] toArray(ByteBuffer buffer) {
        if (buffer.hasArray()) {
            byte[] array = buffer.array();
            int from = buffer.arrayOffset() + buffer.position();
            return Arrays.copyOfRange(array, from, from + buffer.remaining());
        } else {
            byte[] to = new byte[buffer.remaining()];
            buffer.slice().get(to);
            return to;
        }
    }
843887
public ByteBuffer sliceAsByteBuffer(int index, int length)
    {
        if (hasArray()) {
            return ByteBuffer.wrap((byte[]) base, (int) ((address - ARRAY_BYTE_BASE_OFFSET) + index), length);
        }
        else {
            assert (!isUniversalBuffer);
            return DirectBufferAccess.newByteBuffer(address, index, length, reference);
        }
    }
99437
public static DirectBuffer toBuffer(NDArrayMessage message) {
        ByteBuffer byteBuffer =
                        ByteBuffer.allocateDirect(byteBufferSizeForMessage(message)).order(ByteOrder.nativeOrder());
        //declare message opType
        byteBuffer.putInt(MessageType.WHOLE.ordinal());
        //perform the ndarray put on the
        if (message.getArr().isCompressed()) {
            AeronNDArraySerde.doByteBufferPutCompressed(message.getArr(), byteBuffer, false);
        } else {
            AeronNDArraySerde.doByteBufferPutUnCompressed(message.getArr(), byteBuffer, false);
        }

        long sent = message.getSent();
        long index = message.getIndex();

        byteBuffer.putLong(sent);
        byteBuffer.putLong(index);
        byteBuffer.putInt(message.getDimensions().length);
        for (int i = 0; i < message.getDimensions().length; i++) {
            byteBuffer.putInt(message.getDimensions()[i]);
        }

        //rewind the buffer before putting it in to the unsafe buffer
        //note that we set rewind to false in the do byte buffer put methods
        byteBuffer.rewind();

        return new UnsafeBuffer(byteBuffer);
    }
732823
protected static ByteBuffer toTCPBuffer(GelfMessage message, ThreadLocal<ByteBuffer> writeBuffers) {

        while (true) {

            try {
                return message.toTCPBuffer(getBuffer(writeBuffers));
            } catch (BufferOverflowException e) {
                enlargeBuffer(writeBuffers);
            }
        }
    }
140812
static ByteBuffer toFrame(ByteBuffer input, int dataSize) throws GeneralSecurityException {
    Preconditions.checkNotNull(input);
    if (dataSize > input.remaining()) {
      dataSize = input.remaining();
    }
    Producer producer = new Producer();
    ByteBuffer inputAlias = input.duplicate();
    inputAlias.limit(input.position() + dataSize);
    producer.readBytes(inputAlias);
    producer.flush();
    input.position(inputAlias.position());
    ByteBuffer output = producer.getRawFrame();
    return output;
  }
1168223
public static NDArrayMessage[] chunkedMessages(NDArrayMessage arrayMessage, int chunkSize) {
        int sizeOfMessage = NDArrayMessage.byteBufferSizeForMessage(arrayMessage) - 4;
        int numMessages = sizeOfMessage / chunkSize;
        ByteBuffer direct = NDArrayMessage.toBuffer(arrayMessage).byteBuffer();
        NDArrayMessage[] ret = new NDArrayMessage[numMessages];
        for (int i = 0; i < numMessages; i++) {
            byte[] chunk = new byte[chunkSize];
            direct.get(chunk, i * chunkSize, chunkSize);
            ret[i] = NDArrayMessage.builder().chunk(chunk).numChunks(numMessages).build();
        }
        return ret;
    }
815134
public static byte[] getArray(ByteBuffer buffer)
    {
        int length = buffer.remaining();

        if (buffer.hasArray())
        {
            int boff = buffer.arrayOffset() + buffer.position();
            return Arrays.copyOfRange(buffer.array(), boff, boff + length);
        }
        // else, DirectByteBuffer.get() is the fastest route
        byte[] bytes = new byte[length];
        buffer.duplicate().get(bytes);

        return bytes;
    }
1221021
protected byte[] fileToByteArray(File file) throws IOException
   {
      FileInputStream in = null;

      try
      {
         if (channel == null || !channel.isOpen())
         {
            in = PrivilegedFileHelper.fileInputStream(file);
            channel = in.getChannel();
         }

         ByteBuffer bb = ByteBuffer.allocate((int)channel.size());
         channel.read(bb);
         if (bb.hasArray())
         {
            return bb.array();
         }
         else
         {
            // impossible code in most cases, as we use heap backed buffer
            byte[] tmpb = new byte[bb.capacity()];
            bb.get(tmpb);
            return tmpb;
         }
      }
      finally
      {
         if (in != null)
         {
            in.close();
            if (channel != null)
            {
               channel.close();
            }
         }
      }
   }
1290008
function ByteBuffer(size) {
                /*private*/ this.bigEndian = true;
                this.bytes = null;
                this.cursor = 0;
                this.bytes = (function (s) { var a = []; while (s-- > 0)
                    a.push(0); return a; })(size);
                this.cursor = 0;
            }
213136
static byte[] serializeChunk(SegmentChunk segmentChunk) {
        return combine(Long.toString(segmentChunk.getStartOffset()), segmentChunk.getName());
    }
1326175
protected byte[] byteBufferToByteArray() {
        if (byteBuffer.hasArray()) {
            return byteBuffer.array();
        } else {
            final byte[] byteArray = new byte[byteBuffer.remaining()];
            byteBuffer.get(byteArray);
            return byteArray;
        }
    }
133070
function toBuffer (chunk, encoding) {
  return !Buffer.isBuffer(chunk)
    ? Buffer.from(chunk, encoding)
    : chunk
}
392731
function arrayToBuffer(arr) {
    if (arr instanceof ArrayBuffer) {
        return arr;
    }
    var ab = arr.buffer;
    if (arr.byteOffset === 0 && arr.byteLength === ab.byteLength) {
        return ab;
    }
    return arr.buffer.slice(arr.byteOffset, arr.byteOffset + arr.byteLength);
}