List of usage examples for java.nio ByteBuffer array
public final byte[] array()
From source file:com.icloud.framework.core.nio.ByteBufferUtil.java
public static int compare(ByteBuffer o1, byte[] o2) { return compareUnsigned(o1.array(), o2, o1.arrayOffset() + o1.position(), 0, o1.limit() + o1.arrayOffset(), o2.length);/*w w w.j a v a 2 s.co m*/ }
From source file:com.jadarstudios.rankcapes.bukkit.CapePackValidator.java
/** * Utility method to detect if the bytes given are a zip file. * * @param bytes the bytes of the file//from ww w. ja v a 2s . c o m * * @return if it is a zip file */ public static boolean isZipFile(byte[] bytes) { ByteArrayInputStream input = new ByteArrayInputStream(bytes); ByteBuffer buffer = ByteBuffer.allocate(4); input.read(buffer.array(), 0, buffer.capacity()); short packIdentifier = buffer.getShort(); return packIdentifier == ZIP_IDENTIFIER; }
From source file:foss.filemanager.core.Utils.java
static byte[] encode(byte[] arr, Charset srcCharset, Charset dstCharset) { ByteBuffer inputBuffer = ByteBuffer.wrap(arr); CharBuffer data = srcCharset.decode(inputBuffer); ByteBuffer outputBuffer = dstCharset.encode(data); byte[] outputData = outputBuffer.array(); return outputData; }
From source file:Com.Operaciones.java
public static InputStream generateBarras(String text, int h, int w) throws Exception { Charset charset = Charset.forName("ISO-8859-1"); CharsetEncoder encoder = charset.newEncoder(); byte[] b = null; ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(text)); b = bbuf.array(); String data = new String(b, "ISO-8859-1"); // get a byte matrix for the data BitMatrix matrix = null;//from w ww.j a va 2 s .c o m matrix = new Code128Writer().encode(data, BarcodeFormat.CODE_128, w, h, null); int height = matrix.getHeight(); int width = matrix.getWidth(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE); } } byte[] imgByte = ChartUtilities.encodeAsPNG(image); InputStream myInputStream = new ByteArrayInputStream(imgByte); return myInputStream; }
From source file:Main.java
/** * Finds the percentage of pixels that do are empty. * * @param bitmap input bitmap/* www. j a va 2s.c o m*/ * @return a value between 0.0 to 1.0 . Note the method will return 0.0 if either of bitmaps are null nor of same size. * */ public static float getTransparentPixelPercent(Bitmap bitmap) { if (bitmap == null) { return 0f; } ByteBuffer buffer = ByteBuffer.allocate(bitmap.getHeight() * bitmap.getRowBytes()); bitmap.copyPixelsToBuffer(buffer); byte[] array = buffer.array(); int len = array.length; int count = 0; for (int i = 0; i < len; i++) { if (array[i] == 0) { count++; } } return ((float) (count)) / len; }
From source file:com.icloud.framework.core.nio.ByteBufferUtil.java
public static int compareUnsigned(ByteBuffer o1, ByteBuffer o2) { return compareUnsigned(o1.array(), o2.array(), o1.arrayOffset() + o1.position(), o2.arrayOffset() + o2.position(), o1.limit() + o1.arrayOffset(), o2.limit() + o2.arrayOffset()); }
From source file:Main.java
public static byte[] padRight(byte[] tgt, int len, byte padding) { if (tgt.length >= len) { return tgt; }/*w w w .j a v a2 s . co m*/ byte[] paddings = new byte[len - tgt.length]; Arrays.fill(paddings, padding); ByteBuffer buffer = ByteBuffer.allocate(len); buffer.put(tgt); buffer.put(paddings); return buffer.array(); }
From source file:Main.java
/** * Generates a Base64 version of UUID//from w ww .j a v a 2 s .c o m * @return a string in Base64 URL-safe format without padding at the end and no wrapping */ public static String randomUuidBase64() { UUID uuid = UUID.randomUUID(); ByteBuffer bb = ByteBuffer.wrap(new byte[16]); bb.putLong(uuid.getMostSignificantBits()); bb.putLong(uuid.getLeastSignificantBits()); return encodeUrlSafe64(bb.array()); }
From source file:Main.java
public static byte[] genSirfCommand(String commandHexa) { int length = commandHexa.length() / 2; ByteBuffer command = ByteBuffer.allocate(length); command.put(new BigInteger(commandHexa, 16).toByteArray(), 1, length); return command.array(); }
From source file:Main.java
public static final int compareToP(ByteBuffer bb1, ByteBuffer bb2) { final int offset1 = bb1.position(); final int offset2 = bb2.position(); final byte[] array1 = bb1.array(); final byte[] array2 = bb2.array(); final int len1 = bb1.remaining(); final int len2 = bb2.remaining(); return compareTo(array1, offset1, len1, array2, offset2, len2); }