List of usage examples for java.nio ByteBuffer position
public final Buffer position(int newPosition)
From source file:edu.stanford.mobisocial.dungbeetle.model.DbObject.java
private static int colorFor(Long hash) { float[] baseHues = Feed.getBaseHues(); ByteBuffer bos = ByteBuffer.allocate(8); bos.putLong(hash);//from w ww. j a v a2s. co m byte[] hashBytes = new byte[8]; bos.position(0); bos.get(hashBytes); SecureRandom r = new SecureRandom(hashBytes); float hsv[] = new float[] { baseHues[r.nextInt(baseHues.length)], r.nextFloat(), r.nextFloat() }; hsv[0] = hsv[0] + 20 * r.nextFloat() - 10; hsv[1] = hsv[1] * 0.2f + 0.8f; hsv[2] = hsv[2] * 0.2f + 0.8f; return Color.HSVToColor(hsv); }
From source file:net.darkmist.alib.io.BufferUtil.java
/** * Sane ByteBuffer slice//from w ww . j av a 2 s . c om * @param buf the buffer to slice something out of * @param off The offset into the buffer * @param len the length of the part to slice out */ public static ByteBuffer slice(ByteBuffer buf, int off, int len) { ByteBuffer localBuf = buf.duplicate(); // so we don't mess up the position,etc logger.debug("off={} len={}", off, len); localBuf.position(off); localBuf.limit(off + len); logger.debug("pre-slice: localBuf.position()={} localBuf.limit()={}", localBuf.position(), localBuf.limit()); localBuf = localBuf.slice(); logger.debug("post-slice: localBuf.position()={} localBuf.limit()={}", localBuf.position(), localBuf.limit()); return localBuf; }
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * Decode a String representation.// w w w .j a v a2s .c o m * * @param buffer * a byte buffer holding the string representation * @param position * the starting position in {@code buffer} to start decoding from * @param length * the number of bytes from {@code buffer} to use * @param charset * the String encoding charset * @return the decoded string */ public static String string(ByteBuffer buffer, int position, int length, Charset charset) throws CharacterCodingException { ByteBuffer copy = buffer.duplicate(); copy.position(position); copy.limit(copy.position() + length); return string(copy, charset); }
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * buffer, ?positoin limit ?0/*from w w w . j a v a2 s .c o m*/ * * @param buffer */ public static void clear(ByteBuffer buffer) { if (buffer != null) { buffer.position(0); buffer.limit(0); } }
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * buffer, fill?//w w w . j ava 2s. c om * * @param buffer */ public static void clearToFill(ByteBuffer buffer) { if (buffer != null) { buffer.position(0); buffer.limit(buffer.capacity()); } }
From source file:io.github.dsheirer.record.wave.WaveWriter.java
/** * Creates a wave file header with a format descriptor chunk *//*from w w w. jav a 2 s . c om*/ public static ByteBuffer getWaveHeader(AudioFormat format) { ByteBuffer header = ByteBuffer.allocate(12).order(ByteOrder.LITTLE_ENDIAN); //RIFF/WAVE header and size header.put(RIFF_ID.getBytes()); header.putInt(INITIAL_TOTAL_LENGTH); header.put(WAVE_ID.getBytes()); //Reset the buffer pointer to 0 header.position(0); return header; }
From source file:LamportBasicVersion.java
private static String byteToString(ByteBuffer byteBufferFromNeighbor, MessageInfo messageInfoFromNeighbor) { byteBufferFromNeighbor.position(0); byteBufferFromNeighbor.limit(messageInfoFromNeighbor.bytes()); byte[] bufArr = new byte[byteBufferFromNeighbor.remaining()]; byteBufferFromNeighbor.get(bufArr);/*from w ww .ja va2 s . co m*/ return new String(bufArr); }
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; }/*from ww w . j a v a2 s . co m*/ tmp.position(0); tmp.limit(l); buffer.put(tmp); } }
From source file:com.healthmarketscience.jackcess.impl.OleUtil.java
private static String readStr(ByteBuffer bb, int off, int len, Charset charset) { String str = new String(bb.array(), off, len, charset); bb.position(off + len); if (str.charAt(str.length() - 1) == '\0') { str = str.substring(0, str.length() - 1); }//from ww w .ja v a 2 s . co m return str; }
From source file:io.github.dsheirer.record.wave.WaveWriter.java
/** * Creates a little-endian 4-byte buffer containing an unsigned 32-bit * integer value derived from the 4 least significant bytes of the argument. * * The buffer's position is set to 0 to prepare it for writing to a channel. *//* w ww. jav a 2 s. c o m*/ protected static ByteBuffer getUnsignedIntegerBuffer(long size) { ByteBuffer buffer = ByteBuffer.allocate(4); buffer.put((byte) (size & 0xFFl)); buffer.put((byte) (Long.rotateRight(size & 0xFF00l, 8))); buffer.put((byte) (Long.rotateRight(size & 0xFF0000l, 16))); /* This side-steps an issue with right shifting a signed long by 32 * where it produces an error value. Instead, we right shift in two steps. */ buffer.put((byte) Long.rotateRight(Long.rotateRight(size & 0xFF000000l, 16), 8)); buffer.position(0); return buffer; }