List of usage examples for java.nio ByteBuffer flip
public final Buffer flip()
From source file:Main.java
/** * bytes tp chars/*from www. j av a 2 s.c o m*/ * * @param bytes * @return */ public static char[] getChars(byte[] bytes) { Charset cs = Charset.forName("UTF-8"); ByteBuffer bb = ByteBuffer.allocate(bytes.length); bb.put(bytes); bb.flip(); CharBuffer cb = cs.decode(bb); return cb.array(); }
From source file:Main.java
/** * Converts an array of 4 bytes into a int. * * @param bytes The bytes./*from w w w. ja v a2 s . co m*/ * @return The int. */ public static int bytesToInt(final byte[] bytes) { final ByteBuffer buffer = ByteBuffer.allocate(4); buffer.put(bytes, 0, 4); buffer.flip(); return buffer.getInt(); }
From source file:Main.java
/** * Converts an array of 8 bytes into a long. * * @param bytes The bytes./*from ww w .ja va 2 s. c o m*/ * @return The long. */ public static long bytesToLong(final byte[] bytes) { final ByteBuffer buffer = ByteBuffer.allocate(8); buffer.put(bytes, 0, 8); buffer.flip(); return buffer.getLong(); }
From source file:Main.java
public static char[] getChars(byte[] buffer, int offset, int length) { ByteBuffer bb = ByteBuffer.allocate(length); bb.put(buffer, offset, length);// w w w .j a va2 s . c om bb.flip(); return Charset.defaultCharset().decode(bb).array(); }
From source file:Main.java
public static long bytesToLong(byte[] bytes) { long l = 0;//from www .j a v a 2 s .c o m try { ByteBuffer buffer = ByteBuffer.allocate(8); buffer.put(bytes, 0, bytes.length); buffer.flip();//need flip l = buffer.getLong(); } catch (Exception e) { } return l; }
From source file:Main.java
public static String processRead(SelectionKey key) throws Exception { SocketChannel sChannel = (SocketChannel) key.channel(); ByteBuffer buffer = ByteBuffer.allocate(1024); sChannel.read(buffer);/*from w ww .ja v a 2s. c o m*/ buffer.flip(); Charset charset = Charset.forName("UTF-8"); CharsetDecoder decoder = charset.newDecoder(); CharBuffer charBuffer = decoder.decode(buffer); String msg = charBuffer.toString(); return msg; }
From source file:Main.java
public final static ByteBuffer toNALFileFormat(final ByteBuffer buffer) { ByteBuffer result = ByteBuffer.allocate(buffer.remaining()); result.put(buffer);/* w w w.ja va 2 s . co m*/ result.flip(); int length = 0; int position = -1; int remaining = result.remaining() - 3; for (int i = 0; i < remaining; ++i) { if (result.get(i) == 0x00 && result.get(i + 1) == 0x00 && result.get(i + 2) == 0x00 && result.get(i + 3) == 0x01) { if (0 <= position) { result.putInt(position, length - 3); } position = i; length = 0; } else { ++length; } } result.putInt(position, length); return result; }
From source file:Main.java
public static int bytesToInt(byte[] bytes) { ByteBuffer buffer = ByteBuffer.allocate(4); reverseArray(bytes);//w w w . ja v a 2 s . c om buffer.put(bytes, 0, bytes.length); buffer.flip();// need flip return buffer.getInt(); }
From source file:Main.java
public static ByteBuffer cloneByteBuffer(final ByteBuffer original) { // Create clone with same capacity as original. final ByteBuffer clone = (original.isDirect()) ? ByteBuffer.allocateDirect(original.capacity()) : ByteBuffer.allocate(original.capacity()); // Create a read-only copy of the original. // This allows reading from the original without modifying it. final ByteBuffer readOnlyCopy = original.asReadOnlyBuffer(); // Flip and read from the original. readOnlyCopy.flip(); clone.put(readOnlyCopy);/*from w w w .j a va2 s .c o m*/ clone.position(original.position()); clone.limit(original.limit()); clone.order(original.order()); return clone; }
From source file:Main.java
public static long bytesToLong(byte[] bytes) { ByteBuffer buffer = ByteBuffer.allocate(8); reverseArray(bytes);//from w ww.j a v a 2 s .c o m buffer.put(bytes, 0, bytes.length); buffer.flip();// need flip return buffer.getLong(); }