List of usage examples for java.nio ByteBuffer put
public ByteBuffer put(ByteBuffer src)
From source file:com.shuffle.p2p.Bytestring.java
public Bytestring prepend(Bytestring pre) { ByteBuffer target = ByteBuffer.wrap(new byte[bytes.length + pre.bytes.length]); target.put(pre.bytes); target.put(bytes);/* w w w . ja v a2 s . c o m*/ return new Bytestring(target.array()); }
From source file:net.jenet.CommandHeader.java
public void toBuffer(ByteBuffer buffer) { buffer.put(command); buffer.put(channelID);/*from w ww . ja v a2 s. c o m*/ buffer.put(flags); buffer.put(reserved); buffer.putInt(commandLength); buffer.putInt(reliableSequenceNumber); }
From source file:com.rinke.solutions.pinball.io.UsbConnector.java
@Override protected void send(byte[] data, ConnectionHandle handle) { IntBuffer transfered = IntBuffer.allocate(1); ByteBuffer buffer = ByteBuffer.allocateDirect(data.length); buffer.put(data); UsbHandle usb = (UsbHandle) handle;/*from w ww.j a v a 2 s. com*/ // Use device handle here int res = LibUsb.bulkTransfer(usb.getDeviceHandle(), (byte) 0x01, buffer, transfered, 4000); if (res != LibUsb.SUCCESS) throw new LibUsbException("Control transfer failed", res); if (transfered.get() != data.length) { log.error("unexpected length returned on bulk: {}", transfered.get()); } }
From source file:edu.umass.cs.gigapaxos.paxospackets.AcceptPacket.java
@Override public synchronized byte[] toBytes() { long t = System.nanoTime(); if (!(PaxosPacket.BYTEIFICATION && IntegerMap.allInt())) return super.toBytes(); if (this.getByteifiedSelf() != null) return this.getByteifiedSelf(); // else construct byte[] buf = super.toBytes(false); byte[] bytes = new byte[buf.length // ProposalPacket.slot + SIZEOF_PROPOSAL/*from w ww. j a va2 s. com*/ // PValuePacket:ballot, recovery, medianCheckpointedSlot, // noCoalesce + SIZEOF_PVALUE // AcceptPacket.sender + SIZEOF_ACCEPT]; ByteBuffer bbuf = ByteBuffer.wrap(bytes); // request bbuf.put(buf); // proposal bbuf.putInt(this.slot) // pvalue .putInt(this.ballot.ballotNumber).putInt(this.ballot.coordinatorID) .put(this.isRecovery() ? (byte) 1 : 0).putInt(this.getMedianCheckpointedSlot()).put((byte) 0) // accept .putInt(this.sender); assert (bbuf.remaining() == 0); // exact alignment this.setByteifiedSelf(bytes); if (PaxosMessenger.INSTRUMENT_SERIALIZATION && Util.oneIn(100)) DelayProfiler.updateDelayNano("accept->", t, this.batchSize() + 1); return bytes; }
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 .j a va 2s .co m 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; }//from ww w . ja v a 2 s . c o 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:com.rinke.solutions.pinball.io.UsbConnector.java
@Override protected byte[] receive(ConnectionHandle h, int len) { byte[] data = new byte[len]; IntBuffer transfered = IntBuffer.allocate(1); ByteBuffer buffer = ByteBuffer.allocateDirect(data.length); buffer.put(data); // Use device handle here UsbHandle usb = (UsbHandle) h;//w w w . j ava 2 s . c om int res = LibUsb.bulkTransfer(usb.getDeviceHandle(), (byte) 0x81, buffer, transfered, 4000); if (res != LibUsb.SUCCESS) throw new LibUsbException("Control transfer failed", res); int read = transfered.get(); if (read != data.length) { log.error("unexpected length returned on bulk: {}", read); } return data; }
From source file:com.facebook.infrastructure.net.UdpConnection.java
public boolean write(Message message, EndPoint to) throws IOException { boolean bVal = true; ByteArrayOutputStream bos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(bos); Message.serializer().serialize(message, dos); byte[] data = bos.toByteArray(); if (data.length > 0) { logger_.trace("Size of Gossip packet " + data.length); byte[] protocol = BasicUtilities.intToByteArray(protocol_); ByteBuffer buffer = ByteBuffer.allocate(data.length + protocol.length); buffer.put(protocol); buffer.put(data);//from w w w. j a v a 2 s . c o m buffer.flip(); int n = socketChannel_.send(buffer, to.getInetAddress()); if (n == 0) { bVal = false; } } return bVal; }
From source file:edu.tamu.tcat.crypto.bouncycastle.SecureTokenImpl.java
private byte[] createToken(ByteBuffer content) throws NoSuchAlgorithmException { byte[] bytes = new byte[content.remaining()]; ByteBuffer tokenBytes = ByteBuffer.wrap(bytes); content.position(0);//from w ww .j a v a 2 s . co m tokenBytes.put(content); return bytes; }
From source file:gobblin.util.io.StreamUtilsTest.java
private void verifyBuffer(byte[] srcBytes, ByteBuffer buffer) throws IOException { buffer.put(srcBytes); buffer.flip();/*from w ww . jav a 2s . c om*/ ByteArrayOutputStream bOs = new ByteArrayOutputStream(); StreamUtils.byteBufferToOutputStream(buffer, bOs); Assert.assertEquals(bOs.toByteArray(), srcBytes); bOs = new ByteArrayOutputStream(); buffer.rewind(); // consume one character from the buf; make sure it is not included in the output by // byteBufferToOutputStream buffer.getChar(); StreamUtils.byteBufferToOutputStream(buffer, bOs); byte[] offByTwo = bOs.toByteArray(); Assert.assertEquals(offByTwo.length, srcBytes.length - 2); for (int i = 0; i < offByTwo.length; i++) { Assert.assertEquals(offByTwo[i], srcBytes[i + 2]); } }