/**
   * Creates a new composite buffer which wraps the slices of the specified NIO buffers without
   * copying them. A modification on the content of the specified buffers will be visible to the
   * returned buffer.
   *
   * @throws IllegalArgumentException if the specified buffers' endianness are different from each
   *     other
   */
  public static ChannelBuffer wrappedBuffer(ByteBuffer... buffers) {
    switch (buffers.length) {
      case 0:
        break;
      case 1:
        if (buffers[0].hasRemaining()) {
          return wrappedBuffer(buffers[0]);
        }
        break;
      default:
        ByteOrder order = null;
        final List<ChannelBuffer> components = new ArrayList<ChannelBuffer>(buffers.length);
        for (ByteBuffer b : buffers) {
          if (b == null) {
            break;
          }
          if (b.hasRemaining()) {
            if (order != null) {
              if (!order.equals(b.order())) {
                throw new IllegalArgumentException("inconsistent byte order");
              }
            } else {
              order = b.order();
            }
            components.add(wrappedBuffer(b));
          }
        }
        return compositeBuffer(order, components);
    }

    return EMPTY_BUFFER;
  }
Exemple #2
0
 public final AbstractMemory order(Ruby runtime, ByteOrder order) {
   return new Pointer(
       runtime,
       order.equals(getMemoryIO().order())
           ? getMemoryIO()
           : new SwappedMemoryIO(runtime, getMemoryIO()),
       size,
       typeSize);
 }
Exemple #3
0
 /**
  * Convenience method for encoding a short into a byte stream.
  *
  * @param value
  * @param out
  * @throws IOException
  */
 public void write(short value) throws IOException {
   if (endian.equals(ByteOrder.BIG_ENDIAN)) {
     out.write((byte) ((value >> 8) & 0xff));
     out.write((byte) (value & 0xff));
   } else {
     out.write((byte) (value & 0xff));
     out.write((byte) ((value >> 8) & 0xff));
   }
 }
Exemple #4
0
  /**
   * Creates a new buffer whose content is a merged copy of the specified {@code buffers}' slices.
   * The new buffer's {@code readerIndex} and {@code writerIndex} are {@code 0} and the sum of all
   * buffers' {@code remaining} respectively.
   *
   * @throws IllegalArgumentException if the specified buffers' endianness are different from each
   *     other
   */
  public static ByteBuf copiedBuffer(ByteBuffer... buffers) {
    switch (buffers.length) {
      case 0:
        return EMPTY_BUFFER;
      case 1:
        return copiedBuffer(buffers[0]);
    }

    // Merge the specified buffers into one buffer.
    ByteOrder order = null;
    int length = 0;
    for (ByteBuffer b : buffers) {
      int bLen = b.remaining();
      if (bLen <= 0) {
        continue;
      }
      if (Integer.MAX_VALUE - length < bLen) {
        throw new IllegalArgumentException("The total length of the specified buffers is too big.");
      }
      length += bLen;
      if (order != null) {
        if (!order.equals(b.order())) {
          throw new IllegalArgumentException("inconsistent byte order");
        }
      } else {
        order = b.order();
      }
    }

    if (length == 0) {
      return EMPTY_BUFFER;
    }

    byte[] mergedArray = new byte[length];
    for (int i = 0, j = 0; i < buffers.length; i++) {
      // Duplicate the buffer so we not adjust the position during our get operation.
      // See https://github.com/netty/netty/issues/3896
      ByteBuffer b = buffers[i].duplicate();
      int bLen = b.remaining();
      b.get(mergedArray, j, bLen);
      j += bLen;
    }

    return wrappedBuffer(mergedArray).order(order);
  }
