List of usage examples for java.nio ByteBuffer order
Endianness order
To view the source code for java.nio ByteBuffer order.
Click Source Link
From source file:edu.umn.msi.tropix.proteomics.conversion.impl.ConversionUtils.java
public static double[] extractDoublesLittleEndian(final byte[] bytes, final boolean is64Bit) { double[] doubles; ByteBuffer byteBuffer = ByteBuffer.wrap(bytes); byteBuffer.order(ByteOrder.LITTLE_ENDIAN); if (is64Bit) { doubles = new double[bytes.length / 8]; for (int i = 0; i < doubles.length; i++) { doubles[i] = byteBuffer.getDouble(); }//w w w . j av a 2s . c om } else { doubles = new double[bytes.length / 4]; for (int i = 0; i < doubles.length; i++) { doubles[i] = byteBuffer.getFloat(); } } return doubles; }
From source file:com.github.woz_dialog.ros_woz_dialog_project.TTSHTTPClient.java
private static byte[] addWavHeader(byte[] bytes) throws IOException { ByteBuffer bufferWithHeader = ByteBuffer.allocate(bytes.length + 44); bufferWithHeader.order(ByteOrder.LITTLE_ENDIAN); bufferWithHeader.put("RIFF".getBytes()); bufferWithHeader.putInt(bytes.length + 36); bufferWithHeader.put("WAVE".getBytes()); bufferWithHeader.put("fmt ".getBytes()); bufferWithHeader.putInt(16);/*from w w w. ja v a 2s. co m*/ bufferWithHeader.putShort((short) 1); bufferWithHeader.putShort((short) 1); bufferWithHeader.putInt(16000); bufferWithHeader.putInt(32000); bufferWithHeader.putShort((short) 2); bufferWithHeader.putShort((short) 16); bufferWithHeader.put("data".getBytes()); bufferWithHeader.putInt(bytes.length); bufferWithHeader.put(bytes); return bufferWithHeader.array(); }
From source file:au.org.ala.layers.grid.GridCacheBuilder.java
private static void writeGroupGRI(File file, ArrayList<Grid> group) { Grid g = group.get(0);/* ww w. j a va 2 s. c o m*/ RandomAccessFile[] raf = new RandomAccessFile[group.size()]; RandomAccessFile output = null; try { output = new RandomAccessFile(file, "rw"); for (int i = 0; i < group.size(); i++) { raf[i] = new RandomAccessFile(group.get(i).filename + ".gri", "r"); } int length = g.ncols * g.nrows; int size = 4; byte[] b = new byte[size * group.size() * g.ncols]; float noDataValue = Float.MAX_VALUE * -1; byte[] bi = new byte[g.ncols * 8]; float[][] rows = new float[group.size()][g.ncols]; for (int i = 0; i < g.nrows; i++) { //read for (int j = 0; j < raf.length; j++) { nextRowOfFloats(rows[j], group.get(j).datatype, group.get(j).byteorderLSB, g.ncols, raf[j], bi, (float) g.nodatavalue); } //write ByteBuffer bb = ByteBuffer.wrap(b); bb.order(ByteOrder.LITTLE_ENDIAN); for (int k = 0; k < g.ncols; k++) { for (int j = 0; j < raf.length; j++) { //float f = getNextValue(raf[j], group.get(j)); float f = rows[j][k]; if (Float.isNaN(f)) { bb.putFloat(noDataValue); } else { bb.putFloat(f); } } } output.write(b); } } catch (IOException e) { logger.error(e.getMessage(), e); } finally { if (output != null) { try { output.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } for (int i = 0; i < raf.length; i++) { if (raf[i] != null) { try { raf[i].close(); } catch (Exception e) { logger.error(e.getMessage(), e); } } } } }
From source file:au.org.ala.layers.grid.GridCacheBuilder.java
static void nextRowOfFloats(float[] row, String datatype, boolean byteOrderLSB, int ncols, RandomAccessFile raf, byte[] b, float noDataValue) throws IOException { int size = 4; if (datatype.charAt(0) == 'U') { size = 1;//from w ww. jav a2s . c om } else if (datatype.charAt(0) == 'B') { size = 1; } else if (datatype.charAt(0) == 'S') { size = 2; } else if (datatype.charAt(0) == 'I') { size = 4; } else if (datatype.charAt(0) == 'L') { size = 8; } else if (datatype.charAt(0) == 'F') { size = 4; } else if (datatype.charAt(0) == 'D') { size = 8; } raf.read(b, 0, size * ncols); ByteBuffer bb = ByteBuffer.wrap(b); if (byteOrderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); } else { bb.order(ByteOrder.BIG_ENDIAN); } int i; int length = ncols; if (datatype.charAt(0) == 'U') { for (i = 0; i < length; i++) { float ret = bb.get(); if (ret < 0) { ret += 256; } row[i] = ret; } } else if (datatype.charAt(0) == 'B') { for (i = 0; i < length; i++) { row[i] = (float) bb.get(); } } else if (datatype.charAt(0) == 'S') { for (i = 0; i < length; i++) { row[i] = (float) bb.getShort(); } } else if (datatype.charAt(0) == 'I') { for (i = 0; i < length; i++) { row[i] = (float) bb.getInt(); } } else if (datatype.charAt(0) == 'L') { for (i = 0; i < length; i++) { row[i] = (float) bb.getLong(); } } else if (datatype.charAt(0) == 'F') { for (i = 0; i < length; i++) { row[i] = (float) bb.getFloat(); } } else if (datatype.charAt(0) == 'D') { for (i = 0; i < length; i++) { row[i] = (float) bb.getDouble(); } } else { logger.info("UNKNOWN TYPE: " + datatype); } for (i = 0; i < length; i++) { if (row[i] == noDataValue) { row[i] = Float.NaN; } } }
From source file:org.energy_home.jemma.javagal.layers.data.implementations.Utils.DataManipulation.java
/** * Converts a {@code long} to a {@code byte[]}. A long is composed of eight * bytes. Writes eight bytes containing the given long value, in the current * byte order. Numbering them from 0 (the most important) to 7 (the least * important), the resulting array will have all them placed in the same * position./*from w w w . ja va 2 s .c om*/ * * @return the converted list. * @param x * the long to convert * @return the resulting array. */ public static byte[] longToBytes(long x) { ByteBuffer buffer = ByteBuffer.allocate(8); buffer.order(ByteOrder.BIG_ENDIAN); buffer.putLong(x); return buffer.array(); }
From source file:org.energy_home.jemma.javagal.layers.data.implementations.Utils.DataManipulation.java
/** * Converts an {@code int} to a {@code byte[]}. An int is composed of four * bytes. Writes four bytes containing the given int value, in the current * byte order. Numbering them from 0 (the most important) to 5 (the least * important), the resulting array will have all them placed in the same * position./*w ww.ja va 2 s . co m*/ * * @param x * the int to convert. * @return the resulting array. */ public static byte[] intToBytes(int x) { ByteBuffer buffer = ByteBuffer.allocate(4); buffer.order(ByteOrder.BIG_ENDIAN); buffer.putInt(x); return buffer.array(); }
From source file:au.org.ala.layers.grid.GridCacheBuilder.java
static public float getNextValue(RandomAccessFile raf, Grid g) { float f = Float.NaN; try {//from w w w. j ava2 s . c o m int size; byte[] b; if (g.datatype.charAt(0) == 'B') {//"BYTE")) { f = raf.readByte(); } else if (g.datatype.charAt(0) == 'U') {//equalsIgnoreCase("UBYTE")) { f = raf.readByte(); if (f < 0) { f += 256; } } else if (g.datatype.charAt(0) == 'S') {//equalsIgnoreCase("SHORT")) { size = 2; b = new byte[size]; raf.read(b); if (g.byteorderLSB) { f = (short) (((0xFF & b[1]) << 8) | (b[0] & 0xFF)); } else { f = (short) (((0xFF & b[0]) << 8) | (b[1] & 0xFF)); } } else if (g.datatype.charAt(0) == 'I') {//equalsIgnoreCase("INT")) { size = 4; b = new byte[size]; raf.read(b); if (g.byteorderLSB) { f = ((0xFF & b[3]) << 24) | ((0xFF & b[2]) << 16) + ((0xFF & b[1]) << 8) + (b[0] & 0xFF); } else { f = ((0xFF & b[0]) << 24) | ((0xFF & b[1]) << 16) + ((0xFF & b[2]) << 8) + ((0xFF & b[3]) & 0xFF); } } else if (g.datatype.charAt(0) == 'L') {//equalsIgnoreCase("LONG")) { size = 8; b = new byte[size]; raf.read(b); if (g.byteorderLSB) { f = ((long) (0xFF & b[7]) << 56) + ((long) (0xFF & b[6]) << 48) + ((long) (0xFF & b[5]) << 40) + ((long) (0xFF & b[4]) << 32) + ((long) (0xFF & b[3]) << 24) + ((long) (0xFF & b[2]) << 16) + ((long) (0xFF & b[1]) << 8) + (0xFF & b[0]); } else { f = ((long) (0xFF & b[0]) << 56) + ((long) (0xFF & b[1]) << 48) + ((long) (0xFF & b[2]) << 40) + ((long) (0xFF & b[3]) << 32) + ((long) (0xFF & b[4]) << 24) + ((long) (0xFF & b[5]) << 16) + ((long) (0xFF & b[6]) << 8) + (0xFF & b[7]); } } else if (g.datatype.charAt(0) == 'F') {//.equalsIgnoreCase("FLOAT")) { size = 4; b = new byte[size]; raf.read(b); ByteBuffer bb = ByteBuffer.wrap(b); if (g.byteorderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); } f = bb.getFloat(); } else if (g.datatype.charAt(0) == 'D') {//.equalsIgnoreCase("DOUBLE")) { size = 8; b = new byte[8]; raf.read(b); ByteBuffer bb = ByteBuffer.wrap(b); if (g.byteorderLSB) { bb.order(ByteOrder.LITTLE_ENDIAN); } f = (float) bb.getDouble(); } //replace not a number if ((float) f == (float) g.nodatavalue) { f = Float.NaN; } } catch (Exception e) { } return f; }
From source file:com.healthmarketscience.jackcess.impl.PageChannel.java
/** * @return a duplicate of the current buffer narrowed to the given position * and limit. mark will be set at the current position. *//*from w w w. jav a 2 s .c om*/ public static ByteBuffer narrowBuffer(ByteBuffer buffer, int position, int limit) { return (ByteBuffer) buffer.duplicate().order(buffer.order()).clear().limit(limit).position(position).mark(); }
From source file:com.github.nlloyd.hornofmongo.util.BSONizer.java
@SuppressWarnings("deprecation") private static Object convertScriptableMongoToBSON(ScriptableMongoObject jsMongoObj, boolean isJsObj, String dateFormat) {//from ww w . j a v a2 s. co m Object bsonObject = null; if (jsMongoObj instanceof ObjectId) { bsonObject = ((ObjectId) jsMongoObj).getRealObjectId(); } else if (jsMongoObj instanceof BinData) { BinData binData = (BinData) jsMongoObj; byte type = new Integer(binData.getType()).byteValue(); byte[] data = binData.getDataBytes(); if (type == BSON.B_UUID) { ByteBuffer dataBuffer = ByteBuffer.wrap(data); // mongodb wire protocol is little endian dataBuffer.order(ByteOrder.LITTLE_ENDIAN); long mostSigBits = dataBuffer.getLong(); long leastSigBits = dataBuffer.getLong(); bsonObject = new UUID(mostSigBits, leastSigBits); } else bsonObject = new org.bson.types.Binary(type, data); } else if (jsMongoObj instanceof MinKey) { bsonObject = new org.bson.types.MinKey(); } else if (jsMongoObj instanceof MaxKey) { bsonObject = new org.bson.types.MaxKey(); } else if (jsMongoObj instanceof NumberInt) { bsonObject = Integer.valueOf(((NumberInt) jsMongoObj).getRealInt()); } else if (jsMongoObj instanceof NumberLong) { bsonObject = Long.valueOf(((NumberLong) jsMongoObj).getRealLong()); } else if (jsMongoObj instanceof DBRef) { DBRef jsRef = (DBRef) jsMongoObj; Object id = convertJStoBSON(jsRef.getId(), isJsObj, dateFormat); bsonObject = new com.mongodb.DBRef(jsRef.getNs(), id); } else if (jsMongoObj instanceof Timestamp) { bsonObject = convertTimestampToBSONTimestamp((Timestamp) jsMongoObj); } return bsonObject; }
From source file:org.cocos2dx.lib.Cocos2dxBitmap.java
private static byte[] getPixels(Bitmap bitmap) { if (bitmap != null) { byte[] pixels = new byte[bitmap.getWidth() * bitmap.getHeight() * 4]; ByteBuffer buf = ByteBuffer.wrap(pixels); buf.order(ByteOrder.nativeOrder()); bitmap.copyPixelsToBuffer(buf);/* w w w. j a v a2 s .co m*/ return pixels; } return null; }