List of usage examples for java.nio ByteBuffer wrap
public static ByteBuffer wrap(byte[] array)
From source file:Main.java
/** * Convert from array of byte to array of short. * @param byteArray byte array// w ww . j a v a 2 s . co m * @return short array */ public static short[] byteToShort(final byte[] byteArray) { short[] shortOut = new short[byteArray.length / 2]; ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray); byteBuffer.order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shortOut); return shortOut; }
From source file:Main.java
public static int byteArrayCompare(byte[] byte1, byte[] byte2) { byte[] tByte1 = new byte[byte2.length]; ByteArrayInputStream input = new ByteArrayInputStream(byte1); try {//from w w w . j a v a 2s . c o m input.read(tByte1); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } ByteBuffer byteBuf1 = ByteBuffer.wrap(tByte1); ByteBuffer byteBuf2 = ByteBuffer.wrap(byte2); return byteBuf1.compareTo(byteBuf2); }
From source file:Main.java
public static ByteBuffer getDataBuffer() { String lineSeparator = System.getProperty("line.separator"); StringBuilder sb = new StringBuilder(); sb.append("test"); sb.append(lineSeparator);//from ww w . ja va2 s .c om String str = sb.toString(); Charset cs = Charset.forName("UTF-8"); ByteBuffer bb = ByteBuffer.wrap(str.getBytes(cs)); return bb; }
From source file:Main.java
private static void receive(Map<Integer, byte[]> map, DatagramSocket socket, int[] length) { byte[] b = new byte[limit + 4]; DatagramPacket p = new DatagramPacket(b, b.length); try {/*from www. ja v a2s. c o m*/ socket.receive(p); } catch (IOException ex) { ex.printStackTrace(); } if (p.getLength() != 0) { ByteBuffer buf = ByteBuffer.wrap(b); int key = buf.getInt(); byte[] bytes = new byte[p.getLength() - 4]; length[0] += bytes.length; buf.get(bytes); map.put(key, bytes); receive(map, socket, length); } }
From source file:Main.java
private static IntBuffer getImageAsARGBIntBuffer(BufferedImage image) { DataBuffer buffer = image.getRaster().getDataBuffer(); if (buffer instanceof DataBufferInt) { return IntBuffer.wrap(((DataBufferInt) buffer).getData()); } else if (buffer instanceof DataBufferByte) { return ByteBuffer.wrap(((DataBufferByte) buffer).getData()).order(ByteOrder.BIG_ENDIAN).asIntBuffer(); } else {/*from w w w .ja v a 2 s .c o m*/ int width = image.getWidth(); int height = image.getHeight(); int[] pixels = new int[width * height]; image.getRGB(0, 0, width, height, pixels, 0, width); return IntBuffer.wrap(pixels); } }
From source file:Main.java
public static String stripNonASCII(String ret) { char[] chars = ret.toCharArray(); CharsetDecoder d = Charset.forName("US-ASCII").newDecoder(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < chars.length; i++) { try {// w w w . ja va2s .c om char c = chars[i]; String letter = new String(new char[] { c }); byte bytearray[] = letter.getBytes(); CharBuffer r = d.decode(ByteBuffer.wrap(bytearray)); r.toString(); sb.append(c); } catch (CharacterCodingException e) { } } return sb.toString(); }
From source file:com.hengyi.japp.tools.UuidUtils.java
protected static String base64Uuid(UUID uuid) { ByteBuffer bb = ByteBuffer.wrap(new byte[16]); bb.putLong(uuid.getMostSignificantBits()); bb.putLong(uuid.getLeastSignificantBits()); return Base64.encodeBase64URLSafeString(bb.array()); }
From source file:Main.java
/** * Converts the given byte array, which is expected to contain the UUID, into a UUID instance. * * @param byteArray The byte array containing the UUID. * @return A newly created UUID instance or null in case of a failure. *//*from w ww. ja va 2 s .c om*/ public static UUID byteArrayToUuid(byte[] byteArray) { if (byteArray != null && byteArray.length >= UUID_LENGTH_IN_BYTES) { ByteBuffer byteBuffer = ByteBuffer.wrap(byteArray); long mostSignificantBits = byteBuffer.getLong(); long leastSignificantBits = byteBuffer.getLong(); return new UUID(mostSignificantBits, leastSignificantBits); } return null; }
From source file:Main.java
private static String formatBytes(byte[] data, boolean unsigned) { ByteBuffer bb = ByteBuffer.wrap(data); StringBuilder sb = new StringBuilder(bb.capacity() * 3); while (bb.remaining() > 0) { if (unsigned) { sb.append(bb.get() & 0xff); } else {//from w w w . j a v a 2 s .com sb.append(bb.get()); } sb.append(','); sb.append('\n'); } return sb.toString(); }
From source file:Main.java
public static ByteBuffer jsonToByteBuffer(JSONObject jsonObj) { return ByteBuffer.wrap(jsonObj.toString().getBytes(Charset.defaultCharset())); }