List of usage examples for java.nio ByteBuffer put
public ByteBuffer put(byte[] src, int off, int len)
From source file:experts.net.ip6.ULUA.java
/** * Create Interface ID by the Modified EUI-64 format (RFC 4291) * * @param macAddr/*from w ww . ja v a 2 s.c o m*/ * MAC (NIC) address */ public final void createInterfaceIDByEUI64(byte[] macAddr) { ByteBuffer buf = ByteBuffer.allocate(INTERFACE_ID_LENGTH); buf.put(macAddr, 0, 3).putShort(ADDITIONAL_VALUES).put(macAddr, 3, 3); buf.put(0, (byte) (buf.get(0) ^ 0x02)); interfaceID = toList(buf); }
From source file:com.ery.ertc.estorm.util.ByteBufferArray.java
/** * Transfers bytes from the given source array into this buffer array * //w ww . j av a 2 s .c om * @param start * start offset of this buffer array * @param len * The maximum number of bytes to be read from the given array * @param srcArray * The array from which bytes are to be read * @param srcOffset * The offset within the given array of the first byte to be read */ public void putMultiple(long start, int len, byte[] srcArray, int srcOffset) { multiple(start, len, srcArray, srcOffset, new Visitor() { public void visit(ByteBuffer bb, byte[] array, int arrayIdx, int len) { bb.put(array, arrayIdx, len); } }); }
From source file:tachyon.master.RawTables.java
/** * Update the metadata of the specified raw table. It will check if the table exists. * * @param tableId The id of the raw table * @param metadata The new metadata of the raw table * @throws TachyonException/*ww w .ja v a 2s.c o m*/ */ // TODO add version number. public synchronized void updateMetadata(int tableId, ByteBuffer metadata) throws TachyonException { Pair<Integer, ByteBuffer> data = mData.get(tableId); if (null == data) { throw new TachyonException("The raw table " + tableId + " does not exist."); } if (metadata == null) { data.setSecond(ByteBuffer.allocate(0)); } else { long maxVal = mTachyonConf.getBytes(Constants.MAX_TABLE_METADATA_BYTE, 0L); if (metadata.limit() - metadata.position() >= maxVal) { throw new TachyonException("Too big table metadata: " + metadata.toString()); } ByteBuffer tMetadata = ByteBuffer.allocate(metadata.limit() - metadata.position()); tMetadata.put(metadata.array(), metadata.position(), metadata.limit() - metadata.position()); tMetadata.flip(); data.setSecond(tMetadata); } }
From source file:net.fenyo.mail4hotspot.dns.Msg.java
public String debugContent() { String retval = ""; if (input_buffer.length == 0) return "invalid null size"; if (input_buffer[0] == 0) { // UTF-8 message type retval += "type=text;"; final ByteBuffer bb = ByteBuffer.allocate(input_buffer.length - 1); bb.put(input_buffer, 1, input_buffer.length - 1); bb.position(0);//from ww w .ja v a 2s . c o m final String query = Charset.forName("UTF-8").decode(bb).toString(); retval += "query=" + query + ";"; } else { // binary message type retval += "type=binary;"; final ByteBuffer bb = ByteBuffer.allocate(input_buffer[0]); bb.put(input_buffer, 1, input_buffer[0]); bb.position(0); final String query = Charset.forName("UTF-8").decode(bb).toString(); retval += "query=" + query + ";"; } return retval; }
From source file:net.fenyo.mail4hotspot.dns.Msg.java
private byte[] process(final AdvancedServices advancedServices, final Inet4Address address) throws GeneralException { // log.debug("processing message of type " + input_buffer[0]); // for (byte b : input_buffer) log.debug("received byte: [" + b + "]"); if (input_buffer.length == 0) throw new GeneralException("invalid size"); if (input_buffer[0] == 0) { // UTF-8 message type final ByteBuffer bb = ByteBuffer.allocate(input_buffer.length - 1); bb.put(input_buffer, 1, input_buffer.length - 1); bb.position(0);//ww w. j a va 2 s.c om final String query = Charset.forName("UTF-8").decode(bb).toString(); // log.debug("RECEIVED query: [" + query + "]"); final String reply = advancedServices.processQueryFromClient(query, address); // this buffer may not be backed by an accessible byte array, so we do not use Charset.forName("UTF-8").encode(reply).array() to fill output_buffer final ByteBuffer ob = Charset.forName("UTF-8").encode(reply); ob.get(output_buffer = new byte[ob.limit()]); output_size = output_buffer.length; } else { // binary message type // log.debug("processing binary message"); final ByteBuffer bb = ByteBuffer.allocate(input_buffer[0]); bb.put(input_buffer, 1, input_buffer[0]); bb.position(0); final String query = Charset.forName("UTF-8").decode(bb).toString(); // log.debug("RECEIVED query: [" + query + "]"); final BinaryMessageReply reply = advancedServices.processBinaryQueryFromClient(query, Arrays.copyOfRange(input_buffer, input_buffer[0] + 1, input_buffer.length), address); // this buffer may not be backed by an accessible byte array, so we do not use Charset.forName("UTF-8").encode(reply).array() to fill string_part final ByteBuffer ob = Charset.forName("UTF-8").encode(reply.reply_string); final byte[] string_part = new byte[ob.limit()]; ob.get(string_part); if (string_part.length > 255) throw new GeneralException("string_part too long"); output_buffer = new byte[string_part.length + reply.reply_data.length + 1]; output_buffer[0] = (byte) string_part.length; for (int i = 0; i < string_part.length; i++) output_buffer[i + 1] = string_part[i]; for (int i = 0; i < reply.reply_data.length; i++) output_buffer[string_part.length + i + 1] = reply.reply_data[i]; output_size = output_buffer.length; } synchronized (compressed) { // http://docs.oracle.com/javase/7/docs/api/java/util/zip/Deflater.html#deflate(byte[]) // log.debug("processing binary message: length before compressing: " + output_buffer.length); final Deflater compresser = new Deflater(); compresser.setInput(output_buffer); compresser.finish(); final int nbytes = compresser.deflate(compressed); // log.debug("RET: " + nbytes); // log.debug("COMPRESSED: " + compressed.length); // log.debug("processing binary message: length after compressing: " + nbytes); if (compressed.length == nbytes) { log.error("compressed buffer too small..."); throw new GeneralException("compressed buffer too small..."); } output_buffer = Arrays.copyOf(compressed, nbytes); output_size = output_buffer.length; } synchronized (is_processed) { is_processed = true; } return new byte[] { 'E', 0 }; // 'E'rror 0 == OK }
From source file:com.linkedin.databus.core.DbusEventV1.java
private static int serializeFullEvent(long key, ByteBuffer serializationBuffer, DbusEventInfo eventInfo, byte[] attributes) { ByteBuffer valueBuffer = eventInfo.getValueByteBuffer(); int payloadLen = (valueBuffer == null) ? eventInfo.getValueLength() : valueBuffer.remaining(); int startPosition = serializationBuffer.position(); serializationBuffer.put(DbusEventFactory.DBUS_EVENT_V1).putInt(HeaderCrcDefault) .putInt(LongKeyValueOffset + payloadLen).put(attributes).putLong(eventInfo.getSequenceId()) .putShort(eventInfo.getpPartitionId()).putShort(eventInfo.getlPartitionId()) .putLong(eventInfo.getTimeStampInNanos()).putShort(eventInfo.getSrcId()) .put(eventInfo.getSchemaId(), 0, 16).putInt(HeaderCrcDefault).putLong(key); if (valueBuffer != null) { // note. put will advance position. In the case of wrapped byte[] it is ok, in the case of // ByteBuffer this is actually a read only copy of the buffer passed in. serializationBuffer.put(valueBuffer); }/*from w w w. j a v a2s.c o m*/ int stopPosition = serializationBuffer.position(); long valueCrc = ByteBufferCRC32.getChecksum(serializationBuffer, startPosition + LongKeyValueOffset, payloadLen); Utils.putUnsignedInt(serializationBuffer, startPosition + ValueCrcOffset, valueCrc); if (eventInfo.isAutocommit()) { //TODO (DDSDBUS-60): Medium : can avoid new here DbusEventV1 e = new DbusEventV1(serializationBuffer, startPosition); e.applyCrc(); } serializationBuffer.position(stopPosition); return (stopPosition - startPosition); }
From source file:com.flexive.core.stream.BinaryDownloadProtocol.java
/** * {@inheritDoc}/*from w w w .j a v a 2 s. c o m*/ */ @Override public boolean sendStream(ByteBuffer buffer) throws IOException { if (buffer.remaining() <= 0 || bin == null) return true; //should not happen but possible ... if (pre_read > 0) { buffer.put(_buffer, 0, pre_read); //size should not matter since its only 100 bytes pre_read = 0; } int max = Math.min(buffer.remaining(), _buffer.length); int read = bin.read(_buffer, 0, max); if (read == -1) { return false; } buffer.put(_buffer, 0, read); return true; }
From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.safe.TSafeDeflateJsonQueryHandler.java
private ByteBuffer fuse(List<byte[]> buffers, final int length) { //fuses the buffers into a single array of the target length final ByteBuffer bb = ByteBuffer.allocate(length); for (byte[] buffer : buffers) { if (buffer.length > length - bb.position()) { bb.put(buffer, 0, length - bb.position()); } else {//from w w w. ja va 2 s. c o m bb.put(buffer); } } //important bb.flip(); return bb; }
From source file:Main.java
private static ByteBuffer convertImageData(BufferedImage bufferedImage) { ByteBuffer imageBuffer; WritableRaster raster;//from ww w. jav a 2 s . co m BufferedImage texImage; // for a texture if (bufferedImage.getColorModel().hasAlpha()) { raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(), bufferedImage.getHeight(), 4, null); texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable<Object, Object>()); } else { raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, bufferedImage.getWidth(), bufferedImage.getHeight(), 3, null); texImage = new BufferedImage(glColorModel, raster, false, new Hashtable<Object, Object>()); } // copy the source image into the produced image Graphics g = texImage.getGraphics(); g.setColor(new Color(0f, 0f, 0f, 0f)); g.fillRect(0, 0, bufferedImage.getWidth(), bufferedImage.getHeight()); g.drawImage(bufferedImage, 0, 0, null); // build a byte buffer from the temporary image // that be used by OpenGL to produce a texture. byte[] data = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData(); imageBuffer = ByteBuffer.allocateDirect(data.length); imageBuffer.order(ByteOrder.nativeOrder()); imageBuffer.put(data, 0, data.length); imageBuffer.flip(); return imageBuffer; }
From source file:de.hofuniversity.iisys.neo4j.websock.query.encoding.unsafe.DeflateJsonQueryHandler.java
private ByteBuffer fuse(final int length) { //fuses the buffers into a single array of the target length final ByteBuffer bb = ByteBuffer.allocate(length); for (byte[] buffer : fBuffers) { if (buffer.length > length - bb.position()) { bb.put(buffer, 0, length - bb.position()); } else {//w ww . jav a2 s . c om bb.put(buffer); } } //important bb.flip(); fBuffers.clear(); return bb; }