List of usage examples for com.google.common.primitives Shorts fromByteArray
@GwtIncompatible("doesn't work") public static short fromByteArray(byte[] bytes)
From source file:net.bither.xrandom.audio.AmplitudeData.java
public AmplitudeData(byte[] rawData) { if (rawData == null) { amplitude = 0;/* w w w. jav a2 s.com*/ return; } int step = rawData.length / Shorts.BYTES / sampleCount; int count = 0; double sum = 0; for (int i = 0; i < rawData.length - Shorts.BYTES; i += step) { byte[] bytes = new byte[Shorts.BYTES]; for (int j = 0; j < Shorts.BYTES; j++) { bytes[j] = rawData[i + j]; } short s = Shorts.fromByteArray(bytes); sum += s * s; count++; } amplitude = (int) Math.sqrt(sum / count); }
From source file:com.yandex.yoctodb.util.UnsignedByteArrays.java
public static short toShort(@NotNull final UnsignedByteArray bytes) { if (bytes.length() != Shorts.BYTES) throw new IllegalArgumentException("Wrong length"); return (short) (Shorts.fromByteArray(bytes.data) ^ Short.MIN_VALUE); }
From source file:org.apache.directory.server.dhcp.options.ShortOption.java
@Nonnegative public int getShortValue() { return Shorts.fromByteArray(getData()) & 0xFFFF; }
From source file:org.mitre.jet.ebts.records.BinaryHeaderImageRecord.java
/** * Returns the value for a field as a string. * //ww w.j ava 2 s . c om * @param field FieldOccurrence Number * @return String value for the field */ @NotNull public String getFieldValue(final int field) { if (field >= headerFormat.length + 1) { throw new IllegalArgumentException("Field not found in record"); } if (field == headerFormat.length + 1) { return "<BinaryImageData>"; } else { final byte[] value = fields.get(field).getData(); if (value.length == 1) { return Byte.toString(value[0]); } else if (value.length == 2) { return Short.toString(Shorts.fromByteArray(value)); } else if (value.length == 4) { return Integer.toString(Ints.fromByteArray(value)); } else if (value.length == 6) { final StringBuilder stringOut = new StringBuilder(); for (int i = 0; i < 6; i++) { stringOut.append((int) value[i]); stringOut.append(","); } return stringOut.toString(); } else { return ""; } } }
From source file:co.paralleluniverse.galaxy.berkeleydb.BerkeleyDB.java
@Override public short casOwner(long id, short oldNode, short newNode) { final DatabaseEntry key = new DatabaseEntry(Longs.toByteArray(id)); final DatabaseEntry value = new DatabaseEntry(); final Transaction txn = env.beginTransaction(null, null); try {//from w w w.ja v a2s . co m OperationStatus status; value.setData(Shorts.toByteArray(newNode)); if (oldNode < 0) { status = ownerDirectory.putNoOverwrite(txn, key, value); if (status == OperationStatus.SUCCESS) { LOG.debug("CAS owner succeeded."); txn.commit(); return newNode; } } status = ownerDirectory.get(txn, key, value, LockMode.RMW); if (status == OperationStatus.SUCCESS) { final short curOldNode = Shorts.fromByteArray(value.getData()); if (LOG.isDebugEnabled()) LOG.debug("CAS owner of {}: current old node: {} wanted old node: {}", new Object[] { hex(id), curOldNode, oldNode }); if (oldNode != curOldNode) { assert curOldNode >= 0; LOG.debug("CAS owner failed."); txn.commit(); return curOldNode; } LOG.debug("CAS owner succeeded."); value.setData(Shorts.toByteArray(newNode)); ownerDirectory.put(txn, key, value); txn.commit(); return newNode; } else if (status == OperationStatus.NOTFOUND) { LOG.debug("CAS owner failed."); txn.commit(); return (short) -1; } LOG.debug("Bad status: {}", status); throw new AssertionError(); } catch (Exception e) { LOG.error("Exception during DB operation. Aborting transaction.", e); txn.abort(); throw Throwables.propagate(e); } }
From source file:com.liaison.javabasics.serialization.BytesUtil.java
/** * TODO// ww w . j av a 2 s .c o m * @param bytes TODO * @return TODO */ public static Short toShort(final byte[] bytes) { // TODO: determine constant values programmatically, or make them constant if (bytes != null && bytes.length >= 2) { return Short.valueOf(Shorts.fromByteArray(bytes)); } return null; }
From source file:org.mitre.jet.ebts.EbtsUtils.java
public static int byteArrayToInt(final byte[] bytes) { if (bytes.length == 4) { return Ints.fromByteArray(bytes); } else if (bytes.length == 2) { return Shorts.fromByteArray(bytes); } else if (bytes.length == 1) { return bytes[0] & 0xff; } else {/* w ww. j a va2 s .com*/ throw new InputMismatchException("invalid data length of " + bytes.length); } }
From source file:co.paralleluniverse.galaxy.berkeleydb.BerkeleyDB.java
@Override public short findAllocation(long ref) { final DatabaseEntry key = new DatabaseEntry(); final DatabaseEntry data = new DatabaseEntry(); try (Cursor cursor = allocationDirectory.openCursor(null, CursorConfig.DEFAULT)) { OperationStatus retVal = cursor.getSearchKeyRange(key, data, null); if (retVal == OperationStatus.SUCCESS) { ownerDirectory.put(null, key, SERVER); return Shorts.fromByteArray(data.getData()); } else if (retVal == OperationStatus.NOTFOUND) return (short) -1; throw new AssertionError(); }/*from w ww .j a v a 2 s. c o m*/ }
From source file:org.mitre.jet.ebts.EbtsParser.java
/** * Type7 parser.//w w w .j a va 2 s .c o m * * @param recordType the record type * @param bb the bb * @return the logical record */ private static LogicalRecord parseType7(final int recordType, final ByteBuffer bb) {/*?|TeamCodeReview|cfortner|c10|?*/ //Additional fields may exist in the 'image data' of the type 7 final int[] header = new int[] { 4, 1, 1, 6, 1, 2, 2, 1 }; final BinaryHeaderImageRecord record = new BinaryHeaderImageRecord(recordType, header); //No data remains if (bb.capacity() == 0) { return record; } final byte[] len = new byte[4]; bb.get(len); record.setField(1, new Field(convertBinaryFieldData(len), ParseContents.FALSE)); //Get the idc final byte[] idc = { bb.get() }; record.setField(2, new Field(convertBinaryFieldData(idc), ParseContents.FALSE)); //Get the impression type final byte[] imp = { bb.get() }; record.setField(3, new Field(convertBinaryFieldData(imp), ParseContents.FALSE)); //Get the fingerprint position final byte[] fgp = new byte[6]; bb.get(fgp); record.setField(4, new Field(convertBinaryFieldData(fgp), ParseContents.FALSE)); final byte[] isr = { bb.get() }; record.setField(5, new Field(convertBinaryFieldData(isr), ParseContents.FALSE)); final byte[] hll = new byte[2]; bb.get(hll); record.setField(6, new Field(convertBinaryFieldData(hll), ParseContents.FALSE)); final byte[] vll = new byte[2]; bb.get(vll); record.setField(7, new Field(convertBinaryFieldData(vll), ParseContents.FALSE)); final byte[] alg = { bb.get() }; record.setField(8, new Field(convertBinaryFieldData(alg), ParseContents.FALSE)); final int remainingDataPosition = bb.position(); //Examine the mimetype of the remaining data byte[] imageData = new byte[Ints.fromByteArray(len) - 18]; bb.get(imageData); final String ext = EbtsUtils.getMimeExtension(imageData); if (IMAGE_MIME_EXTENSIONS.contains(ext)) { log.debug("Found mime-type ext of remaining data to be: {}", ext); record.setField(9, new Field(imageData, ParseContents.FALSE)); } else { log.debug("Ignoring mime-type ext of {} and searching for mimetype based on CGA", ext); bb.position(remainingDataPosition); //TODO: Add Length Checks //If CGA is provided, we hunt for the header as it may not be at the beginning of the remaining data (Thanks CBEFF) if (alg[0] != Byte.valueOf("0")) { int imageLocation = -1; if (alg[0] == Byte.valueOf("1")) { imageLocation = ImageUtils.getWsqImagePosition(bb); log.debug("Found WSQ at byte:{}", imageLocation); } else if (alg[0] == Byte.valueOf("2")) { imageLocation = ImageUtils.getJpgImagePosition(bb); log.debug("Found WSQ at byte:{}", imageLocation); } else if (alg[0] == Byte.valueOf("4") || alg[0] == Byte.valueOf("5")) { imageLocation = ImageUtils.getJp2ImagePosition(bb); log.debug("Found JP2 at byte:{}", imageLocation); } //PNG else if (alg[0] == Byte.valueOf("6")) { imageLocation = ImageUtils.getPngImagePosition(bb); log.debug("Found PNG at byte:{}", imageLocation); } if (imageLocation != -1) { bb.position(imageLocation); imageData = new byte[Ints.fromByteArray(len) - imageLocation]; bb.get(imageData); record.setField(9, new Field(imageData, ParseContents.FALSE)); } } else { //Assume raw //TODO: Validate lengths final int hllInt = Shorts.fromByteArray(hll); final int vllInt = Shorts.fromByteArray(vll); final int imageLength = hllInt * vllInt; log.debug("image length = {}", hllInt, vllInt); imageData = new byte[imageLength]; bb.position(Ints.fromByteArray(len) - imageLength); bb.get(imageData); record.setField(9, new Field(imageData, ParseContents.FALSE)); } } if (log.isDebugEnabled()) { for (final Map.Entry<Integer, Field> entry : record.getFields().entrySet()) { if (entry.getKey() != 9) { log.debug("Parsed Field: {} Data:{}", entry.getKey(), entry.getValue().toString()); } } } return record; }
From source file:co.paralleluniverse.galaxy.berkeleydb.BerkeleyDB.java
public void printOwners(java.io.PrintStream ps) { ps.println("OWNERS"); ps.println("======"); final DatabaseEntry key = new DatabaseEntry(); final DatabaseEntry value = new DatabaseEntry(); try (Cursor cursor = ownerDirectory.openCursor(null, CursorConfig.DEFAULT)) { while (cursor.getNext(key, value, LockMode.DEFAULT) == OperationStatus.SUCCESS) { long id = Longs.fromByteArray(key.getData()); short owner = Shorts.fromByteArray(value.getData()); ps.println("Id : " + hex(id) + " owner: " + owner + ""); }/*from w ww . j a va 2s . co m*/ } }