Home | History | Annotate | Download | only in nio
      1 /* Licensed to the Apache Software Foundation (ASF) under one or more
      2  * contributor license agreements.  See the NOTICE file distributed with
      3  * this work for additional information regarding copyright ownership.
      4  * The ASF licenses this file to You under the Apache License, Version 2.0
      5  * (the "License"); you may not use this file except in compliance with
      6  * the License.  You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package java.nio;
     18 
     19 /**
     20  * Defines byte order constants.
     21  */
     22 public final class ByteOrder {
     23     private static final ByteOrder NATIVE_ORDER;
     24 
     25     /**
     26      * This constant represents big endian.
     27      */
     28     public static final ByteOrder BIG_ENDIAN;
     29 
     30     /**
     31      * This constant represents little endian.
     32      */
     33     public static final ByteOrder LITTLE_ENDIAN;
     34 
     35     private static native boolean isLittleEndian();
     36 
     37     static {
     38         boolean isLittleEndian = isLittleEndian();
     39         BIG_ENDIAN = new ByteOrder("BIG_ENDIAN", isLittleEndian);
     40         LITTLE_ENDIAN = new ByteOrder("LITTLE_ENDIAN", !isLittleEndian);
     41         NATIVE_ORDER = isLittleEndian ? LITTLE_ENDIAN : BIG_ENDIAN;
     42     }
     43 
     44     private final String name;
     45 
     46     /**
     47      * This is the only thing that ByteOrder is really used for: to know whether we need to swap
     48      * bytes to get this order, given bytes in native order. (That is, this is the opposite of
     49      * the hypothetical "isNativeOrder".)
     50      * @hide - needed in libcore.io too.
     51      */
     52     public final boolean needsSwap;
     53 
     54     private ByteOrder(String name, boolean needsSwap) {
     55         this.name = name;
     56         this.needsSwap = needsSwap;
     57     }
     58 
     59     /**
     60      * Returns the current platform byte order.
     61      *
     62      * @return the byte order object, which is either LITTLE_ENDIAN or
     63      *         BIG_ENDIAN.
     64      */
     65     public static ByteOrder nativeOrder() {
     66         return NATIVE_ORDER;
     67     }
     68 
     69     /**
     70      * Returns a string that describes this object.
     71      *
     72      * @return "BIG_ENDIAN" for {@link #BIG_ENDIAN ByteOrder.BIG_ENDIAN}
     73      *         objects, "LITTLE_ENDIAN" for
     74      *         {@link #LITTLE_ENDIAN ByteOrder.LITTLE_ENDIAN} objects.
     75      */
     76     @Override
     77     public String toString() {
     78         return name;
     79     }
     80 }
     81