List of usage examples for java.nio ByteBuffer position
public final Buffer position(int newPosition)
From source file:Main.java
public static ByteBuffer createByteBuffer(byte... bytes) { ByteBuffer bb = createByteBuffer(bytes.length).put(bytes); bb.position(0); return bb;//from www .ja v a 2 s.co m }
From source file:Main.java
public static ByteBuffer buildByteBuffer(byte[] array) { ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length); byteBuffer.put(array);/*from w ww .j a v a 2 s. c om*/ byteBuffer.position(0); return byteBuffer; }
From source file:Main.java
/** * Make a direct NIO ByteBuffer from an array of floats * @param arr The array/* w ww . j a va 2 s. co m*/ * @return The newly created FloatBuffer */ public static ByteBuffer makeByteBuffer(byte[] arr) { ByteBuffer bb = ByteBuffer.allocateDirect(arr.length); bb.order(ByteOrder.nativeOrder()); bb.put(arr); bb.position(0); return bb; }
From source file:Main.java
public static ByteBuffer makeByteBuffer(byte[] array) { if (array == null) throw new IllegalArgumentException(); ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length); byteBuffer.order(ByteOrder.nativeOrder()); byteBuffer.put(array);//from ww w . ja va 2s .c o m byteBuffer.position(0); return byteBuffer; }
From source file:Main.java
/** * Returns the {@code String} instance with detecting the Japanese encoding. * @throws UnsupportedEncodingException if it fails to detect the encoding. *//* w w w. j av a 2 s . c om*/ static String toStringWithEncodingDetection(ByteBuffer buffer) throws UnsupportedEncodingException { for (String encoding : JAPANESE_ENCODING_LIST) { buffer.position(0); try { Charset charset = Charset.forName(encoding); CharBuffer result = charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT).decode(buffer); String str = result.toString(); if (str.length() > 0 && str.charAt(0) == 0xFEFF) { // Remove leading BOM if necessary. str = str.substring(1); } return str; } catch (Exception e) { // Ignore exceptions, and retry next encoding. } } throw new UnsupportedEncodingException("Failed to detect encoding"); }
From source file:Main.java
public static ByteBuffer makeByteBuffer(byte[] array) { final int SIZE = Byte.SIZE / 8; ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * SIZE); byteBuffer.order(ByteOrder.nativeOrder()); byteBuffer.put(array);// w w w . j av a2 s. c o m byteBuffer.position(0); return byteBuffer; }
From source file:Main.java
public static ByteBuffer makeByteBuffer(byte[] array) /* */ {//from w w w . j a v a 2 s . c o m /* 63 */int SIZE = 1; /* 64 */ByteBuffer byteBuffer = ByteBuffer.allocateDirect(array.length * 1); /* 65 */byteBuffer.order(ByteOrder.nativeOrder()); /* 66 */byteBuffer.put(array); /* 67 */byteBuffer.position(0); /* 68 */return byteBuffer; /* */}
From source file:TimedJsonStreamParserTest.java
private static ByteBuffer getJsonByteBuffer(Object obj) throws IOException { byte[] bytes = mapper.writeValueAsBytes(obj); ByteBuffer buff = ByteBuffer.wrap(bytes); buff.position(0); return buff;//from w w w. j ava 2 s . co m }
From source file:Main.java
public static ByteBuffer setupByteBuffer(ByteBuffer preBuffer, byte[] array) { if (preBuffer == null || preBuffer.capacity() < array.length) { preBuffer = createByteBuffer(array.length * 2); } else {//from ww w .j a v a 2 s.c om preBuffer.clear(); } preBuffer.put(array); preBuffer.position(0); return preBuffer; }
From source file:Main.java
public static byte[] makeFontBitmap(String font, String code, int size, float[] arrayOfPos) { Canvas c = new Canvas(); Paint p = new Paint(); float density = context.getResources().getDisplayMetrics().density; // Log.v(TAG, String.format("makeFontBitmap called(Java): font=%s code=%s density=%f", font, code, density)); p.setTextSize((float) size * density); p.setAntiAlias(true);/*from w w w . j a va2 s . c o m*/ Rect textBounds = new Rect(); p.getTextBounds(code, 0, code.length(), textBounds); // Log.v(TAG, String.format("makeFontBitmap textBounds: %d,%d,%d,%d", textBounds.left, textBounds.top, textBounds.right, textBounds.bottom)); Rect textBoundsAxA = new Rect(); String axa = String.format("A%sA", code); p.getTextBounds(axa, 0, axa.length(), textBoundsAxA); Rect textBoundsAA = new Rect(); String aa = "AA"; p.getTextBounds(aa, 0, aa.length(), textBoundsAA); // cache.distDelta = Vec2(0, 0); arrayOfPos[0] = textBounds.left; arrayOfPos[1] = textBounds.top; // cache.srcWidth = Vec2(16, 16); arrayOfPos[2] = textBounds.width(); arrayOfPos[3] = textBounds.height(); // cache.step = 16; // arrayOfPos[4] = textBounds.width() + 1; arrayOfPos[4] = textBoundsAxA.width() - textBoundsAA.width(); if (textBounds.width() == 0 || textBounds.height() == 0) { Log.v(TAG, "makeFontBitmap: empty"); return null; } Bitmap b = Bitmap.createBitmap(textBounds.width(), textBounds.height(), Bitmap.Config.ARGB_8888); c.setBitmap(b); Rect r = new Rect(0, 0, textBounds.width(), textBounds.height()); // p.setColor(Color.RED); p.setARGB(0, 0, 0, 0); c.drawRect(r, p); p.setARGB(255, 255, 255, 255); // Log.v(TAG, "makeFontBitmap: drawText"); c.drawText(code, -textBounds.left, -textBounds.top, p); // Log.v(TAG, String.format("makeFontBitmap: w=%.2f h=%.2f", arrayOfPos[2], arrayOfPos[3])); ByteBuffer buf = ByteBuffer.allocate(textBounds.width() * textBounds.height() * 4); // Log.v(TAG, String.format("makeFontBitmap: b.getRowBytes() %d", b.getRowBytes())); buf.position(0); b.copyPixelsToBuffer(buf); // Log.v(TAG, String.format("makeFontBitmap results: capacity=%d", buf.capacity())); return buf.array(); }