List of usage examples for java.nio ByteBuffer putShort
public abstract ByteBuffer putShort(short value);
From source file:com.healthmarketscience.jackcess.Column.java
/** * Writes the header info for a long value page. *//*from w ww .j av a 2 s . c om*/ private void writeLongValueHeader(ByteBuffer lvalPage) { lvalPage.put(PageTypes.DATA); //Page type lvalPage.put((byte) 1); //Unknown lvalPage.putShort((short) getFormat().DATA_PAGE_INITIAL_FREE_SPACE); //Free space lvalPage.put((byte) 'L'); lvalPage.put((byte) 'V'); lvalPage.put((byte) 'A'); lvalPage.put((byte) 'L'); lvalPage.putInt(0); //unknown lvalPage.putShort((short) 0); // num rows in page }
From source file:com.healthmarketscience.jackcess.impl.TableImpl.java
/** * Writes a new table defined by the given TableCreator to the database. * @usage _advanced_method_//from w ww .j a va 2s .c o m */ protected static void writeTableDefinition(TableCreator creator) throws IOException { // first, create the usage map page createUsageMapDefinitionBuffer(creator); // next, determine how big the table def will be (in case it will be more // than one page) JetFormat format = creator.getFormat(); int idxDataLen = (creator.getIndexCount() * (format.SIZE_INDEX_DEFINITION + format.SIZE_INDEX_COLUMN_BLOCK)) + (creator.getLogicalIndexCount() * format.SIZE_INDEX_INFO_BLOCK); int colUmapLen = creator.getLongValueColumns().size() * 10; int totalTableDefSize = format.SIZE_TDEF_HEADER + (format.SIZE_COLUMN_DEF_BLOCK * creator.getColumns().size()) + idxDataLen + colUmapLen + format.SIZE_TDEF_TRAILER; // total up the amount of space used by the column and index names (2 // bytes per char + 2 bytes for the length) for (ColumnBuilder col : creator.getColumns()) { int nameByteLen = (col.getName().length() * JetFormat.TEXT_FIELD_UNIT_SIZE); totalTableDefSize += nameByteLen + 2; } for (IndexBuilder idx : creator.getIndexes()) { int nameByteLen = (idx.getName().length() * JetFormat.TEXT_FIELD_UNIT_SIZE); totalTableDefSize += nameByteLen + 2; } // now, create the table definition PageChannel pageChannel = creator.getPageChannel(); ByteBuffer buffer = PageChannel.createBuffer(Math.max(totalTableDefSize, format.PAGE_SIZE)); writeTableDefinitionHeader(creator, buffer, totalTableDefSize); if (creator.hasIndexes()) { // index row counts IndexData.writeRowCountDefinitions(creator, buffer); } // column definitions ColumnImpl.writeDefinitions(creator, buffer); if (creator.hasIndexes()) { // index and index data definitions IndexData.writeDefinitions(creator, buffer); IndexImpl.writeDefinitions(creator, buffer); } // write long value column usage map references for (ColumnBuilder lvalCol : creator.getLongValueColumns()) { buffer.putShort(lvalCol.getColumnNumber()); TableCreator.ColumnState colState = creator.getColumnState(lvalCol); // owned pages umap (both are on same page) buffer.put(colState.getUmapOwnedRowNumber()); ByteUtil.put3ByteInt(buffer, colState.getUmapPageNumber()); // free space pages umap buffer.put(colState.getUmapFreeRowNumber()); ByteUtil.put3ByteInt(buffer, colState.getUmapPageNumber()); } //End of tabledef buffer.put((byte) 0xff); buffer.put((byte) 0xff); // write table buffer to database if (totalTableDefSize <= format.PAGE_SIZE) { // easy case, fits on one page buffer.putShort(format.OFFSET_FREE_SPACE, (short) (buffer.remaining() - 8)); // overwrite page free space // Write the tdef page to disk. pageChannel.writePage(buffer, creator.getTdefPageNumber()); } else { // need to split across multiple pages ByteBuffer partialTdef = pageChannel.createPageBuffer(); buffer.rewind(); int nextTdefPageNumber = PageChannel.INVALID_PAGE_NUMBER; while (buffer.hasRemaining()) { // reset for next write partialTdef.clear(); if (nextTdefPageNumber == PageChannel.INVALID_PAGE_NUMBER) { // this is the first page. note, the first page already has the // page header, so no need to write it here nextTdefPageNumber = creator.getTdefPageNumber(); } else { // write page header writeTablePageHeader(partialTdef); } // copy the next page of tdef bytes int curTdefPageNumber = nextTdefPageNumber; int writeLen = Math.min(partialTdef.remaining(), buffer.remaining()); partialTdef.put(buffer.array(), buffer.position(), writeLen); ByteUtil.forward(buffer, writeLen); if (buffer.hasRemaining()) { // need a next page nextTdefPageNumber = pageChannel.allocateNewPage(); partialTdef.putInt(format.OFFSET_NEXT_TABLE_DEF_PAGE, nextTdefPageNumber); } // update page free space partialTdef.putShort(format.OFFSET_FREE_SPACE, (short) (partialTdef.remaining() - 8)); // overwrite page free space // write partial page to disk pageChannel.writePage(partialTdef, curTdefPageNumber); } } }
From source file:com.healthmarketscience.jackcess.Table.java
/** * Create a new data page//from w ww.j ava 2s .c om * @return Page number of the new page */ private ByteBuffer newDataPage() throws IOException { if (LOG.isDebugEnabled()) { LOG.debug("Creating new data page"); } ByteBuffer dataPage = _addRowBufferH.setNewPage(getPageChannel()); dataPage.put(PageTypes.DATA); //Page type dataPage.put((byte) 1); //Unknown dataPage.putShort((short) getFormat().DATA_PAGE_INITIAL_FREE_SPACE); //Free space in this page dataPage.putInt(_tableDefPageNumber); //Page pointer to table definition dataPage.putInt(0); //Unknown dataPage.putShort((short) 0); //Number of rows on this page int pageNumber = _addRowBufferH.getPageNumber(); getPageChannel().writePage(dataPage, pageNumber); _ownedPages.addPageNumber(pageNumber); _freeSpacePages.addPageNumber(pageNumber); return dataPage; }
From source file:com.healthmarketscience.jackcess.impl.TableImpl.java
/** * Create the usage map definition page buffer. The "used pages" map is in * row 0, the "pages with free space" map is in row 1. Index usage maps are * in subsequent rows./*from ww w. j a v a2 s . c o m*/ */ private static void createUsageMapDefinitionBuffer(TableCreator creator) throws IOException { List<ColumnBuilder> lvalCols = creator.getLongValueColumns(); // 2 table usage maps plus 1 for each index and 2 for each lval col int indexUmapEnd = 2 + creator.getIndexCount(); int umapNum = indexUmapEnd + (lvalCols.size() * 2); JetFormat format = creator.getFormat(); int umapRowLength = format.OFFSET_USAGE_MAP_START + format.USAGE_MAP_TABLE_BYTE_LENGTH; int umapSpaceUsage = getRowSpaceUsage(umapRowLength, format); PageChannel pageChannel = creator.getPageChannel(); int umapPageNumber = PageChannel.INVALID_PAGE_NUMBER; ByteBuffer umapBuf = null; int freeSpace = 0; int rowStart = 0; int umapRowNum = 0; for (int i = 0; i < umapNum; ++i) { if (umapBuf == null) { // need new page for usage maps if (umapPageNumber == PageChannel.INVALID_PAGE_NUMBER) { // first umap page has already been reserved umapPageNumber = creator.getUmapPageNumber(); } else { // need another umap page umapPageNumber = creator.reservePageNumber(); } freeSpace = format.DATA_PAGE_INITIAL_FREE_SPACE; umapBuf = pageChannel.createPageBuffer(); umapBuf.put(PageTypes.DATA); umapBuf.put((byte) 0x1); //Unknown umapBuf.putShort((short) freeSpace); //Free space in page umapBuf.putInt(0); //Table definition umapBuf.putInt(0); //Unknown umapBuf.putShort((short) 0); //Number of records on this page rowStart = findRowEnd(umapBuf, 0, format) - umapRowLength; umapRowNum = 0; } umapBuf.putShort(getRowStartOffset(umapRowNum, format), (short) rowStart); if (i == 0) { // table "owned pages" map definition umapBuf.put(rowStart, UsageMap.MAP_TYPE_REFERENCE); } else if (i == 1) { // table "free space pages" map definition umapBuf.put(rowStart, UsageMap.MAP_TYPE_INLINE); } else if (i < indexUmapEnd) { // index umap int indexIdx = i - 2; IndexBuilder idx = creator.getIndexes().get(indexIdx); // allocate root page for the index int rootPageNumber = pageChannel.allocateNewPage(); // stash info for later use TableCreator.IndexState idxState = creator.getIndexState(idx); idxState.setRootPageNumber(rootPageNumber); idxState.setUmapRowNumber((byte) umapRowNum); idxState.setUmapPageNumber(umapPageNumber); // index map definition, including initial root page umapBuf.put(rowStart, UsageMap.MAP_TYPE_INLINE); umapBuf.putInt(rowStart + 1, rootPageNumber); umapBuf.put(rowStart + 5, (byte) 1); } else { // long value column umaps int lvalColIdx = i - indexUmapEnd; int umapType = lvalColIdx % 2; lvalColIdx /= 2; ColumnBuilder lvalCol = lvalCols.get(lvalColIdx); TableCreator.ColumnState colState = creator.getColumnState(lvalCol); umapBuf.put(rowStart, UsageMap.MAP_TYPE_INLINE); if ((umapType == 1) && (umapPageNumber != colState.getUmapPageNumber())) { // we want to force both usage maps for a column to be on the same // data page, so just discard the previous one we wrote --i; umapType = 0; } if (umapType == 0) { // lval column "owned pages" usage map colState.setUmapOwnedRowNumber((byte) umapRowNum); colState.setUmapPageNumber(umapPageNumber); } else { // lval column "free space pages" usage map (always on same page) colState.setUmapFreeRowNumber((byte) umapRowNum); } } rowStart -= umapRowLength; freeSpace -= umapSpaceUsage; ++umapRowNum; if ((freeSpace <= umapSpaceUsage) || (i == (umapNum - 1))) { // finish current page umapBuf.putShort(format.OFFSET_FREE_SPACE, (short) freeSpace); umapBuf.putShort(format.OFFSET_NUM_ROWS_ON_DATA_PAGE, (short) umapRowNum); pageChannel.writePage(umapBuf, umapPageNumber); umapBuf = null; } } }
From source file:com.healthmarketscience.jackcess.Table.java
/** * Serialize a row of Objects into a byte buffer. * //from w w w .j a va 2 s. com * @param rowArray row data, expected to be correct length for this table * @param buffer buffer to which to write the row data * @param minRowSize min size for result row * @param rawVarValues optional, pre-written values for var length columns * (enables re-use of previously written values). * @return the given buffer, filled with the row data */ private ByteBuffer createRow(Object[] rowArray, ByteBuffer buffer, int minRowSize, Map<Column, byte[]> rawVarValues) throws IOException { buffer.putShort(_maxColumnCount); NullMask nullMask = new NullMask(_maxColumnCount); //Fixed length column data comes first int fixedDataStart = buffer.position(); int fixedDataEnd = fixedDataStart; for (Column col : _columns) { if (col.isVariableLength()) { continue; } Object rowValue = col.getRowValue(rowArray); if (col.getType() == DataType.BOOLEAN) { if (Column.toBooleanValue(rowValue)) { //Booleans are stored in the null mask nullMask.markNotNull(col); } rowValue = null; } if (rowValue != null) { // we have a value to write nullMask.markNotNull(col); // remainingRowLength is ignored when writing fixed length data buffer.position(fixedDataStart + col.getFixedDataOffset()); buffer.put(col.write(rowValue, 0)); } // always insert space for the entire fixed data column length // (including null values), access expects the row to always be at least // big enough to hold all fixed values buffer.position(fixedDataStart + col.getFixedDataOffset() + col.getLength()); // keep track of the end of fixed data if (buffer.position() > fixedDataEnd) { fixedDataEnd = buffer.position(); } } // reposition at end of fixed data buffer.position(fixedDataEnd); // only need this info if this table contains any var length data if (_maxVarColumnCount > 0) { int maxRowSize = getFormat().MAX_ROW_SIZE; // figure out how much space remains for var length data. first, // account for already written space maxRowSize -= buffer.position(); // now, account for trailer space int trailerSize = (nullMask.byteSize() + 4 + (_maxVarColumnCount * 2)); maxRowSize -= trailerSize; // for each non-null long value column we need to reserve a small // amount of space so that we don't end up running out of row space // later by being too greedy for (Column varCol : _varColumns) { if ((varCol.getType().isLongValue()) && (varCol.getRowValue(rowArray) != null)) { maxRowSize -= getFormat().SIZE_LONG_VALUE_DEF; } } //Now write out variable length column data short[] varColumnOffsets = new short[_maxVarColumnCount]; int varColumnOffsetsIndex = 0; for (Column varCol : _varColumns) { short offset = (short) buffer.position(); Object rowValue = varCol.getRowValue(rowArray); if (rowValue != null) { // we have a value nullMask.markNotNull(varCol); byte[] rawValue = null; ByteBuffer varDataBuf = null; if (((rawValue = rawVarValues.get(varCol)) != null) && (rawValue.length <= maxRowSize)) { // save time and potentially db space, re-use raw value varDataBuf = ByteBuffer.wrap(rawValue); } else { // write column value varDataBuf = varCol.write(rowValue, maxRowSize); } maxRowSize -= varDataBuf.remaining(); if (varCol.getType().isLongValue()) { // we already accounted for some amount of the long value data // above. add that space back so we don't double count maxRowSize += getFormat().SIZE_LONG_VALUE_DEF; } buffer.put(varDataBuf); } // we do a loop here so that we fill in offsets for deleted columns while (varColumnOffsetsIndex <= varCol.getVarLenTableIndex()) { varColumnOffsets[varColumnOffsetsIndex++] = offset; } } // fill in offsets for any remaining deleted columns while (varColumnOffsetsIndex < varColumnOffsets.length) { varColumnOffsets[varColumnOffsetsIndex++] = (short) buffer.position(); } // record where we stopped writing int eod = buffer.position(); // insert padding if necessary padRowBuffer(buffer, minRowSize, trailerSize); buffer.putShort((short) eod); //EOD marker //Now write out variable length offsets //Offsets are stored in reverse order for (int i = _maxVarColumnCount - 1; i >= 0; i--) { buffer.putShort(varColumnOffsets[i]); } buffer.putShort(_maxVarColumnCount); //Number of var length columns } else { // insert padding for row w/ no var cols padRowBuffer(buffer, minRowSize, nullMask.byteSize()); } nullMask.write(buffer); //Null mask buffer.flip(); if (LOG.isDebugEnabled()) { LOG.debug("Creating new data block:\n" + ByteUtil.toHexString(buffer, buffer.limit())); } return buffer; }
From source file:edu.umass.cs.gigapaxos.paxospackets.RequestPacket.java
/** * The weird constant above is to try to avoid mistakes in the painful (but * totally worth it) byte'ification method below. Using bytes as opposed to * json strings makes a non-trivial difference (~2x over json-smart and >4x * over org.json. So we just chuck json libraries and use our own byte[] * serializer for select packets./* w w w . j av a 2 s .co m*/ * * The serialization overhead really matters most for RequestPacket and * AcceptPacket. Every request, even with batching, must be deserialized by * the coordinator and must be serialized back while sending out the * AcceptPacket. The critical path is the following at a coordinator and is * incurred at least in part even with batching for every request: (1) * receive request, (2) send accept, (3) receive accept_replies, (4) send * commit Accordingly, we use byteification for {@link RequestPacket}, * {@link AcceptPacket}, {@link BatchedAcceptReply} and * {@link BatchedCommit}. * * */ protected byte[] toBytes(boolean instrument) { // return cached value if already present if ((this.getType() == PaxosPacketType.REQUEST || this.getType() == PaxosPacketType.ACCEPT) && this.byteifiedSelf != null && !instrument) return this.byteifiedSelf; // check if we can use byteification at all; if not, use toString() if (!((BYTEIFICATION && IntegerMap.allInt()) || instrument)) { try { if (this.getType() == PaxosPacketType.REQUEST || this.getType() == PaxosPacketType.ACCEPT) return this.byteifiedSelf = this.toString().getBytes(CHARSET); // cache return this.toString().getBytes(CHARSET); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); return null; } } // else byteify try { int exactLength = 0; byte[] array = new byte[this.lengthEstimate()]; ByteBuffer bbuf = ByteBuffer.wrap(array); assert (bbuf.position() == 0); // paxospacket stuff super.toBytes(bbuf); int ppPos = bbuf.position(); // for assertion assert (bbuf.position() == ByteBuffer.wrap(array, SIZEOF_PAXOSPACKET_FIXED - 1, 1).get() + SIZEOF_PAXOSPACKET_FIXED) : bbuf.position() + " != " + ByteBuffer.wrap(array, SIZEOF_PAXOSPACKET_FIXED - 1, 1).get() + SIZEOF_PAXOSPACKET_FIXED; exactLength += (bbuf.position()); bbuf.putLong(this.requestID); bbuf.put(this.stop ? (byte) 1 : (byte) 0); exactLength += (Long.BYTES + 1); // addresses /* Note: 0 is ambiguous with wildcard address, but that's okay * because an incoming packet will never come with a wildcard * address. */ bbuf.put(this.clientAddress != null ? this.clientAddress.getAddress().getAddress() : new byte[4]); // 0 (not -1) means invalid port bbuf.putShort(this.clientAddress != null ? (short) this.clientAddress.getPort() : 0); /* Note: 0 is an ambiguous wildcard address that could also be a * legitimate value of the listening socket address. If the request * happens to have no listening address, we will end up assuming it * was received on the wildcard address. At worst, the matching for * the corresponding response back to the client can fail. */ bbuf.put(this.listenAddress != null ? this.listenAddress.getAddress().getAddress() : new byte[4]); // 0 (not -1) means invalid port bbuf.putShort(this.listenAddress != null ? (short) this.listenAddress.getPort() : 0); exactLength += 2 * (Integer.BYTES + Short.BYTES); // other non-final fields bbuf.putInt(this.entryReplica); bbuf.putLong(this.entryTime); bbuf.put(this.shouldReturnRequestValue ? (byte) 1 : (byte) 0); bbuf.putInt(this.forwardCount); exactLength += (Integer.BYTES + Long.BYTES + 1 + Integer.BYTES); // digest related fields: broadcasted, digest // whether this request was already broadcasted bbuf.put(this.broadcasted ? (byte) 1 : (byte) 0); exactLength += 1; assert (exactLength == // where parent left us off ppPos + SIZEOF_REQUEST_FIXED // for the three int fields not yet filled - 4 * Integer.BYTES) : exactLength + " != [" + ppPos + " + " + SIZEOF_REQUEST_FIXED + " - " + 4 * Integer.BYTES + "]"; // digest length and digest iteself bbuf.putInt(this.digest != null ? this.digest.length : 0); exactLength += Integer.BYTES; if (this.digest != null) bbuf.put(this.digest); exactLength += (this.digest != null ? this.digest.length : 0); // /////////// end of digest related fields ////////// // highly variable length fields // requestValue byte[] reqValBytes = this.requestValue != null ? this.requestValue.getBytes(CHARSET) : new byte[0]; bbuf.putInt(reqValBytes != null ? reqValBytes.length : 0); bbuf.put(reqValBytes); exactLength += (4 + reqValBytes.length); // responseValue byte[] respValBytes = this.responseValue != null ? this.responseValue.getBytes(CHARSET) : new byte[0]; bbuf.putInt(respValBytes != null ? respValBytes.length : 0); bbuf.put(respValBytes); exactLength += (4 + respValBytes.length); // batched requests batchSize|(length:batchedReqBytes)+ bbuf.putInt(this.batchSize()); exactLength += (4); if (this.batchSize() > 0) for (RequestPacket req : this.batched) { byte[] element = req.toBytes(); bbuf.putInt(element.length); bbuf.put(element); exactLength += (4 + element.length); } // bbuf.array() was a generous allocation byte[] exactBytes = new byte[exactLength]; bbuf.flip(); assert (bbuf.remaining() == exactLength) : bbuf.remaining() + " != " + exactLength; bbuf.get(exactBytes); if (this.getType() == PaxosPacketType.REQUEST) this.byteifiedSelf = exactBytes; return exactBytes; } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
From source file:com.healthmarketscience.jackcess.impl.TableImpl.java
/** * Create a new data page/*from w w w. j av a 2 s . co m*/ * @return Page number of the new page */ private ByteBuffer newDataPage() throws IOException { ByteBuffer dataPage = _addRowBufferH.setNewPage(getPageChannel()); dataPage.put(PageTypes.DATA); //Page type dataPage.put((byte) 1); //Unknown dataPage.putShort((short) getFormat().DATA_PAGE_INITIAL_FREE_SPACE); //Free space in this page dataPage.putInt(_tableDefPageNumber); //Page pointer to table definition dataPage.putInt(0); //Unknown dataPage.putShort((short) 0); //Number of rows on this page int pageNumber = _addRowBufferH.getPageNumber(); getPageChannel().writePage(dataPage, pageNumber); _ownedPages.addPageNumber(pageNumber); _freeSpacePages.addPageNumber(pageNumber); return dataPage; }
From source file:com.healthmarketscience.jackcess.IndexData.java
/** * Writes the index definitions into a table definition buffer. * @param buffer Buffer to write to/*ww w. j av a 2 s .co m*/ * @param indexes List of IndexBuilders to write definitions for */ protected static void writeDefinitions(TableCreator creator, ByteBuffer buffer) throws IOException { ByteBuffer rootPageBuffer = creator.getPageChannel().createPageBuffer(); writeDataPage(rootPageBuffer, SimpleIndexData.NEW_ROOT_DATA_PAGE, creator.getTdefPageNumber(), creator.getFormat()); for (IndexBuilder idx : creator.getIndexes()) { buffer.putInt(MAGIC_INDEX_NUMBER); // seemingly constant magic value // write column information (always MAX_COLUMNS entries) List<IndexBuilder.Column> idxColumns = idx.getColumns(); for (int i = 0; i < MAX_COLUMNS; ++i) { short columnNumber = COLUMN_UNUSED; byte flags = 0; if (i < idxColumns.size()) { // determine column info IndexBuilder.Column idxCol = idxColumns.get(i); flags = idxCol.getFlags(); // find actual table column number for (Column col : creator.getColumns()) { if (col.getName().equalsIgnoreCase(idxCol.getName())) { columnNumber = col.getColumnNumber(); break; } } if (columnNumber == COLUMN_UNUSED) { // should never happen as this is validated before throw new IllegalArgumentException("Column with name " + idxCol.getName() + " not found"); } } buffer.putShort(columnNumber); // table column number buffer.put(flags); // column flags (e.g. ordering) } TableCreator.IndexState idxState = creator.getIndexState(idx); buffer.put(idxState.getUmapRowNumber()); // umap row ByteUtil.put3ByteInt(buffer, creator.getUmapPageNumber()); // umap page // write empty root index page creator.getPageChannel().writePage(rootPageBuffer, idxState.getRootPageNumber()); buffer.putInt(idxState.getRootPageNumber()); buffer.putInt(0); // unknown buffer.put(idx.getFlags()); // index flags (unique, etc.) ByteUtil.forward(buffer, 5); // unknown } }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
private byte[] encodeDatalog(byte handle, byte reply) { ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + 2); buf.order(ByteOrder.BIG_ENDIAN); buf.putShort((short) 2); buf.putShort(ENDPOINT_DATALOG);//from w ww .java 2 s . c o m buf.put(reply); buf.put(handle); return buf.array(); }
From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java
@Override public byte[] encodeAppReorder(UUID[] uuids) { int length = 2 + uuids.length * LENGTH_UUID; ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + length); buf.order(ByteOrder.BIG_ENDIAN); buf.putShort((short) length); buf.putShort(ENDPOINT_APPREORDER);// w w w.j a v a 2s . c o m buf.put((byte) 0x01); buf.put((byte) uuids.length); for (UUID uuid : uuids) { buf.putLong(uuid.getMostSignificantBits()); buf.putLong(uuid.getLeastSignificantBits()); } return buf.array(); }