use of java.nio.ByteOrder in project bitrafael_public by GENERALBYTESCOM.
the class Keccak256 method update.
public void update(ByteBuffer in) {
int inBytes = in.remaining();
if (inBytes <= 0)
return;
if (padded)
throw new IllegalStateException("Cannot update while padded");
int rateBits = this.rateBits;
if (// this could be implemented but would introduce considerable performance degradation - also, it's never technically possible.
(rateBits & 0x7) > 0)
throw new IllegalStateException("Cannot update while in bit-mode");
long[] state = this.state;
int rateBytes = rateBits >>> 3;
int rateBytesWord = rateBytes & 0x7;
if (rateBytesWord > 0) {
// logically must have space at this point
int c = 8 - rateBytesWord;
if (c > inBytes)
c = inBytes;
int i = rateBytes >>> 3;
long w = state[i];
rateBytes += c;
inBytes -= c;
rateBytesWord <<= 3;
c = rateBytesWord + (c << 3);
do {
w ^= (long) (in.get() & 0xff) << rateBytesWord;
rateBytesWord += 8;
} while (rateBytesWord < c);
state[i] = w;
if (inBytes > 0) {
this.rateBits = rateBytes << 3;
return;
}
}
int rateWords = rateBytes >>> 3;
int rateSizeWords = rateSizeBits >>> 6;
int inWords = inBytes >>> 3;
if (inWords > 0) {
ByteOrder order = in.order();
try {
in.order(ByteOrder.LITTLE_ENDIAN);
do {
if (rateWords >= rateSizeWords) {
Keccak256.keccak(state);
rateWords = 0;
}
int c = rateSizeWords - rateWords;
if (c > inWords)
c = inWords;
inWords -= c;
c += rateWords;
do {
state[rateWords] ^= in.getLong();
rateWords++;
} while (rateWords < c);
} while (inWords > 0);
} finally {
in.order(order);
}
inBytes &= 0x7;
if (inBytes <= 0) {
this.rateBits = rateWords << 6;
return;
}
}
if (rateWords >= rateSizeWords) {
Keccak256.keccak(state);
rateWords = 0;
}
long w = state[rateWords];
inBytes <<= 3;
int i = 0;
do {
w ^= (long) (in.get() & 0xff) << i;
i += 8;
} while (i < inBytes);
state[rateWords] = w;
this.rateBits = (rateWords << 6) | inBytes;
}
use of java.nio.ByteOrder in project cassandra by apache.
the class SafeMemoryWriter method resizeTo.
private void resizeTo(long newCapacity) {
if (newCapacity != capacity()) {
long position = length();
ByteOrder order = buffer.order();
SafeMemory oldBuffer = memory;
memory = this.memory.copy(newCapacity);
buffer = tailBuffer(memory);
int newPosition = (int) (position - tailOffset(memory));
buffer.position(newPosition);
buffer.order(order);
oldBuffer.free();
}
}
use of java.nio.ByteOrder in project uCrop by Yalantis.
the class ImageHeaderParser method parseExifSegment.
private static int parseExifSegment(RandomAccessReader segmentData) {
final int headerOffsetSize = JPEG_EXIF_SEGMENT_PREAMBLE.length();
short byteOrderIdentifier = segmentData.getInt16(headerOffsetSize);
final ByteOrder byteOrder;
if (byteOrderIdentifier == MOTOROLA_TIFF_MAGIC_NUMBER) {
byteOrder = ByteOrder.BIG_ENDIAN;
} else if (byteOrderIdentifier == INTEL_TIFF_MAGIC_NUMBER) {
byteOrder = ByteOrder.LITTLE_ENDIAN;
} else {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Unknown endianness = " + byteOrderIdentifier);
}
byteOrder = ByteOrder.BIG_ENDIAN;
}
segmentData.order(byteOrder);
int firstIfdOffset = segmentData.getInt32(headerOffsetSize + 4) + headerOffsetSize;
int tagCount = segmentData.getInt16(firstIfdOffset);
int tagOffset, tagType, formatCode, componentCount;
for (int i = 0; i < tagCount; i++) {
tagOffset = calcTagOffset(firstIfdOffset, i);
tagType = segmentData.getInt16(tagOffset);
// We only want orientation.
if (tagType != ORIENTATION_TAG_TYPE) {
continue;
}
formatCode = segmentData.getInt16(tagOffset + 2);
// 12 is max format code.
if (formatCode < 1 || formatCode > 12) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Got invalid format code = " + formatCode);
}
continue;
}
componentCount = segmentData.getInt32(tagOffset + 4);
if (componentCount < 0) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Negative tiff component count");
}
continue;
}
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Got tagIndex=" + i + " tagType=" + tagType + " formatCode=" + formatCode + " componentCount=" + componentCount);
}
final int byteCount = componentCount + BYTES_PER_FORMAT[formatCode];
if (byteCount > 4) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Got byte count > 4, not orientation, continuing, formatCode=" + formatCode);
}
continue;
}
final int tagValueOffset = tagOffset + 8;
if (tagValueOffset < 0 || tagValueOffset > segmentData.length()) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Illegal tagValueOffset=" + tagValueOffset + " tagType=" + tagType);
}
continue;
}
if (byteCount < 0 || tagValueOffset + byteCount > segmentData.length()) {
if (Log.isLoggable(TAG, Log.DEBUG)) {
Log.d(TAG, "Illegal number of bytes for TI tag data tagType=" + tagType);
}
continue;
}
// assume componentCount == 1 && fmtCode == 3
return segmentData.getInt16(tagValueOffset);
}
return -1;
}
use of java.nio.ByteOrder in project netty by netty.
the class ByteBufCopyBenchmark method setup.
@Setup
public void setup() {
final int requiredByteBufSize = alignedCopyByteBuf ? size : size + 1;
final int requiredByteBufferSize = alignedCopyByteBuffer ? size : size + 1;
byteBuffer = directByteBuffer ? ByteBuffer.allocateDirect(requiredByteBufferSize) : ByteBuffer.allocate(requiredByteBufferSize);
if (pooledByteBuf) {
buffer = directByteBuff ? PooledByteBufAllocator.DEFAULT.directBuffer(requiredByteBufSize, requiredByteBufSize) : PooledByteBufAllocator.DEFAULT.heapBuffer(requiredByteBufSize, requiredByteBufSize);
} else {
buffer = directByteBuff ? Unpooled.directBuffer(requiredByteBufSize, requiredByteBufSize) : Unpooled.buffer(requiredByteBufSize, requiredByteBufSize);
}
if (!alignedCopyByteBuffer) {
byteBuffer.position(1);
byteBuffer = byteBuffer.slice();
}
if (readonlyByteBuffer) {
byteBuffer = byteBuffer.asReadOnlyBuffer();
}
final ByteOrder byteBufferOrder;
if (!nativeOrderByteBuffer) {
byteBufferOrder = ByteOrder.LITTLE_ENDIAN == ByteOrder.nativeOrder() ? ByteOrder.BIG_ENDIAN : ByteOrder.LITTLE_ENDIAN;
} else {
byteBufferOrder = ByteOrder.nativeOrder();
}
byteBuffer.order(byteBufferOrder);
index = alignedCopyByteBuf ? 0 : 1;
}
use of java.nio.ByteOrder in project netty by netty.
the class Unpooled method copiedBuffer.
/**
* Creates a new buffer whose content is a merged copy of the specified
* {@code buffers}' readable bytes. The new buffer's {@code readerIndex}
* and {@code writerIndex} are {@code 0} and the sum of all buffers'
* {@code readableBytes} respectively.
*
* @throws IllegalArgumentException
* if the specified buffers' endianness are different from each
* other
*/
public static ByteBuf copiedBuffer(ByteBuf... buffers) {
switch(buffers.length) {
case 0:
return EMPTY_BUFFER;
case 1:
return copiedBuffer(buffers[0]);
}
// Merge the specified buffers into one buffer.
ByteOrder order = null;
int length = 0;
for (ByteBuf b : buffers) {
int bLen = b.readableBytes();
if (bLen <= 0) {
continue;
}
if (Integer.MAX_VALUE - length < bLen) {
throw new IllegalArgumentException("The total length of the specified buffers is too big.");
}
length += bLen;
if (order != null) {
if (!order.equals(b.order())) {
throw new IllegalArgumentException("inconsistent byte order");
}
} else {
order = b.order();
}
}
if (length == 0) {
return EMPTY_BUFFER;
}
byte[] mergedArray = PlatformDependent.allocateUninitializedArray(length);
for (int i = 0, j = 0; i < buffers.length; i++) {
ByteBuf b = buffers[i];
int bLen = b.readableBytes();
b.getBytes(b.readerIndex(), mergedArray, j, bLen);
j += bLen;
}
return wrappedBuffer(mergedArray).order(order);
}
Aggregations