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.harvard.iq.dvn.unf.Base64Encoding.java
/** * * @param digest byte array for encoding in base 64, * @param chngByteOrd boolean indicating if to change byte order * @return String the encoded base64 of digest *//*from w w w . j a v a 2s. c o m*/ public static String tobase64(byte[] digest, boolean chngByteOrd) { byte[] tobase64 = null; ByteOrder local = ByteOrder.nativeOrder(); String ordbyte = local.toString(); mLog.finer("Native byte order is: " + ordbyte); ByteBuffer btstream = ByteBuffer.wrap(digest); btstream.order(ByteOrder.BIG_ENDIAN); byte[] revdigest = null; if (chngByteOrd) { revdigest = changeByteOrder(digest, local); } if (revdigest != null) { btstream.put(revdigest); } else { btstream.put(digest); } tobase64 = Base64.encodeBase64(btstream.array()); return new String(tobase64); }
From source file:Main.java
public static long getCommentLength(final FileChannel fileChannel) throws IOException { // End of central directory record (EOCD) // Offset Bytes Description[23] // 0 4 End of central directory signature = 0x06054b50 // 4 2 Number of this disk // 6 2 Disk where central directory starts // 8 2 Number of central directory records on this disk // 10 2 Total number of central directory records // 12 4 Size of central directory (bytes) // 16 4 Offset of start of central directory, relative to start of archive // 20 2 Comment length (n) // 22 n Comment // For a zip with no archive comment, the // end-of-central-directory record will be 22 bytes long, so // we expect to find the EOCD marker 22 bytes from the end. final long archiveSize = fileChannel.size(); if (archiveSize < ZIP_EOCD_REC_MIN_SIZE) { throw new IOException("APK too small for ZIP End of Central Directory (EOCD) record"); }//from ww w .ja v a 2 s.c o m // ZIP End of Central Directory (EOCD) record is located at the very end of the ZIP archive. // The record can be identified by its 4-byte signature/magic which is located at the very // beginning of the record. A complication is that the record is variable-length because of // the comment field. // The algorithm for locating the ZIP EOCD record is as follows. We search backwards from // end of the buffer for the EOCD record signature. Whenever we find a signature, we check // the candidate record's comment length is such that the remainder of the record takes up // exactly the remaining bytes in the buffer. The search is bounded because the maximum // size of the comment field is 65535 bytes because the field is an unsigned 16-bit number. final long maxCommentLength = Math.min(archiveSize - ZIP_EOCD_REC_MIN_SIZE, UINT16_MAX_VALUE); final long eocdWithEmptyCommentStartPosition = archiveSize - ZIP_EOCD_REC_MIN_SIZE; for (int expectedCommentLength = 0; expectedCommentLength <= maxCommentLength; expectedCommentLength++) { final long eocdStartPos = eocdWithEmptyCommentStartPosition - expectedCommentLength; final ByteBuffer byteBuffer = ByteBuffer.allocate(4); fileChannel.position(eocdStartPos); fileChannel.read(byteBuffer); byteBuffer.order(ByteOrder.LITTLE_ENDIAN); if (byteBuffer.getInt(0) == ZIP_EOCD_REC_SIG) { final ByteBuffer commentLengthByteBuffer = ByteBuffer.allocate(2); fileChannel.position(eocdStartPos + ZIP_EOCD_COMMENT_LENGTH_FIELD_OFFSET); fileChannel.read(commentLengthByteBuffer); commentLengthByteBuffer.order(ByteOrder.LITTLE_ENDIAN); final int actualCommentLength = commentLengthByteBuffer.getShort(0); if (actualCommentLength == expectedCommentLength) { return actualCommentLength; } } } throw new IOException("ZIP End of Central Directory (EOCD) record not found"); }
From source file:ca.psiphon.ploggy.Robohash.java
public static Bitmap getRobohash(Context context, boolean cacheCandidate, byte[] data) throws Utils.ApplicationError { try {//from www . j a v a2 s . com MessageDigest sha1 = MessageDigest.getInstance("SHA-1"); byte[] digest = sha1.digest(data); String key = Utils.formatFingerprint(digest); Bitmap cachedBitmap = mCache.get(key); if (cachedBitmap != null) { return cachedBitmap; } ByteBuffer byteBuffer = ByteBuffer.wrap(digest); byteBuffer.order(ByteOrder.BIG_ENDIAN); // TODO: SecureRandom SHA1PRNG (but not LinuxSecureRandom) Random random = new Random(byteBuffer.getLong()); AssetManager assetManager = context.getAssets(); if (mConfig == null) { mConfig = new JSONObject(loadAssetToString(assetManager, CONFIG_FILENAME)); } int width = mConfig.getInt("width"); int height = mConfig.getInt("height"); JSONArray colors = mConfig.getJSONArray("colors"); JSONArray parts = colors.getJSONArray(random.nextInt(colors.length())); Bitmap robotBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas robotCanvas = new Canvas(robotBitmap); for (int i = 0; i < parts.length(); i++) { JSONArray partChoices = parts.getJSONArray(i); String selection = partChoices.getString(random.nextInt(partChoices.length())); Bitmap partBitmap = loadAssetToBitmap(assetManager, selection); Rect rect = new Rect(0, 0, width, height); Paint paint = new Paint(); paint.setAlpha(255); robotCanvas.drawBitmap(partBitmap, rect, rect, paint); partBitmap.recycle(); } if (cacheCandidate) { mCache.set(key, robotBitmap); } return robotBitmap; } catch (IOException e) { throw new Utils.ApplicationError(LOG_TAG, e); } catch (JSONException e) { throw new Utils.ApplicationError(LOG_TAG, e); } catch (NoSuchAlgorithmException e) { throw new Utils.ApplicationError(LOG_TAG, e); } }
From source file:org.bimserver.collada.SupportFunctions.java
public static void printMatrix(PrintWriter out, GeometryInfo geometryInfo) { ByteBuffer transformation = ByteBuffer.wrap(geometryInfo.getTransformation()); transformation.order(ByteOrder.LITTLE_ENDIAN); FloatBuffer floatBuffer = transformation.asFloatBuffer(); // Prepare to create the transform matrix. float[] matrix = new float[16]; // Add the first 16 values of the buffer. for (int i = 0; i < matrix.length; i++) matrix[i] = floatBuffer.get();/*from w w w .ja va 2 s .c om*/ // Switch from column-major (x.x ... x.y ... x.z ... 0 ...) to row-major orientation (x.x x.y x.z 0 ...)? matrix = Matrix.changeOrientation(matrix); // List all 16 elements of the matrix as a single space-delimited String object. if (!matrix.equals(identity)) out.println(" <matrix>" + floatArrayToSpaceDelimitedString(matrix) + "</matrix>"); }
From source file:Main.java
/** * Creates the Uri string with embedded expansion codes. * * @param uri to be encoded/* w w w. ja v a 2 s . c om*/ * @return the Uri string with expansion codes. */ public static byte[] encodeUri(String uri) { if (uri == null || uri.length() == 0) { Log.i(TAG, "null or empty uri"); return new byte[0]; } ByteBuffer bb = ByteBuffer.allocate(uri.length()); // UUIDs are ordered as byte array, which means most significant first bb.order(ByteOrder.BIG_ENDIAN); int position = 0; // Add the byte code for the scheme or return null if none Byte schemeCode = encodeUriScheme(uri); if (schemeCode == null) { Log.i(TAG, "null scheme code"); return null; } String scheme = URI_SCHEMES.get(schemeCode); bb.put(schemeCode); position += scheme.length(); if (URLUtil.isNetworkUrl(scheme)) { Log.i(TAG, "is network URL"); return encodeUrl(uri, position, bb); } else if ("urn:uuid:".equals(scheme)) { Log.i(TAG, "is UUID"); return encodeUrnUuid(uri, position, bb); } return null; }
From source file:com.monitor.baseservice.utils.XCodeUtil.java
public static byte[] longToByteArray(long value) { ByteBuffer bb = ByteBuffer.allocate(Long.SIZE / Byte.SIZE); bb.order(ByteOrder.LITTLE_ENDIAN); bb.putLong(value);//from w ww . j a va 2 s . c om return bb.array(); }
From source file:burstcoin.observer.service.ATService.java
public static String getATLong(String hex) { ByteBuffer bf = ByteBuffer.allocate(8); bf.order(ByteOrder.LITTLE_ENDIAN); bf.put(parseHexString(hex));//from w w w.j av a 2 s . c om return toUnsignedLong(bf.getLong(0)); }
From source file:org.zuinnote.hadoop.bitcoin.format.BitcoinUtil.java
/** * Reads a size from a reversed byte order, such as block size in the block header * * @param byteSize byte array with a length of exactly 4 * * @return size, returns 0 in case of invalid block size * */// w w w . j ava 2 s . com public static long getSize(byte[] byteSize) { if (byteSize.length != 4) return 0; ByteBuffer converterBuffer = ByteBuffer.wrap(byteSize); converterBuffer.order(ByteOrder.LITTLE_ENDIAN); return convertSignedIntToUnsigned(converterBuffer.getInt()); }
From source file:io.warp10.quasar.trl.QuasarTokenRevocationListLoader.java
public static QuasarTokenRevocationListLoader getInstance(Properties config, byte[] appSipHashKey) { if (singleton.compareAndSet(false, true)) { ByteBuffer bb = ByteBuffer.wrap(appSipHashKey); bb.order(ByteOrder.BIG_ENDIAN); appIdSipHashKeyK0 = bb.getLong(); appIdSipHashKeyK1 = bb.getLong(); quasarTokenRevocationListLoader = new QuasarTokenRevocationListLoader(config); }/*from w w w.j a v a 2 s . c o m*/ return quasarTokenRevocationListLoader; }
From source file:mtmo.test.mediadrm.Utils.java
public static String accountIdToMarlinFormat(final String accountId) { ByteBuffer accountIdBuf = ByteBuffer.allocate(BYTES_OF_ACCOUNT_ID); try {/*w w w.java 2 s . c o m*/ accountIdBuf.putLong(Long.valueOf(accountId)); } catch (Exception e) { return null; } accountIdBuf.order(ByteOrder.LITTLE_ENDIAN); return String.format(Locale.US, "%016x", accountIdBuf.getLong(0)); }