src/java.base/share/classes/sun/security/provider/ByteArrayAccess.java

Print this page




  71     // implemented using a software trap and therefore very slow)
  72     private static final boolean bigEndian;
  73 
  74     private final static int byteArrayOfs = unsafe.arrayBaseOffset(byte[].class);
  75 
  76     static {
  77         boolean scaleOK = ((unsafe.arrayIndexScale(byte[].class) == 1)
  78             && (unsafe.arrayIndexScale(int[].class) == 4)
  79             && (unsafe.arrayIndexScale(long[].class) == 8)
  80             && ((byteArrayOfs & 3) == 0));
  81 
  82         ByteOrder byteOrder = ByteOrder.nativeOrder();
  83         littleEndianUnaligned =
  84             scaleOK && unaligned() && (byteOrder == ByteOrder.LITTLE_ENDIAN);
  85         bigEndian =
  86             scaleOK && (byteOrder == ByteOrder.BIG_ENDIAN);
  87     }
  88 
  89     // Return whether this platform supports full speed int/long memory access
  90     // at unaligned addresses.
  91     // This code was copied from java.nio.Bits because there is no equivalent
  92     // public API.
  93     private static boolean unaligned() {
  94         String arch = java.security.AccessController.doPrivileged
  95             (new sun.security.action.GetPropertyAction("os.arch", ""));
  96         return arch.equals("i386") || arch.equals("x86") || arch.equals("amd64")
  97             || arch.equals("x86_64");
  98     }
  99 
 100     /**
 101      * byte[] to int[] conversion, little endian byte order.
 102      */
 103     static void b2iLittle(byte[] in, int inOfs, int[] out, int outOfs, int len) {
 104         if ((inOfs < 0) || ((in.length - inOfs) < len) ||
 105             (outOfs < 0) || ((out.length - outOfs) < len/4)) {
 106             throw new ArrayIndexOutOfBoundsException();
 107         }
 108         if (littleEndianUnaligned) {
 109             inOfs += byteArrayOfs;
 110             len += inOfs;
 111             while (inOfs < len) {
 112                 out[outOfs++] = unsafe.getInt(in, (long)inOfs);
 113                 inOfs += 4;
 114             }
 115         } else if (bigEndian && ((inOfs & 3) == 0)) {
 116             inOfs += byteArrayOfs;
 117             len += inOfs;




  71     // implemented using a software trap and therefore very slow)
  72     private static final boolean bigEndian;
  73 
  74     private final static int byteArrayOfs = unsafe.arrayBaseOffset(byte[].class);
  75 
  76     static {
  77         boolean scaleOK = ((unsafe.arrayIndexScale(byte[].class) == 1)
  78             && (unsafe.arrayIndexScale(int[].class) == 4)
  79             && (unsafe.arrayIndexScale(long[].class) == 8)
  80             && ((byteArrayOfs & 3) == 0));
  81 
  82         ByteOrder byteOrder = ByteOrder.nativeOrder();
  83         littleEndianUnaligned =
  84             scaleOK && unaligned() && (byteOrder == ByteOrder.LITTLE_ENDIAN);
  85         bigEndian =
  86             scaleOK && (byteOrder == ByteOrder.BIG_ENDIAN);
  87     }
  88 
  89     // Return whether this platform supports full speed int/long memory access
  90     // at unaligned addresses.


  91     private static boolean unaligned() {
  92         return unsafe.unalignedAccess();



  93     }
  94 
  95     /**
  96      * byte[] to int[] conversion, little endian byte order.
  97      */
  98     static void b2iLittle(byte[] in, int inOfs, int[] out, int outOfs, int len) {
  99         if ((inOfs < 0) || ((in.length - inOfs) < len) ||
 100             (outOfs < 0) || ((out.length - outOfs) < len/4)) {
 101             throw new ArrayIndexOutOfBoundsException();
 102         }
 103         if (littleEndianUnaligned) {
 104             inOfs += byteArrayOfs;
 105             len += inOfs;
 106             while (inOfs < len) {
 107                 out[outOfs++] = unsafe.getInt(in, (long)inOfs);
 108                 inOfs += 4;
 109             }
 110         } else if (bigEndian && ((inOfs & 3) == 0)) {
 111             inOfs += byteArrayOfs;
 112             len += inOfs;