core/src/main/java/com/orientechnologies/orient/core/serialization/OMemoryStream.java
public final byte[] toByteArray() {
    if (position == buffer.length - 1)
      // 100% USED, RETURN THE FULL BUFFER
      return buffer;

    final int pos = position;

    final byte[] destinBuffer = new byte[pos];
    final byte[] sourceBuffer = buffer;

    if (pos < NATIVE_COPY_THRESHOLD)
      for (int i = 0; i < pos; ++i)
        destinBuffer[i] = sourceBuffer[i];
    else
      System.arraycopy(sourceBuffer, 0, destinBuffer, 0, pos);

    return destinBuffer;
  }
public final byte[] toByteArray() {
    if (position == buffer.length - 1)
      // 100% USED, RETURN THE FULL BUFFER
      return buffer;

    final int pos = position;

    final byte[] destinBuffer = new byte[pos];
    final byte[] sourceBuffer = buffer;

    if (pos < NATIVE_COPY_THRESHOLD)
      for (int i = 0; i < pos; ++i)
        destinBuffer[i] = sourceBuffer[i];
    else
      System.arraycopy(sourceBuffer, 0, destinBuffer, 0, pos);

    return destinBuffer;
  }
Returns the used buffer as byte[].

@return [result.length = size()]