Exemple #5
0
  /**
   * Creates a new buffer whose content is a merged copy of the specified {@code buffers}' readable
   * bytes. The new buffer's {@code readerIndex} and {@code writerIndex} are {@code 0} and the sum
   * of all buffers' {@code readableBytes} respectively.
   *
   * @throws IllegalArgumentException if the specified buffers' endianness are different from each
   *     other
   */
  public static ByteBuf copiedBuffer(ByteBuf... buffers) {
    switch (buffers.length) {
      case 0:
        return EMPTY_BUFFER;
      case 1:
        return copiedBuffer(buffers[0]);
    }

    // Merge the specified buffers into one buffer.
    ByteOrder order = null;
    int length = 0;
    for (ByteBuf b : buffers) {
      int bLen = b.readableBytes();
      if (bLen <= 0) {
        continue;
      }
      if (Integer.MAX_VALUE - length < bLen) {
        throw new IllegalArgumentException("The total length of the specified buffers is too big.");
      }
      length += bLen;
      if (order != null) {
        if (!order.equals(b.order())) {
          throw new IllegalArgumentException("inconsistent byte order");
        }
      } else {
        order = b.order();
      }
    }

    if (length == 0) {
      return EMPTY_BUFFER;
    }

    byte[] mergedArray = new byte[length];
    for (int i = 0, j = 0; i < buffers.length; i++) {
      ByteBuf b = buffers[i];
      int bLen = b.readableBytes();
      b.getBytes(b.readerIndex(), mergedArray, j, bLen);
      j += bLen;
    }

    return wrappedBuffer(mergedArray).order(order);
  }
Exemple #6
0
 /**
  * Convenience method for encoding a long into a byte stream.
  *
  * @param value
  * @param out
  * @throws IOException
  */
 public void write(long value) throws IOException {
   if (endian.equals(ByteOrder.BIG_ENDIAN)) {
     out.write((byte) ((value >> 56) & 0xff));
     out.write((byte) ((value >> 48) & 0xff));
     out.write((byte) ((value >> 40) & 0xff));
     out.write((byte) ((value >> 32) & 0xff));
     out.write((byte) ((value >> 24) & 0xff));
     out.write((byte) ((value >> 16) & 0xff));
     out.write((byte) ((value >> 8) & 0xff));
     out.write((byte) (value & 0xff));
   } else {
     out.write((byte) (value & 0xff));
     out.write((byte) ((value >> 8) & 0xff));
     out.write((byte) ((value >> 16) & 0xff));
     out.write((byte) ((value >> 24) & 0xff));
     out.write((byte) ((value >> 32) & 0xff));
     out.write((byte) ((value >> 40) & 0xff));
     out.write((byte) ((value >> 48) & 0xff));
     out.write((byte) ((value >> 56) & 0xff));
   }
 }
 /**
  * Creates a new composite buffer which wraps the readable bytes of the specified buffers without
  * copying them. A modification on the content of the specified buffers will be visible to the
  * returned buffer.
  *
  * @throws IllegalArgumentException if the specified buffers' endianness are different from each
  *     other
  */
 public static ChannelBuffer wrappedBuffer(ChannelBuffer... buffers) {
   switch (buffers.length) {
     case 0:
       break;
     case 1:
       if (buffers[0].readable()) {
         return wrappedBuffer(buffers[0]);
       }
       break;
     default:
       ByteOrder order = null;
       final List<ChannelBuffer> components = new ArrayList<ChannelBuffer>(buffers.length);
       for (ChannelBuffer c : buffers) {
         if (c == null) {
           break;
         }
         if (c.readable()) {
           if (order != null) {
             if (!order.equals(c.order())) {
               throw new IllegalArgumentException("inconsistent byte order");
             }
           } else {
             order = c.order();
           }
           if (c instanceof CompositeChannelBuffer) {
             // Expand nested composition.
             components.addAll(
                 ((CompositeChannelBuffer) c).decompose(c.readerIndex(), c.readableBytes()));
           } else {
             // An ordinary buffer (non-composite)
             components.add(c.slice());
           }
         }
       }
       return compositeBuffer(order, components);
   }
   return EMPTY_BUFFER;
 }
Exemple #8
0
 public static boolean isLittleEndian() {
   ByteOrder b = ByteOrder.nativeOrder();
   return b.equals(ByteOrder.LITTLE_ENDIAN);
 }