ByteBuffers.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * https://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */

  19. package org.apache.commons.io;

  20. import java.nio.ByteBuffer;
  21. import java.nio.ByteOrder;

  22. /**
  23.  * Manufactures {@link ByteBuffer} instances.
  24.  *
  25.  * @since 2.19.0
  26.  */
  27. public final class ByteBuffers {

  28.     /**
  29.      * Allocates a new byte buffer with little-endian byte order. The bytes of a multibyte value are ordered from least significant to most significant.
  30.      * <p>
  31.      * The new buffer's position is zero, the limit is its capacity, the mark is undefined, and each of element is initialized to zero. The new buffer has the
  32.      * given backing {@code array}, and its {@link ByteBuffer#arrayOffset() array offset} is zero.
  33.      * </p>
  34.      *
  35.      * @param array The array that will back the new byte buffer.
  36.      * @return The new byte buffer.
  37.      */
  38.     public static ByteBuffer littleEndian(final byte[] array) {
  39.         return littleEndian(ByteBuffer.wrap(array));
  40.     }

  41.     /**
  42.      * Sets the give buffer to little-endian.
  43.      *
  44.      * @param allocate The buffer to set to little-endian.
  45.      * @return the given buffer.
  46.      */
  47.     public static ByteBuffer littleEndian(final ByteBuffer allocate) {
  48.         return allocate.order(ByteOrder.LITTLE_ENDIAN);
  49.     }

  50.     /**
  51.      * Allocates a new byte buffer with little-endian byte order. The bytes of a multibyte value are ordered from least significant to most significant.
  52.      * <p>
  53.      * The new buffer's position is zero, the limit is its capacity, the mark is undefined, and each of element is initialized to zero. The new buffer has a
  54.      * {@link ByteBuffer#array() backing array}, and its {@link ByteBuffer#arrayOffset() array offset} is zero.
  55.      * </p>
  56.      *
  57.      * @param capacity The new buffer's capacity, in bytes.
  58.      * @return The new byte buffer.
  59.      * @throws IllegalArgumentException If the <code>capacity</code> is negative.
  60.      */
  61.     public static ByteBuffer littleEndian(final int capacity) {
  62.         return littleEndian(ByteBuffer.allocate(capacity));
  63.     }

  64.     private ByteBuffers() {
  65.         // empty, no instance.
  66.     }

  67. }