List of usage examples for java.nio ByteBuffer put
public ByteBuffer put(ByteBuffer src)
From source file:de.rwhq.btree.LeafNode.java
/** * * adds an entry to this LeafNodes rawPage, does not sync! * * @param key// w w w. j a v a 2s . c om * @param value */ private void addEntry(final K key, final V value) { final ByteBuffer buf = rawPage().bufferForWriting(0); int offset = offsetOfKey(key, true); if (offset == -1) { offset = offsetBehindLastEntry(); } else { // move everything including pos backwards System.arraycopy(buf.array(), offset, buf.array(), offset + keySerializer.getSerializedLength() + valueSerializer.getSerializedLength(), buf.capacity() - (offset + keySerializer.getSerializedLength() + valueSerializer.getSerializedLength())); } // insert both buf.position(offset); buf.put(keySerializer.serialize(key)); buf.put(valueSerializer.serialize(value)); setNumberOfEntries(getNumberOfEntries() + 1); }
From source file:edu.hawaii.soest.kilonalu.adam.AdamDispatcher.java
/** * A method that executes the streaming of data from the source to the RBNB * server after all configuration of settings, connections to hosts, and * thread initiatizing occurs. This method contains the detailed code for * streaming the data and interpreting the stream. *//*from ww w.j a va2 s.co m*/ protected boolean execute() { logger.info("AdamDispatcher.execute() called."); // do not execute the stream if there is no connection if (!isConnected()) return false; boolean failed = false; // while data are being sent, read them into the buffer try { // Create a buffer that will store the sample bytes as they are read byte[] bufferArray = new byte[getBufferSize()]; // and a ByteBuffer used to transfer the bytes to the parser ByteBuffer sampleBuffer = ByteBuffer.allocate(getBufferSize()); this.datagramPacket = new DatagramPacket(bufferArray, bufferArray.length); // while there are bytes to read from the socket ... while (!failed) { // receive any incoming UDP packets and parse the data payload datagramSocket.receive(this.datagramPacket); logger.debug("Host: " + datagramPacket.getAddress() + " data: " + new String(Hex.encodeHex(datagramPacket.getData()))); // the address seems to be returned with a leading slash (/). Trim it. String datagramAddress = datagramPacket.getAddress().toString().replaceAll("/", ""); sampleBuffer.put(datagramPacket.getData()); // Given the IP address of the source UDP packet and the data ByteBuffer, // find the correct source in the sourceMap hash and process the data if (sourceMap.get(datagramAddress) != null) { AdamSource source = sourceMap.get(datagramAddress); // process the data using the AdamSource driver source.process(datagramAddress, this.xmlConfiguration, sampleBuffer); } else { logger.debug("There is no configuration information for " + "the ADAM module at " + datagramAddress + ". Please add the configuration to the " + "sensor.properties.xml configuration file."); } sampleBuffer.clear(); } // end while (more socket bytes to read) disconnect(); // } catch (IOException e) { // handle exceptions // In the event of an i/o exception, log the exception, and allow execute() // to return false, which will prompt a retry. failed = true; e.printStackTrace(); return !failed; } return !failed; }
From source file:com.healthmarketscience.jackcess.Column.java
/** * Writes the sort order info to the given buffer at the current position. *///from w w w .ja v a 2 s .c o m private static void writeSortOrder(ByteBuffer buffer, SortOrder sortOrder, JetFormat format) { if (sortOrder == null) { sortOrder = format.DEFAULT_SORT_ORDER; } buffer.putShort(sortOrder.getValue()); if (format.SIZE_SORT_ORDER == 4) { buffer.put((byte) 0x00); // unknown buffer.put(sortOrder.getVersion()); } }
From source file:com.healthmarketscience.jackcess.impl.IndexData.java
/** * Writes the data page info to the given buffer. *//*from ww w.j a v a2 s.co m*/ protected static void writeDataPage(ByteBuffer buffer, DataPage dataPage, int tdefPageNumber, JetFormat format) throws IOException { buffer.put(dataPage.isLeaf() ? PageTypes.INDEX_LEAF : PageTypes.INDEX_NODE); //Page type buffer.put((byte) 0x01); //Unknown buffer.putShort((short) 0); //Free space buffer.putInt(tdefPageNumber); buffer.putInt(0); //Unknown buffer.putInt(dataPage.getPrevPageNumber()); //Prev page buffer.putInt(dataPage.getNextPageNumber()); //Next page buffer.putInt(dataPage.getChildTailPageNumber()); //ChildTail page byte[] entryPrefix = dataPage.getEntryPrefix(); buffer.putShort((short) entryPrefix.length); // entry prefix byte count buffer.put((byte) 0); //Unknown byte[] entryMask = new byte[format.SIZE_INDEX_ENTRY_MASK]; // first entry includes the prefix int totalSize = entryPrefix.length; for (Entry entry : dataPage.getEntries()) { totalSize += (entry.size() - entryPrefix.length); int idx = totalSize / 8; entryMask[idx] |= (1 << (totalSize % 8)); } buffer.put(entryMask); // first entry includes the prefix buffer.put(entryPrefix); for (Entry entry : dataPage.getEntries()) { entry.write(buffer, entryPrefix); } // update free space buffer.putShort(2, (short) (format.PAGE_SIZE - buffer.position())); }
From source file:com.healthmarketscience.jackcess.impl.TableImpl.java
/** * Writes the page header for a table definition page * @param buffer Buffer to write to// ww w .jav a 2 s . c o m */ private static void writeTablePageHeader(ByteBuffer buffer) { buffer.put(PageTypes.TABLE_DEF); //Page type buffer.put((byte) 0x01); //Unknown buffer.put((byte) 0); //Unknown buffer.put((byte) 0); //Unknown buffer.putInt(0); //Next TDEF page pointer }
From source file:com.koda.integ.hbase.blockcache.OffHeapBlockCache.java
/** * Store external with codec./*w ww . jav a2 s. com*/ * Format: * 0..3 - total record size (-4) * 4..7 - size of a key in bytes (16 if use hash128) * 8 .. x - key data * x+1 ..x+1- IN_MEMORY flag ( 1- in memory, 0 - not) * x+2 ... block, serialized and compressed * * @param blockName the block name * @param buf the buf * @param inMemory the in memory * @throws IOException Signals that an I/O exception has occurred. */ private void storeExternalWithCodec(String blockName, Cacheable buf, boolean inMemory) throws IOException { // If external storage is disable - bail out if (overflowExtEnabled == false) { return; } byte[] hashed = Utils.hash128(blockName); ByteBuffer buffer = extStorageCache.getLocalBufferWithAddress().getBuffer(); deserializer.set(buf.getDeserializer()); SerDe serde = extStorageCache.getSerDe(); Codec codec = extStorageCache.getCompressionCodec(); buffer.clear(); buffer.position(4); // Save key buffer.putInt(hashed.length); buffer.put(hashed); buffer.put(inMemory ? (byte) 1 : (byte) 0); if (buf != null) { serde.writeCompressed(buffer, buf, codec); int pos = buffer.position(); buffer.putInt(0, pos - 4); } buffer.flip(); StorageHandle handle = storage.storeData(buffer); try { // WE USE byte array as a key extStorageCache.put(hashed, handle.toBytes()); } catch (Exception e) { throw new IOException(e); } }
From source file:com.healthmarketscience.jackcess.impl.TableImpl.java
/** * Writes the given name into the given buffer in the format as expected by * {@link #readName}./* www. j a v a 2 s. c om*/ */ static void writeName(ByteBuffer buffer, String name, Charset charset) { ByteBuffer encName = ColumnImpl.encodeUncompressedText(name, charset); buffer.putShort((short) encName.remaining()); buffer.put(encName); }
From source file:com.healthmarketscience.jackcess.Table.java
/** * @param buffer Buffer to write to//ww w .j a v a2 s. com * @param columns List of Columns in the table */ private static void writeTableDefinitionHeader(TableCreator creator, ByteBuffer buffer, int totalTableDefSize) throws IOException { List<Column> columns = creator.getColumns(); //Start writing the tdef writeTablePageHeader(buffer); buffer.putInt(totalTableDefSize); //Length of table def buffer.putInt(MAGIC_TABLE_NUMBER); // seemingly constant magic value buffer.putInt(0); //Number of rows buffer.putInt(0); //Last Autonumber buffer.put((byte) 1); // this makes autonumbering work in access for (int i = 0; i < 15; i++) { //Unknown buffer.put((byte) 0); } buffer.put(Table.TYPE_USER); //Table type buffer.putShort((short) columns.size()); //Max columns a row will have buffer.putShort(Column.countVariableLength(columns)); //Number of variable columns in table buffer.putShort((short) columns.size()); //Number of columns in table buffer.putInt(creator.getLogicalIndexCount()); //Number of logical indexes in table buffer.putInt(creator.getIndexCount()); //Number of indexes in table buffer.put((byte) 0); //Usage map row number ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Usage map page number buffer.put((byte) 1); //Free map row number ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); //Free map page number if (LOG.isDebugEnabled()) { int position = buffer.position(); buffer.rewind(); LOG.debug("Creating new table def block:\n" + ByteUtil.toHexString(buffer, creator.getFormat().SIZE_TDEF_HEADER)); buffer.position(position); } }
From source file:com.healthmarketscience.jackcess.impl.ColumnImpl.java
/** * Writes a GUID value.//w w w . j a v a 2 s . c om */ private static void writeGUIDValue(ByteBuffer buffer, Object value) throws IOException { Matcher m = GUID_PATTERN.matcher(toCharSequence(value)); if (!m.matches()) { throw new IOException("Invalid GUID: " + value); } ByteBuffer origBuffer = null; byte[] tmpBuf = null; if (buffer.order() != ByteOrder.BIG_ENDIAN) { // write to a temp buf so we can do some swapping below origBuffer = buffer; tmpBuf = new byte[16]; buffer = ByteBuffer.wrap(tmpBuf); } ByteUtil.writeHexString(buffer, m.group(1)); ByteUtil.writeHexString(buffer, m.group(2)); ByteUtil.writeHexString(buffer, m.group(3)); ByteUtil.writeHexString(buffer, m.group(4)); ByteUtil.writeHexString(buffer, m.group(5)); if (tmpBuf != null) { // the first 3 guid components are integer components which need to // respect endianness, so swap 4-byte int, 2-byte int, 2-byte int ByteUtil.swap4Bytes(tmpBuf, 0); ByteUtil.swap2Bytes(tmpBuf, 4); ByteUtil.swap2Bytes(tmpBuf, 6); origBuffer.put(tmpBuf); } }
From source file:de.rwhq.btree.InnerNode.java
/** * @param rawKeys/* ww w .j a v a 2s . c om*/ * @param pageIds * @param fromId * @return */ public int bulkInitialize(final ArrayList<byte[]> rawKeys, final ArrayList<Integer> pageIds, final int fromId) { if (pageIds.size() < (fromId + 2) || rawKeys.size() != (pageIds.size() - 1)) throw new IllegalArgumentException( "for bulkinsert, you must have at least 2 page ids and keys.size() == (pageIds.size() - 1)\n" + "pageIds.size()=" + pageIds.size() + ";fromId=" + fromId + ";rawKeys.size()=" + rawKeys.size()); final int fromId2 = fromId; initialize(); final ByteBuffer buf = rawPage().bufferForWriting(Header.size()); buf.putInt(pageIds.get(fromId2)); final int requiredSpace = Integer.SIZE / 8 + rawKeys.get(0).length; final int spaceForEntries = buf.remaining() / requiredSpace; final int totalEntriesToInsert = (pageIds.size() - fromId - 1); int entriesToInsert = spaceForEntries < totalEntriesToInsert ? spaceForEntries : totalEntriesToInsert; // make sure that not exactly one pageId remains, because that can't be inserted alone in the next // InnerNode. == 2 because final int remaining = pageIds.size() - fromId - (entriesToInsert + 1); if (remaining == 1) entriesToInsert--; for (int i = 0; i < entriesToInsert; i++) { // System.out.println("fetching rawKey " + (fromId + i) + " from array length " + rawKeys.size() + " with i=" + i); buf.put(rawKeys.get(fromId + i)); // fromId + 1 - 1 +i //LOG.debug("insert key: " + keySerializer.deserialize(rawKeys.get(fromId + i))); buf.putInt(pageIds.get(fromId + 1 + i)); } setNumberOfKeys(entriesToInsert); rawPage.sync(); return entriesToInsert + 1; // page ids }