use of java.nio.ByteOrder in project disunity by ata4.
the class UnityGUID method read.
@Override
public void read(DataReader in) throws IOException {
// read GUID as big-endian
ByteOrder order = in.order();
in.order(ByteOrder.BIG_ENDIAN);
long guidMost = in.readLong();
long guidLeast = in.readLong();
in.order(order);
uuid = new UUID(guidMost, guidLeast);
}
use of java.nio.ByteOrder in project buck by facebook.
the class UUIDCommandUtils method createFromBuffer.
public static UUIDCommand createFromBuffer(ByteBuffer buffer) {
LoadCommandCommonFields fields = LoadCommandCommonFieldsUtils.createFromBuffer(buffer);
ByteOrder order = buffer.order();
buffer.order(ByteOrder.BIG_ENDIAN);
long high = buffer.getLong();
long low = buffer.getLong();
buffer.order(order);
UUID uuid = new UUID(high, low);
return UUIDCommand.of(fields, uuid);
}
use of java.nio.ByteOrder in project hazelcast by hazelcast.
the class AbstractXmlConfigHelper method parseSerialization.
protected SerializationConfig parseSerialization(final Node node) {
SerializationConfig serializationConfig = new SerializationConfig();
for (Node child : childElements(node)) {
final String name = cleanNodeName(child);
if ("portable-version".equals(name)) {
String value = getTextContent(child);
serializationConfig.setPortableVersion(getIntegerValue(name, value));
} else if ("check-class-def-errors".equals(name)) {
String value = getTextContent(child);
serializationConfig.setCheckClassDefErrors(getBooleanValue(value));
} else if ("use-native-byte-order".equals(name)) {
serializationConfig.setUseNativeByteOrder(getBooleanValue(getTextContent(child)));
} else if ("byte-order".equals(name)) {
String value = getTextContent(child);
ByteOrder byteOrder = null;
if (ByteOrder.BIG_ENDIAN.toString().equals(value)) {
byteOrder = ByteOrder.BIG_ENDIAN;
} else if (ByteOrder.LITTLE_ENDIAN.toString().equals(value)) {
byteOrder = ByteOrder.LITTLE_ENDIAN;
}
serializationConfig.setByteOrder(byteOrder != null ? byteOrder : ByteOrder.BIG_ENDIAN);
} else if ("enable-compression".equals(name)) {
serializationConfig.setEnableCompression(getBooleanValue(getTextContent(child)));
} else if ("enable-shared-object".equals(name)) {
serializationConfig.setEnableSharedObject(getBooleanValue(getTextContent(child)));
} else if ("allow-unsafe".equals(name)) {
serializationConfig.setAllowUnsafe(getBooleanValue(getTextContent(child)));
} else if ("data-serializable-factories".equals(name)) {
fillDataSerializableFactories(child, serializationConfig);
} else if ("portable-factories".equals(name)) {
fillPortableFactories(child, serializationConfig);
} else if ("serializers".equals(name)) {
fillSerializers(child, serializationConfig);
}
}
return serializationConfig;
}
use of java.nio.ByteOrder in project flink by apache.
the class EventSerializer method isEvent.
/**
* Identifies whether the given buffer encodes the given event.
*
* <p><strong>Pre-condition</strong>: This buffer must encode some event!</p>
*
* @param buffer the buffer to peak into
* @param eventClass the expected class of the event type
* @param classLoader the class loader to use for custom event classes
* @return whether the event class of the <tt>buffer</tt> matches the given <tt>eventClass</tt>
* @throws IOException
*/
private static boolean isEvent(ByteBuffer buffer, Class<?> eventClass, ClassLoader classLoader) throws IOException {
if (buffer.remaining() < 4) {
throw new IOException("Incomplete event");
}
final int bufferPos = buffer.position();
final ByteOrder bufferOrder = buffer.order();
buffer.order(ByteOrder.BIG_ENDIAN);
try {
int type = buffer.getInt();
switch(type) {
case END_OF_PARTITION_EVENT:
return eventClass.equals(EndOfPartitionEvent.class);
case CHECKPOINT_BARRIER_EVENT:
return eventClass.equals(CheckpointBarrier.class);
case END_OF_SUPERSTEP_EVENT:
return eventClass.equals(EndOfSuperstepEvent.class);
case CANCEL_CHECKPOINT_MARKER_EVENT:
return eventClass.equals(CancelCheckpointMarker.class);
case OTHER_EVENT:
try {
final DataInputDeserializer deserializer = new DataInputDeserializer(buffer);
final String className = deserializer.readUTF();
final Class<? extends AbstractEvent> clazz;
try {
clazz = classLoader.loadClass(className).asSubclass(AbstractEvent.class);
} catch (ClassNotFoundException e) {
throw new IOException("Could not load event class '" + className + "'.", e);
} catch (ClassCastException e) {
throw new IOException("The class '" + className + "' is not a valid subclass of '" + AbstractEvent.class.getName() + "'.", e);
}
return eventClass.equals(clazz);
} catch (Exception e) {
throw new IOException("Error while deserializing or instantiating event.", e);
}
default:
throw new IOException("Corrupt byte stream for event");
}
} finally {
buffer.order(bufferOrder);
// restore the original position in the buffer (recall: we only peak into it!)
buffer.position(bufferPos);
}
}
use of java.nio.ByteOrder in project jna by java-native-access.
the class Kernel32Util method readEnvironmentStringBlockEntry.
/**
* @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 within the block to look for the entry
* @param asWideChars If {@code true} then the block contains {@code wchar_t}
* instead of "plain old" {@code char}s
* @return A {@link String} containing the <code>name=value</code> pair or
* empty if reached end of block
* @see #isWideCharEnvironmentStringBlock
* @see #findEnvironmentStringBlockEntryEnd
*/
public static String readEnvironmentStringBlockEntry(Pointer lpszEnvironmentBlock, long offset, boolean asWideChars) {
long endOffset = findEnvironmentStringBlockEntryEnd(lpszEnvironmentBlock, offset, asWideChars);
int dataLen = (int) (endOffset - offset);
if (dataLen == 0) {
return "";
}
int charsLen = asWideChars ? (dataLen / 2) : dataLen;
char[] chars = new char[charsLen];
long curOffset = offset, stepSize = asWideChars ? 2L : 1L;
ByteOrder byteOrder = ByteOrder.nativeOrder();
for (int index = 0; index < chars.length; index++, curOffset += stepSize) {
byte b = lpszEnvironmentBlock.getByte(curOffset);
if (asWideChars) {
byte x = lpszEnvironmentBlock.getByte(curOffset + 1L);
if (ByteOrder.LITTLE_ENDIAN.equals(byteOrder)) {
chars[index] = (char) (((x << Byte.SIZE) & 0xFF00) | (b & 0x00FF));
} else {
// unlikely, but handle it
chars[index] = (char) (((b << Byte.SIZE) & 0xFF00) | (x & 0x00FF));
}
} else {
chars[index] = (char) (b & 0x00FF);
}
}
return new String(chars);
}
Aggregations