/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* Optimized methods for converting between byte[] and int[]/long[], both for
* big endian and little endian byte orders.
*
* Currently, it includes a default code path plus two optimized code paths.
* architectures (that only support correctly aligned access), such as SPARC.
* These are the only platforms we currently support, but other optimized
* variants could be added as needed.
*
* NOTE that because this code performs unchecked direct memory access, it
* MUST be restricted to trusted code. It is imperative that the caller protects
* against out of bounds memory access by performing the necessary bounds
* checks before calling methods in this class.
*
* This class may also be helpful in improving the performance of the
* crypto code in the SunJCE provider. However, for now it is only accessible by
* the message digest implementation in the SUN provider.
*
* @since 1.6
* @author Andreas Sterbenz
*/
final class ByteArrayAccess {
private ByteArrayAccess() {
// empty
}
// whether to use the optimized path for little endian platforms that
// support full speed unaligned memory access.
private static final boolean littleEndianUnaligned;
// whether to use the optimzied path for big endian platforms that
// support only correctly aligned full speed memory access.
// (Note that on SPARC unaligned memory access is possible, but it is
// implemented using a software trap and therefore very slow)
private static final boolean bigEndian;
static {
}
// at unaligned addresses.
// This code was copied from java.nio.Bits because there is no equivalent
// public API.
private static boolean unaligned() {
}
/**
* byte[] to int[] conversion, little endian byte order.
*/
if (littleEndianUnaligned) {
inOfs += byteArrayOfs;
inOfs += 4;
}
inOfs += byteArrayOfs;
inOfs += 4;
}
} else {
inOfs += 4;
}
}
}
// Special optimization of b2iLittle(in, inOfs, out, 0, 64)
if (littleEndianUnaligned) {
inOfs += byteArrayOfs;
inOfs += byteArrayOfs;
} else {
}
}
/**
* int[] to byte[] conversion, little endian byte order.
*/
if (littleEndianUnaligned) {
outOfs += byteArrayOfs;
outOfs += 4;
}
outOfs += byteArrayOfs;
outOfs += 4;
}
} else {
}
}
}
// Store one 32-bit value into out[outOfs..outOfs+3] in little endian order.
if (littleEndianUnaligned) {
} else {
}
}
/**
* byte[] to int[] conversion, big endian byte order.
*/
if (littleEndianUnaligned) {
inOfs += byteArrayOfs;
inOfs += 4;
}
inOfs += byteArrayOfs;
inOfs += 4;
}
} else {
inOfs += 4;
}
}
}
// Special optimization of b2iBig(in, inOfs, out, 0, 64)
if (littleEndianUnaligned) {
inOfs += byteArrayOfs;
inOfs += byteArrayOfs;
} else {
}
}
/**
* int[] to byte[] conversion, big endian byte order.
*/
if (littleEndianUnaligned) {
outOfs += byteArrayOfs;
outOfs += 4;
}
outOfs += byteArrayOfs;
outOfs += 4;
}
} else {
}
}
}
// Store one 32-bit value into out[outOfs..outOfs+3] in big endian order.
if (littleEndianUnaligned) {
} else {
}
}
/**
* byte[] to long[] conversion, big endian byte order.
*/
if (littleEndianUnaligned) {
inOfs += byteArrayOfs;
inOfs += 8;
}
// In the current HotSpot memory layout, the first element of a
// byte[] is only 32-bit aligned, not 64-bit.
// That means we could use getLong() only for offset 4, 12, etc.,
// which would rarely occur in practice. Instead, we use an
// optimization that uses getInt() so that it works for offset 0.
inOfs += byteArrayOfs;
inOfs += 8;
}
} else {
inOfs += 4;
inOfs += 4;
}
}
}
// Special optimization of b2lBig(in, inOfs, out, 0, 128)
if (littleEndianUnaligned) {
inOfs += byteArrayOfs;
} else {
// no optimization for big endian, see comments in b2lBig
}
}
/**
* long[] to byte[] conversion, big endian byte order.
*/
}
}
}