View Javadoc
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   * http://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  
20  package org.apache.commons.io;
21  
22  import java.nio.ByteBuffer;
23  import java.nio.ByteOrder;
24  
25  /**
26   * Manufactures {@link ByteBuffer} instances.
27   *
28   * @since 2.19.0
29   */
30  public final class ByteBuffers {
31  
32      /**
33       * Allocates a new byte buffer with little-endian byte order. The bytes of a multibyte value are ordered from least significant to most significant.
34       * <p>
35       * 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
36       * given backing {@code array}, and its {@link ByteBuffer#arrayOffset() array offset} is zero.
37       * </p>
38       *
39       * @param array The array that will back the new byte buffer.
40       * @return The new byte buffer.
41       */
42      public static ByteBuffer littleEndian(final byte[] array) {
43          return littleEndian(ByteBuffer.wrap(array));
44      }
45  
46      /**
47       * Sets the give buffer to little-endian.
48       *
49       * @param allocate The buffer to set to little-endian.
50       * @return the given buffer.
51       */
52      public static ByteBuffer littleEndian(final ByteBuffer allocate) {
53          return allocate.order(ByteOrder.LITTLE_ENDIAN);
54      }
55  
56      /**
57       * Allocates a new byte buffer with little-endian byte order. The bytes of a multibyte value are ordered from least significant to most significant.
58       * <p>
59       * 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
60       * {@link ByteBuffer#array() backing array}, and its {@link ByteBuffer#arrayOffset() array offset} is zero.
61       * </p>
62       *
63       * @param capacity The new buffer's capacity, in bytes.
64       * @return The new byte buffer.
65       * @throws IllegalArgumentException If the <code>capacity</code> is negative.
66       */
67      public static ByteBuffer littleEndian(final int capacity) {
68          return littleEndian(ByteBuffer.allocate(capacity));
69      }
70  
71      private ByteBuffers() {
72          // empty, no instance.
73      }
74  
75  }