List of usage examples for java.nio ByteBuffer array
public final byte[] array()
From source file:com.stratagis.geoevent.adapter.nmeaplus.NmeaPlusInboundAdapter.java
private static List<byte[]> index(ByteBuffer in) { List<byte[]> messages = new ArrayList<byte[]>(); for (int i = -1; in.hasRemaining();) { byte b = in.get(); if (b == ((byte) '$')) // bom {/* w w w.j a v a2s . c o m*/ i = in.position(); in.mark(); } else if (b == ((byte) '\r') || b == ((byte) '\n')) // eom { if (i != -1) { byte[] message = new byte[in.position() - 1 - i]; System.arraycopy(in.array(), i, message, 0, message.length); messages.add(message); } i = -1; in.mark(); } else if (messages.isEmpty() && i == -1) in.mark(); } return messages; }
From source file:com.glaf.core.util.ByteBufferUtils.java
public static void write(ByteBuffer buffer, DataOutput out) throws IOException { if (buffer.hasArray()) { out.write(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()); } else {/* w w w. ja va2s . co m*/ for (int i = buffer.position(); i < buffer.limit(); i++) { out.writeByte(buffer.get(i)); } } }
From source file:com.glaf.core.util.ByteBufferUtils.java
public static void readFrom(InputStream is, int needed, ByteBuffer buffer) throws IOException { ByteBuffer tmp = allocate(8192); while (needed > 0 && buffer.hasRemaining()) { int l = is.read(tmp.array(), 0, 8192); if (l < 0) { break; }/* w w w.j ava 2s. c o m*/ tmp.position(0); tmp.limit(l); buffer.put(tmp); } }
From source file:io.mycat.util.ByteBufferUtil.java
/** * @return a new copy of the data in @param buffer * USUALLY YOU SHOULD USE ByteBuffer.duplicate() INSTEAD, which creates a new Buffer * (so you can mutate its position without affecting the original) without copying the underlying array. *///from w ww .j a v a 2 s. co m public static ByteBuffer clone(ByteBuffer buffer) { assert buffer != null; if (buffer.remaining() == 0) { return EMPTY_BYTE_BUFFER; } ByteBuffer clone = ByteBuffer.allocate(buffer.remaining()); if (buffer.hasArray()) { System.arraycopy(buffer.array(), buffer.arrayOffset() + buffer.position(), clone.array(), 0, buffer.remaining()); } else { clone.put(buffer.duplicate()); clone.flip(); } return clone; }
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * buffer?//from w w w .j a v a2 s. c o m * * @param buffer * @return */ public static byte[] toArray(ByteBuffer buffer) { // ?heap buffer if (buffer.hasArray()) { byte[] array = buffer.array(); int from = buffer.arrayOffset() + buffer.position(); return Arrays.copyOfRange(array, from, from + buffer.remaining()); } // direct buffer else { byte[] to = new byte[buffer.remaining()]; buffer.slice().get(to); return to; } }
From source file:Main.java
public static byte[] makeFontBitmap(String font, String code, int size, float[] arrayOfPos) { Log.v(TAG, String.format("makeFontBitmap called(Java): font=%s code=%s", font, code)); Canvas c = new Canvas(); Paint p = new Paint(); // Log.v(TAG, "get density"); float density = context.getResources().getDisplayMetrics().density; Log.v(TAG, String.format("makeFontBitmap density: %f", density)); p.setTextSize((float) size * density); p.setAntiAlias(true);/* w ww . ja v a 2 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); // ByteBuffer buf = ByteBuffer.allocate(b.getRowBytes()); 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())); // byte bytes[] = buf.array(); // for (int i = 0; i < size * size * 2; i++) // bytes[i] = (byte)(Math.random() * 255); return buf.array(); }
From source file:com.glaf.core.util.ByteBufferUtils.java
public static void arrayCopy(ByteBuffer buffer, int position, byte[] bytes, int offset, int length) { if (buffer.hasArray()) System.arraycopy(buffer.array(), buffer.arrayOffset() + position, bytes, offset, length); else/*w w w .j a va2 s . c o m*/ ((ByteBuffer) buffer.duplicate().position(position)).get(bytes, offset, length); }
From source file:io.Text.java
/** Write a UTF8 encoded string to out *///from w ww .j a v a 2 s .c o m public static int writeString(DataOutput out, String s) throws IOException { ByteBuffer bytes = encode(s); int length = bytes.limit(); WritableUtils.writeVInt(out, length); out.write(bytes.array(), 0, length); return length; }
From source file:eap.util.EDcodeUtil.java
public static byte[] utf8Encode(CharSequence string) { try {// w ww .j a v a2 s . c om ByteBuffer bytes = CHARSET.newEncoder().encode(CharBuffer.wrap(string)); byte[] bytesCopy = new byte[bytes.limit()]; System.arraycopy(bytes.array(), 0, bytesCopy, 0, bytes.limit()); return bytesCopy; } catch (CharacterCodingException e) { throw new IllegalArgumentException("Encoding failed", e); } }
From source file:edu.ku.brc.ui.GraphicsUtils.java
/** * @param name//www .j a va2 s . c om * @param str * @return */ public static ImageIcon uudecodeImage(final String name, final String str) { try { UUDecoder decoder = new UUDecoder(); ByteBuffer bb = decoder.decodeBufferToByteBuffer(str); ImageIcon img = new ImageIcon(bb.array()); return img; } catch (Exception ex) { edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(GraphicsUtils.class, ex); ex.printStackTrace(); } return null; }