List of usage examples for java.nio ByteBuffer allocateDirect
public static ByteBuffer allocateDirect(int capacity)
From source file:Main.java
public static ByteBuffer fromStream(InputStream stream) throws IOException { ByteArrayOutputStream outStream = new ByteArrayOutputStream(BUFFER_SIZE); byte[] buffer = BUFFER_REF.getAndSet(null); if (buffer == null) { buffer = new byte[BUFFER_SIZE]; }// w w w.j av a 2 s . c o m int n = -1; while ((n = stream.read(buffer)) >= 0) { outStream.write(buffer, 0, n); } BUFFER_REF.set(buffer); byte[] bytes = outStream.toByteArray(); // Some resource decoders require a direct byte buffer. Prefer allocateDirect() over wrap() return (ByteBuffer) ByteBuffer.allocateDirect(bytes.length).put(bytes).position(0); }
From source file:Main.java
public static ByteBuffer createByteBuffer(final int size) { final ByteBuffer buf = ByteBuffer.allocateDirect(size).order(ByteOrder.nativeOrder()); buf.clear();/*w w w . j a v a 2 s . c o m*/ return buf; }
From source file:Main.java
/** * Allocates a direct float buffer, and populates it with the float array data. */// w w w .j av a2 s . c om public static FloatBuffer createFloatBuffer(float[] coords) { // Allocate a direct ByteBuffer, using 4 bytes per float, and copy coords into it. ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * SIZEOF_FLOAT); bb.order(ByteOrder.nativeOrder()); FloatBuffer fb = bb.asFloatBuffer(); fb.put(coords); fb.position(0); return fb; }
From source file:Main.java
public static ByteBuffer newByteBuffer(int numElements) { ByteBuffer bb = ByteBuffer.allocateDirect(numElements); bb.order(ByteOrder.nativeOrder()); return bb;/*w ww .jav a 2s.co m*/ }
From source file:Main.java
public static boolean fileCopy(File srcFile, File dstFile) { int length = 1048891; FileChannel inC = null;/*from ww w.j a v a 2s. c o m*/ FileChannel outC = null; try { FileInputStream in = new FileInputStream(srcFile); FileOutputStream out = new FileOutputStream(dstFile); inC = in.getChannel(); outC = out.getChannel(); ByteBuffer b = null; while (inC.position() < inC.size()) { if ((inC.size() - inC.position()) < length) { length = (int) (inC.size() - inC.position()); } else { length = 1048891; } b = ByteBuffer.allocateDirect(length); inC.read(b); b.flip(); outC.write(b); outC.force(false); } return true; } catch (IOException e) { e.printStackTrace(); return false; } finally { try { if (inC != null && inC.isOpen()) { inC.close(); } if (outC != null && outC.isOpen()) { outC.close(); } } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.arrow.acn.client.utils.MD5Util.java
public static byte[] calcMD5Checksum(Path path) throws NoSuchAlgorithmException, IOException { MessageDigest md = MessageDigest.getInstance("MD5"); try (SeekableByteChannel sbc = Files.newByteChannel(path)) { ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE); while (sbc.read(buf) > 0) { buf.flip();/*from ww w. j a v a 2s.co m*/ md.update(buf); buf.clear(); } } return md.digest(); }
From source file:Main.java
static private void ensureSize(int n) { if (buffer.capacity() < n) { buffer = ByteBuffer.allocateDirect(n).order(ByteOrder.nativeOrder()); bufferInt = buffer.asIntBuffer(); bufferFloat = buffer.asFloatBuffer(); }// ww w . j a va 2s. co m }
From source file:Main.java
public static IntBuffer createIntBuffer(final int size) { final IntBuffer buf = ByteBuffer.allocateDirect(SIZEOF_FLOAT * size).order(ByteOrder.nativeOrder()) .asIntBuffer();// w w w .j av a2 s .c o m buf.clear(); return buf; }
From source file:Main.java
/**android methods*/ //Only for Android public static IntBuffer makeFloatBuffer(int[] array) { final int integerSize = Integer.SIZE / 8; ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * integerSize); byteBuffer.order(ByteOrder.nativeOrder()); IntBuffer intBuffer = byteBuffer.asIntBuffer(); intBuffer.put(array);/*from w w w . ja va 2 s . c o m*/ intBuffer.position(0); return intBuffer; }
From source file:Main.java
public static FloatBuffer makeFloatBuffer(float[] array) { final int floatSize = Float.SIZE / 8; ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * floatSize); byteBuffer.order(ByteOrder.nativeOrder()); FloatBuffer floatBuffer = byteBuffer.asFloatBuffer(); floatBuffer.put(array);/*from w w w . j a v a 2 s. c o m*/ floatBuffer.position(0); return floatBuffer; }