List of usage examples for java.nio ByteBuffer position
public final Buffer position(int newPosition)
From source file:com.glaf.core.util.ByteBufferUtils.java
/** * from to //from w w w .j a v a 2 s .c om * * @param fromBuffer * Buffer ? flush * @param toBuffer * Buffer ? fill * @return number of bytes moved */ public static int put(ByteBuffer fromBuffer, ByteBuffer toBuffer) { int put; int remaining = fromBuffer.remaining(); if (remaining > 0) { // if (remaining <= toBuffer.remaining()) { toBuffer.put(fromBuffer); put = remaining; // from fromBuffer.position(fromBuffer.limit()); } // heap buffer else if (fromBuffer.hasArray()) { put = toBuffer.remaining(); // ?? toBuffer.put(fromBuffer.array(), fromBuffer.arrayOffset() + fromBuffer.position(), put); fromBuffer.position(fromBuffer.position() + put); } // direct buffer else { // ?? put = toBuffer.remaining(); ByteBuffer slice = fromBuffer.slice(); slice.limit(put); toBuffer.put(slice); fromBuffer.position(fromBuffer.position() + put); } } else { put = 0; } return put; }
From source file:de.ailis.usb4java.descriptors.SimpleUsbStringDescriptor.java
/** * Constructs a new string descriptor by reading the descriptor data * from the specified byte buffer./*from www .j av a 2 s . co m*/ * * @param data * The descriptor data as a byte buffer. */ public SimpleUsbStringDescriptor(final ByteBuffer data) { super(data.get(0), data.get(1)); data.position(2); this.bString = new byte[bLength() - 2]; data.get(this.bString); }
From source file:io.druid.segment.writeout.WriteOutBytesTest.java
@Test public void testWriteOutBytes() throws IOException { WriteOutBytes writeOutBytes = segmentWriteOutMedium.makeWriteOutBytes(); writeOutBytes.write('1'); verifyContents(writeOutBytes, "1"); writeOutBytes.writeInt(Ints.fromBytes((byte) '2', (byte) '3', (byte) '4', (byte) '5')); verifyContents(writeOutBytes, "12345"); writeOutBytes.write(new byte[] { 'a' }); verifyContents(writeOutBytes, "12345a"); writeOutBytes.write(new byte[] { 'a', 'b', 'c' }, 1, 1); verifyContents(writeOutBytes, "12345ab"); ByteBuffer bb = ByteBuffer.wrap(new byte[] { 'a', 'b', 'c' }); bb.position(2); writeOutBytes.write(bb);//from ww w . ja va 2s. c om Assert.assertEquals(3, bb.position()); verifyContents(writeOutBytes, "12345abc"); }
From source file:edu.smu.tspell.wordnet.impl.RandomAccessReader.java
/** * Moves the file pointer to a new position and records that position so * that it's available for quick retrieval. * /*from w w w . j a v a 2 s.c om*/ * @param newPosition New byte position within the file. * @throws IOException An error occurred updating the file pointer. */ protected void seek(long newPosition) throws IOException { if (newPosition != filePointer) { ByteBuffer reader = getAccessor(); reader.position((int) newPosition); filePointer = (int) newPosition; } }
From source file:com.dianping.puma.parser.mysql.event.AbstractBinlogEvent.java
private void parseCheckSum(ByteBuffer buf) { if (checksumAlg != BinlogConstants.CHECKSUM_ALG_OFF && checksumAlg != BinlogConstants.CHECKSUM_ALG_UNDEF) { buf.position((int) (this.header.getEventLength() - 4)); setCrc(PacketUtils.readLong(buf, 4)); }/*from www.j av a 2 s . c o m*/ }
From source file:com.netflix.aegisthus.pig.AegisthusLoadCaster.java
private long getNumber(byte[] arg0) throws Exception { byte[] by = hex.decode(arg0); ByteBuffer bb = ByteBuffer.allocate(by.length); bb.put(by);/*ww w . ja va 2 s . co m*/ bb.position(0); switch (by.length) { case 1: return (long) bb.get(); case 2: return (long) bb.getShort(); case 4: return (long) bb.getInt(); case 8: return (long) bb.getLong(); } throw new UnexpectedException("couldn't determine datatype"); }
From source file:com.netflix.aegisthus.pig.AegisthusLoadCaster.java
@Override public Double bytesToDouble(byte[] arg0) throws IOException { if (arg0 == null || arg0.length == 0) { return null; }/*from w ww . ja va 2s.c om*/ try { byte[] by = hex.decode(arg0); ByteBuffer bb = ByteBuffer.allocate(by.length); bb.put(by); bb.position(0); return bb.getDouble(); } catch (Exception e) { LOG.error("failed to convert " + new String(arg0) + " to double"); return null; } }
From source file:com.netflix.aegisthus.pig.AegisthusLoadCaster.java
@Override public Float bytesToFloat(byte[] arg0) throws IOException { if (arg0 == null || arg0.length == 0) { return null; }/*w w w.j a v a2s . co m*/ try { byte[] by = hex.decode(arg0); ByteBuffer bb = ByteBuffer.allocate(by.length); bb.put(by); bb.position(0); return bb.getFloat(); } catch (Exception e) { LOG.error("failed to convert " + new String(arg0) + " to float"); return null; } }
From source file:io.github.dsheirer.record.wave.WaveWriter.java
/** * Creates an audio format chunk//from w w w.j a va2 s . c om */ public static ByteBuffer getFormatChunk(AudioFormat format) { ByteBuffer header = ByteBuffer.allocate(24).order(ByteOrder.LITTLE_ENDIAN); //Format descriptor header.put(FORMAT_CHUNK_ID.getBytes()); header.putInt(FORMAT_CHUNK_LENGTH); header.putShort(FORMAT_UNCOMPRESSED_PCM); header.putShort((short) format.getChannels()); header.putInt((int) format.getSampleRate()); //Byte Rate = sample rate * channels * bits per sample / 8 int frameByteRate = format.getChannels() * format.getSampleSizeInBits() / 8; int byteRate = (int) (format.getSampleRate() * frameByteRate); header.putInt(byteRate); //Block Align header.putShort((short) frameByteRate); //Bits per Sample header.putShort((short) format.getSampleSizeInBits()); //Reset the buffer pointer to 0 header.position(0); return header; }
From source file:ru.jts_dev.gameserver.movement.geoengine.GeoService.java
public void loadRegion(int x, int y, ByteBuffer byteBuffer) { Region region = new Region(); int index = 0; byteBuffer.position(18); while (byteBuffer.hasRemaining()) { short type = byteBuffer.getShort(); if (type == 0x0000) { // 1x short, flat block short maxHeight = (short) (byteBuffer.getShort() & 0x0fff0); short minHeight = (short) (byteBuffer.getShort() & 0x0fff0); Block block = new FlatBlock(minHeight, maxHeight); region.addBlock(x, y, index, block); } else if (type == 0x0040) { //type id from rebellion // 64x short, complex block Cell[] cells = new Cell[64]; for (int cell = 0; cell < 64; cell++) { int value = byteBuffer.getShort(); short height = (short) ((short) (value & 0x0fff0) >> 1); byte direction = (byte) (value & 0x0F); cells[cell] = new Cell(direction, height); }// w ww .j a v a 2s . co m Block block = new ComplexBlock(cells); region.addBlock(x, y, index, block); } else { //0x0048 block id // 64x-8192x short, multilevel block short[][] height = new short[64][]; byte[][] NSWE = new byte[64][]; byte[] layersa = new byte[64]; for (int cell = 0; cell < 64; cell++) { short layers = byteBuffer.getShort(); height[cell] = new short[layers]; NSWE[cell] = new byte[layers]; for (int i = 0; i < layers; i++) { int value = byteBuffer.getShort(); height[cell][i] = (short) ((short) (value & 0x0fff0) >> 1); NSWE[cell][i] = (byte) (value & 0x0F); } layersa[cell] = (byte) --layers; } Block block = new MultilevelBlock(layersa, height, NSWE); region.addBlock(x, y, index, block); } index++; } }