use of java.nio.ByteOrder in project buck by facebook.
the class ObjectPathsAbsolutifier method updateFileSizeTo.
private void updateFileSizeTo(int newSize) throws IOException {
ByteOrder order = buffer.order();
int position = buffer.position();
file.setLength(newSize);
remapBuffer();
buffer.order(order);
buffer.position(position);
}
use of java.nio.ByteOrder in project buck by facebook.
the class MachoMagicInfoUtils method getMachMagicInfo.
/**
* Reads 4 bytes from the given byte buffer from position 0 and produces the MachoMagicInfo object
* which describes basic information about Mach Object file.
* @param buffer Byte Buffer which holds bytes for the mach header magic number.
* @return MachoMagicInfo object.
* @throws IOException
*/
public static MachoMagicInfo getMachMagicInfo(ByteBuffer buffer) {
ByteOrder order = buffer.order();
UnsignedInteger magic = UnsignedInteger.fromIntBits(buffer.order(ByteOrder.BIG_ENDIAN).getInt());
buffer.order(order);
return new MachoMagicInfo(magic);
}
use of java.nio.ByteOrder in project jmonkeyengine by jMonkeyEngine.
the class PFMLoader method load.
private Image load(InputStream in, boolean needYFlip) throws IOException {
Format format = null;
String fmtStr = readString(in);
if (fmtStr.equals("PF")) {
format = Format.RGB32F;
} else if (fmtStr.equals("Pf")) {
format = Format.Luminance32F;
} else {
throw new IOException("File is not PFM format");
}
String sizeStr = readString(in);
int spaceIdx = sizeStr.indexOf(" ");
if (spaceIdx <= 0 || spaceIdx >= sizeStr.length() - 1)
throw new IOException("Invalid size syntax in PFM file");
int width = Integer.parseInt(sizeStr.substring(0, spaceIdx));
int height = Integer.parseInt(sizeStr.substring(spaceIdx + 1));
if (width <= 0 || height <= 0)
throw new IOException("Invalid size specified in PFM file");
String scaleStr = readString(in);
float scale = Float.parseFloat(scaleStr);
ByteOrder order = scale < 0 ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN;
boolean needEndienFlip = order != ByteOrder.nativeOrder();
// make sure all unneccessary stuff gets deleted from heap
// before allocating large amount of memory
System.gc();
int bytesPerPixel = format.getBitsPerPixel() / 8;
int scanLineBytes = bytesPerPixel * width;
ByteBuffer imageData = BufferUtils.createByteBuffer(width * height * bytesPerPixel);
byte[] scanline = new byte[width * bytesPerPixel];
for (int y = height - 1; y >= 0; y--) {
if (!needYFlip)
imageData.position(scanLineBytes * y);
int read = 0;
int off = 0;
do {
read = in.read(scanline, off, scanline.length - off);
off += read;
} while (read > 0);
if (needEndienFlip) {
flipScanline(scanline);
}
imageData.put(scanline);
}
imageData.rewind();
return new Image(format, width, height, imageData, null, ColorSpace.Linear);
}
use of java.nio.ByteOrder in project voltdb by VoltDB.
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 = new byte[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);
}
use of java.nio.ByteOrder in project jna by java-native-access.
the class Kernel32Util method isWideCharEnvironmentStringBlock.
/**
* <P>Attempts to determine whether the data block uses {@code wchar_t}
* instead of "plain old" {@code char}s. It does that by reading
* 2 bytes from the specified offset - the character value and its charset
* indicator - and examining them as follows:</P>
* <UL>
* <LI>
* If the charset indicator is non-zero then it is assumed to be
* a "plain old" {@code char}s data block. <B>Note:</B>
* the assumption is that the environment variable <U>name</U> (at
* least) is ASCII.
* </LI>
*
* <LI>
* Otherwise (i.e., zero charset indicator), it is assumed to be
* a {@code wchar_t}
* </LI>
* </UL>
* <B>Note:</B> the code takes into account the {@link ByteOrder} even though
* only {@link ByteOrder#LITTLE_ENDIAN} is the likely one
* @param lpszEnvironmentBlock The environment block as received from the
* <A HREF="https://msdn.microsoft.com/en-us/library/windows/desktop/ms683187(v=vs.85).aspx">GetEnvironmentStrings</A>
* function
* @param offset offset
* @return {@code true} if the block contains {@code wchar_t} instead of
* "plain old" {@code char}s
*/
public static boolean isWideCharEnvironmentStringBlock(Pointer lpszEnvironmentBlock, long offset) {
byte b0 = lpszEnvironmentBlock.getByte(offset);
byte b1 = lpszEnvironmentBlock.getByte(offset + 1L);
ByteOrder byteOrder = ByteOrder.nativeOrder();
if (ByteOrder.LITTLE_ENDIAN.equals(byteOrder)) {
return isWideCharEnvironmentStringBlock(b1);
} else {
return isWideCharEnvironmentStringBlock(b0);
}
}
Aggregations