Example usage for java.nio ByteBuffer position

List of usage examples for java.nio ByteBuffer position

Introduction

In this page you can find the example usage for java.nio ByteBuffer position.

Prototype

public final int position() 

Source Link

Document

Returns the position of this buffer.

Usage

From source file:edu.hawaii.soest.kilonalu.ctd.SBE37Source.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  w ww  . jav a2s. c om*/
protected boolean execute() {
    logger.debug("SBE37Source.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 {

        this.socketChannel = getSocketConnection();

        // create four byte placeholders used to evaluate up to a four-byte 
        // window.  The FIFO layout looks like:
        //           -------------------------
        //   in ---> | One | Two |Three|Four |  ---> out
        //           -------------------------
        byte byteOne = 0x00, // set initial placeholder values
                byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00;

        // Create a buffer that will store the sample bytes as they are read
        ByteBuffer sampleBuffer = ByteBuffer.allocate(getBufferSize());

        // create a byte buffer to store bytes from the TCP stream
        ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize());

        // create a character string to store characters from the TCP stream
        StringBuilder responseString = new StringBuilder();

        // add a channel of data that will be pushed to the server.  
        // Each sample will be sent to the Data Turbine as an rbnb frame.
        ChannelMap rbnbChannelMap = new ChannelMap();
        int channelIndex = rbnbChannelMap.Add(getRBNBChannelName());

        // wake the instrument with an initial take sample command
        this.command = this.commandPrefix + getInstrumentID() + "TS" + this.commandSuffix;
        this.sentCommand = queryInstrument(this.command);

        // verify the instrument ID is correct
        while (getInstrumentID() == null) {
            // allow time for the instrument response
            streamingThread.sleep(2000);
            buffer.clear();
            // send the command and update the sentCommand status
            this.sentCommand = queryInstrument(this.command);

            // read the response into the buffer. Note that the streamed bytes
            // are 8-bit, not 16-bit Unicode characters.  Use the US-ASCII
            // encoding instead.
            while (this.socketChannel.read(buffer) != -1 || buffer.position() > 0) {

                buffer.flip();
                while (buffer.hasRemaining()) {
                    String nextCharacter = new String(new byte[] { buffer.get() }, "US-ASCII");
                    responseString.append(nextCharacter);
                }
                // look for the command line ending
                if (responseString.toString().indexOf("S>") > 0) {

                    // parse the ID from the idCommand response
                    int idStartIndex = responseString.indexOf("=") + 2;
                    int idStopIndex = responseString.indexOf("=") + 4;
                    String idString = responseString.substring(idStartIndex, idStopIndex);
                    // test that the ID is a valid number and set the instrument ID
                    if ((new Integer(idString)).intValue() > 0) {
                        setInstrumentID(idString);
                        buffer.clear();
                        logger.debug("Instrument ID is " + getInstrumentID() + ".");
                        break;

                    } else {
                        logger.debug("Instrument ID \"" + idString + "\" was not set.");
                    }

                } else {
                    break;
                }

                buffer.compact();
                if (getInstrumentID() != null) {
                    break;
                }
            }
        }

        // instrumentID is set

        // allow time for the instrument response
        streamingThread.sleep(5000);
        this.command = this.commandPrefix + getInstrumentID() + this.takeSampleCommand + this.commandSuffix;
        this.sentCommand = queryInstrument(command);

        // while there are bytes to read from the socket ...
        while (this.socketChannel.read(buffer) != -1 || buffer.position() > 0) {
            // prepare the buffer for reading
            buffer.flip();

            // while there are unread bytes in the ByteBuffer
            while (buffer.hasRemaining()) {
                byteOne = buffer.get();
                //logger.debug("b1: " + new String(Hex.encodeHex((new byte[]{byteOne})))   + "\t" + 
                //             "b2: " + new String(Hex.encodeHex((new byte[]{byteTwo})))   + "\t" + 
                //             "b3: " + new String(Hex.encodeHex((new byte[]{byteThree}))) + "\t" + 
                //             "b4: " + new String(Hex.encodeHex((new byte[]{byteFour})))  + "\t" +
                //             "sample pos: "   + sampleBuffer.position()                  + "\t" +
                //             "sample rem: "   + sampleBuffer.remaining()                 + "\t" +
                //             "sample cnt: "   + sampleByteCount                          + "\t" +
                //             "buffer pos: "   + buffer.position()                        + "\t" +
                //             "buffer rem: "   + buffer.remaining()                       + "\t" +
                //             "state: "        + state
                //);

                // Use a State Machine to process the byte stream.
                // Start building an rbnb frame for the entire sample, first by 
                // inserting a timestamp into the channelMap.  This time is merely
                // the time of insert into the data turbine, not the time of
                // observations of the measurements.  That time should be parsed out
                // of the sample in the Sink client code

                switch (state) {

                case 0:

                    // sample line is begun by S>
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x3E && byteTwo == 0x53) {
                        // we've found the beginning of a sample, move on
                        state = 1;
                        break;

                    } else {
                        break;
                    }

                case 1: // read the rest of the bytes to the next EOL characters

                    // sample line is terminated by S>
                    // note bytes are in reverse order in the FIFO window
                    if (byteOne == 0x3E && byteTwo == 0x53) {

                        sampleByteCount++; // add the last byte found to the count

                        // add the last byte found to the sample buffer
                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);

                        } else {
                            sampleBuffer.compact();
                            sampleBuffer.put(byteOne);

                        }

                        // extract just the length of the sample bytes (less 2 bytes
                        // to exclude the 'S>' prompt characters) out of the
                        // sample buffer, and place it in the channel map as a 
                        // byte array.  Then, send it to the data turbine.
                        byte[] sampleArray = new byte[sampleByteCount - 2];
                        sampleBuffer.flip();
                        sampleBuffer.get(sampleArray);

                        // send the sample to the data turbine
                        rbnbChannelMap.PutTimeAuto("server");
                        String sampleString = new String(sampleArray, "US-ASCII");
                        rbnbChannelMap.PutMime(channelIndex, "text/plain");
                        rbnbChannelMap.PutDataAsString(channelIndex, sampleString);
                        getSource().Flush(rbnbChannelMap);
                        logger.info("Sample: " + sampleString);
                        logger.info("flushed data to the DataTurbine. ");

                        byteOne = 0x00;
                        byteTwo = 0x00;
                        byteThree = 0x00;
                        byteFour = 0x00;
                        sampleBuffer.clear();
                        sampleByteCount = 0;
                        //rbnbChannelMap.Clear();                      
                        //logger.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap.");
                        //state = 0;

                        // Once the sample is flushed, take a new sample
                        if (getInstrumentID() != null) {
                            // allow time for the instrument response
                            streamingThread.sleep(2000);
                            this.command = this.commandPrefix + getInstrumentID() + this.takeSampleCommand
                                    + this.commandSuffix;
                            this.sentCommand = queryInstrument(command);
                        }

                    } else { // not 0x0A0D

                        // still in the middle of the sample, keep adding bytes
                        sampleByteCount++; // add each byte found

                        if (sampleBuffer.remaining() > 0) {
                            sampleBuffer.put(byteOne);
                        } else {
                            sampleBuffer.compact();
                            logger.debug("Compacting sampleBuffer ...");
                            sampleBuffer.put(byteOne);

                        }

                        break;
                    } // end if for 0x0A0D EOL

                } // end switch statement

                // shift the bytes in the FIFO window
                byteFour = byteThree;
                byteThree = byteTwo;
                byteTwo = byteOne;

            } //end while (more unread bytes)

            // prepare the buffer to read in more bytes from the stream
            buffer.compact();

        } // end while (more socket bytes to read)
        this.socketChannel.close();

    } 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;
    } catch (SAPIException sapie) {
        // In the event of an RBNB communication  exception, log the exception, 
        // and allow execute() to return false, which will prompt a retry.
        failed = true;
        sapie.printStackTrace();
        return !failed;
    } catch (java.lang.InterruptedException ie) {
        ie.printStackTrace();
    }

    return !failed;
}

From source file:libepg.epg.section.sectionreconstructor.SectionReconstructor.java

/**
 * ??????? ??????????????????/*from   w ww  .j  a v  a  2 s.co m*/
 *
 * @return ???
 *
 */
public synchronized Set<byte[]> getSectionByteArrays() {

    Set<byte[]> ret = new TreeSet<>((byte[] left, byte[] right) -> {
        for (int i = 0, j = 0; i < left.length && j < right.length; i++, j++) {
            int a = (left[i] & 0xff);
            int b = (right[j] & 0xff);
            if (a != b) {
                return a - b;
            }
        }
        return left.length - right.length;
    });

    boolean first_start_indicator_found = false;

    ByteBuffer buf = null;

    PayLoadSplitter splitter = new PayLoadSplitter();

    for (TsPacketParcel parcel : parcels) {

        splitter.setPacket(parcel.getPacket());
        Map<PayLoadSplitter.PAYLOAD_PART_KEY, byte[]> t_map = splitter.getSplittedPayLoad();

        if ((buf == null)
                || (parcel.isMissingJustBefore() == TsPacketParcel.MISSING_PACKET_FLAG.MISSING_JUST_BEFORE)) {
            if (LOG.isTraceEnabled()) {
                LOG.trace(
                        "?????????????????????????");
            }
            if (buf == null) {
                buf = ByteBuffer.allocate(TABLE_ID.MAX_SECTION_LENGTH.BYTE_4093.getMaxSectionLength());
            } else {
                buf.clear();
            }
        }
        if (LOG.isTraceEnabled()) {
            LOG.trace("?=" + Hex.encodeHexString(buf.array()));
        }
        if ((first_start_indicator_found == false) && (parcel.getPacket()
                .getPayload_unit_start_indicator() == TsPacket.PAYLOAD_UNIT_START_INDICATOR.START_PES_OR_START_SECTION)) {
            if (LOG.isTraceEnabled()) {
                LOG.trace("??");
            }
            first_start_indicator_found = true;
        }

        if (first_start_indicator_found == false) {
            if (LOG.isTraceEnabled()) {
                LOG.trace(
                        "?????????");
            }
        } else if (parcel.getPacket()
                .getPayload_unit_start_indicator() == TsPacket.PAYLOAD_UNIT_START_INDICATOR.START_PES_OR_START_SECTION) {
            if (LOG.isTraceEnabled()) {
                LOG.trace(
                        "??????????");
            }
            if (buf.position() == 0) {
                if (LOG.isTraceEnabled()) {
                    LOG.trace(
                            "??0=?????????->??2?????????????+1?????");
                }
                byte[] temp_array = null;
                if (t_map.size() == 1) {
                    temp_array = t_map.get(PayLoadSplitter.PAYLOAD_PART_KEY.PAYLOAD_AFTER_2_BYTE);
                } else {
                    temp_array = t_map.get(PayLoadSplitter.PAYLOAD_PART_KEY.NEXT_POINTER);
                }
                buf.put(temp_array);
            } else {
                if (LOG.isTraceEnabled()) {
                    LOG.trace(
                            "??0????=???????->??2???????????\n???????????????+1?????");
                }
                byte[] temp_array = null;
                if (t_map.containsKey(PayLoadSplitter.PAYLOAD_PART_KEY.PAYLOAD_AFTER_2_BYTE)) {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace(
                                "???????????????");
                    }

                    this.addToReturnObject(buf, ret);

                    buf.clear();

                    temp_array = t_map.get(PayLoadSplitter.PAYLOAD_PART_KEY.PAYLOAD_AFTER_2_BYTE);
                    buf.put(temp_array);
                } else {
                    if (LOG.isTraceEnabled()) {
                        LOG.trace(
                                "??????????????????");
                    }
                    temp_array = t_map.get(PayLoadSplitter.PAYLOAD_PART_KEY.PREV_POINTER);
                    buf.put(temp_array);

                    this.addToReturnObject(buf, ret);

                    buf.clear();

                    temp_array = t_map.get(PayLoadSplitter.PAYLOAD_PART_KEY.NEXT_POINTER);
                    buf.put(temp_array);
                }
            }
        } else {
            if (LOG.isTraceEnabled()) {
                LOG.trace(
                        "?????->????->??????");
            }
            buf.put(t_map.get(PayLoadSplitter.PAYLOAD_PART_KEY.ALL_PAYLOAD));
        }
    }

    return Collections.unmodifiableSet(ret);
}

From source file:com.healthmarketscience.jackcess.impl.TableImpl.java

/**
 * Reads the column data from the given row buffer.  Leaves limit unchanged.
 * Caches the returned value in the rowState.
 *///from   w  ww . j  a  v  a2s . c om
private static Object getRowColumn(JetFormat format, ByteBuffer rowBuffer, ColumnImpl column, RowState rowState,
        Map<ColumnImpl, byte[]> rawVarValues) throws IOException {
    byte[] columnData = null;
    try {

        NullMask nullMask = rowState.getNullMask(rowBuffer);
        boolean isNull = nullMask.isNull(column);
        if (column.storeInNullMask()) {
            // Boolean values are stored in the null mask.  see note about
            // caching below
            return rowState.setRowCacheValue(column.getColumnIndex(), column.readFromNullMask(isNull));
        } else if (isNull) {
            // well, that's easy! (no need to update cache w/ null)
            return null;
        }

        Object cachedValue = rowState.getRowCacheValue(column.getColumnIndex());
        if (cachedValue != null) {
            // we already have it, use it
            return cachedValue;
        }

        // reset position to row start
        rowBuffer.reset();

        // locate the column data bytes
        int rowStart = rowBuffer.position();
        int colDataPos = 0;
        int colDataLen = 0;
        if (!column.isVariableLength()) {

            // read fixed length value (non-boolean at this point)
            int dataStart = rowStart + format.OFFSET_COLUMN_FIXED_DATA_ROW_OFFSET;
            colDataPos = dataStart + column.getFixedDataOffset();
            colDataLen = column.getType().getFixedSize(column.getLength());

        } else {
            int varDataStart;
            int varDataEnd;

            if (format.SIZE_ROW_VAR_COL_OFFSET == 2) {

                // read simple var length value
                int varColumnOffsetPos = (rowBuffer.limit() - nullMask.byteSize() - 4)
                        - (column.getVarLenTableIndex() * 2);

                varDataStart = rowBuffer.getShort(varColumnOffsetPos);
                varDataEnd = rowBuffer.getShort(varColumnOffsetPos - 2);

            } else {

                // read jump-table based var length values
                short[] varColumnOffsets = readJumpTableVarColOffsets(rowState, rowBuffer, rowStart, nullMask);

                varDataStart = varColumnOffsets[column.getVarLenTableIndex()];
                varDataEnd = varColumnOffsets[column.getVarLenTableIndex() + 1];
            }

            colDataPos = rowStart + varDataStart;
            colDataLen = varDataEnd - varDataStart;
        }

        // grab the column data
        rowBuffer.position(colDataPos);
        columnData = ByteUtil.getBytes(rowBuffer, colDataLen);

        if ((rawVarValues != null) && column.isVariableLength()) {
            // caller wants raw value as well
            rawVarValues.put(column, columnData);
        }

        // parse the column data.  we cache the row values in order to be able
        // to update the index on row deletion.  note, most of the returned
        // values are immutable, except for binary data (returned as byte[]),
        // but binary data shouldn't be indexed anyway.
        return rowState.setRowCacheValue(column.getColumnIndex(), column.read(columnData));

    } catch (Exception e) {

        // cache "raw" row value.  see note about caching above
        rowState.setRowCacheValue(column.getColumnIndex(), ColumnImpl.rawDataWrapper(columnData));

        return rowState.handleRowError(column, columnData, e);
    }
}

From source file:com.healthmarketscience.jackcess.Table.java

/**
 * Serialize a row of Objects into a byte buffer.
 * //from www . j av  a2s . c  om
 * @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:com.aionemu.gameserver.services.LegionService.java

/**
 * @param player//  w  w w.  j a  v  a 2 s  . c  om
 * @param legionEmblem
 * @param legionId
 * @param legionName
 */
public void sendEmblemData(Player player, LegionEmblem legionEmblem, int legionId, String legionName) {
    PacketSendUtility.sendPacket(player,
            new SM_LEGION_SEND_EMBLEM(legionId, legionEmblem.getEmblemId(), legionEmblem.getColor_r(),
                    legionEmblem.getColor_g(), legionEmblem.getColor_b(), legionName,
                    legionEmblem.getEmblemType(), legionEmblem.getCustomEmblemData().length));
    ByteBuffer buf = ByteBuffer.allocate(legionEmblem.getCustomEmblemData().length);
    buf.put(legionEmblem.getCustomEmblemData()).position(0);
    log.debug("legionEmblem size: " + buf.capacity() + " bytes");
    int maxSize = 7993;
    int currentSize;
    byte[] bytes;
    do {
        log.debug("legionEmblem data position: " + buf.position());
        currentSize = buf.capacity() - buf.position();
        log.debug("legionEmblem data remaining capacity: " + currentSize + " bytes");

        if (currentSize >= maxSize) {
            bytes = new byte[maxSize];
            for (int i = 0; i < maxSize; i++) {
                bytes[i] = buf.get();
            }
            log.debug("legionEmblem data send size: " + (bytes.length) + " bytes");
            PacketSendUtility.sendPacket(player, new SM_LEGION_SEND_EMBLEM_DATA(maxSize, bytes));
        } else {
            bytes = new byte[currentSize];
            for (int i = 0; i < currentSize; i++) {
                bytes[i] = buf.get();
            }
            log.debug("legionEmblem data send size: " + (bytes.length) + " bytes");
            PacketSendUtility.sendPacket(player, new SM_LEGION_SEND_EMBLEM_DATA(currentSize, bytes));
        }
    } while (buf.capacity() != buf.position());
}

From source file:com.healthmarketscience.jackcess.Table.java

/**
 * @param buffer Buffer to write to// w w w  . j  a v a  2 s  .  c  om
 * @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.IndexData.java

/**
 * Returns an entry buffer containing the relevant data for an entry given
 * the valuePrefix.//  ww w.  j  a  va2s  . c  o m
 */
private ByteBuffer getTempEntryBuffer(ByteBuffer indexPage, int entryLen, byte[] valuePrefix,
        TempBufferHolder tmpEntryBufferH) {
    ByteBuffer tmpEntryBuffer = tmpEntryBufferH.getBuffer(getPageChannel(), valuePrefix.length + entryLen);

    // combine valuePrefix and rest of entry from indexPage, then prep for
    // reading
    tmpEntryBuffer.put(valuePrefix);
    tmpEntryBuffer.put(indexPage.array(), indexPage.position(), entryLen);
    tmpEntryBuffer.flip();

    return tmpEntryBuffer;
}

From source file:org.alfresco.contentstore.patch.PatchServiceImpl.java

private void updatePatchDocument(PatchDocument patchDocument, NodeChecksums checksums, Reader reader)
        throws IOException {
    ByteBuffer data = ByteBuffer.allocate(blockSize * 20);

    int blockSize = checksums.getBlockSize();

    int i = 0;/*from   w w w.ja  v  a2s  . c o  m*/

    Adler32 adlerInfo = new Adler32(hasher);
    int lastMatchIndex = 1; // starts at 1
    ByteBuffer currentPatch = ByteBuffer.allocate(5000000); // TODO

    int x = 0;

    for (;;) {
        if (x == 0 || i >= data.limit()) {
            data.clear();
            i = 0;
            int numRead = reader.read(data);
            if (numRead <= 0) {
                break;
            }
            data.flip();
            x += numRead;
        }

        int chunkSize = 0;
        // determine the size of the next data chuck to evaluate. Default to
        // blockSize, but clamp to end of data
        if ((i + blockSize) > data.limit()) {
            chunkSize = data.limit() - i;
            adlerInfo.reset(); // need to reset this because the rolling
                               // checksum doesn't work correctly on a final
                               // non-aligned block
        } else {
            chunkSize = blockSize;
        }

        int end = i + chunkSize - 1;

        int matchedBlockIndex = adlerInfo.checkMatch(lastMatchIndex, checksums, data, i, end);
        if (matchedBlockIndex != -1) {
            //                try
            //                {
            //                    String y = hasher.md5(data, i, end);
            //                    System.out.println("y = " + y + ", x = " + x + ", i = " + i + ", end = " + end);
            //                }
            //                catch (NoSuchAlgorithmException e)
            //                {
            //                    // TODO Auto-generated catch block
            //                    e.printStackTrace();
            //                }

            // if we have a match, do the following:
            // 1) add the matched block index to our tracking buffer
            // 2) check to see if there's a current patch. If so, add it to
            // the patch document.
            // 3) jump forward blockSize bytes and continue
            patchDocument.addMatchedBlock(matchedBlockIndex);

            if (currentPatch.position() > 0) {
                // there are outstanding patches, add them to the list
                // create the patch and append it to the patches buffer
                currentPatch.flip();
                int size = currentPatch.limit();
                byte[] dst = new byte[size];
                currentPatch.get(dst, 0, size);
                Patch patch = new Patch(lastMatchIndex, size, dst);
                patchDocument.addPatch(patch);
                currentPatch.clear();
            }

            lastMatchIndex = matchedBlockIndex;

            i += chunkSize;

            adlerInfo.reset();
        } else {
            // while we don't have a block match, append bytes to the
            // current patch
            if (currentPatch.position() >= currentPatch.limit()) {
                //                    System.out.println("count=" + (x + i));
                //                    System.out.println("count1=" + currentPatch.position() + ", " + currentPatch.limit());
                //                    System.out.println(matchedBlockIndexes);
                //                    System.out.println(patches);
            }
            currentPatch.put(data.get(i));
            i++;
        }
    } // end for each byte in the data

    if (currentPatch.position() > 0) {
        currentPatch.flip();
        int size = currentPatch.limit();
        byte[] dst = new byte[size];
        currentPatch.get(dst, 0, size);
        Patch patch = new Patch(lastMatchIndex, size, dst);
        patchDocument.addPatch(patch);
    }
}

From source file:net.yacy.cora.document.id.MultiProtocolURL.java

/**
 * Decode UTF-8 percent-encoded characters eventually found in the given path.
 * <ul>/*from  ww w  . j ava2 s.  c  o m*/
 * Differences with {@link URLDecoder#decode(String, String)} :
 * <li>the '+' character is not decoded to space character</li>
 * <li>no exception is thrown when invalid hexadecimal digits are found after a '%' character</li>
 * </ul>
 * 
 * @param path an URL path eventually escaped
 * @return return the unescaped path or null when path is null.
 */
public static final String unescapePath(final String escaped) {
    if (escaped == null) {
        return escaped;
    }
    boolean modified = false;
    final int len = escaped.length();
    final StringBuilder unescaped = new StringBuilder(len > 500 ? len / 2 : len);
    ByteBuffer utf8Bytes = null;
    int i = 0;
    while (i < len) {
        final char ch = escaped.charAt(i);
        if (ch == '%' && (i + 2) < len) {
            final char digit1 = escaped.charAt(i + 1);
            final char digit2 = escaped.charAt(i + 2);
            if (isHexDigit(digit1) && isHexDigit(digit2)) {
                if (utf8Bytes == null) {
                    utf8Bytes = ByteBuffer.allocate((len - i) / 3);
                }
                /* Percent-encoded character UTF-8 byte */
                int hexaValue = Integer.parseInt(escaped.substring(i + 1, i + 3), 16);
                utf8Bytes.put((byte) hexaValue);
                modified = true;
                i += 2;
            } else {
                /* Not a valid percent-encoded character : we append it as is */
                unescaped.append(ch);
            }
        } else {
            if (utf8Bytes != null && utf8Bytes.position() > 0) {
                unescaped
                        .append(new String(utf8Bytes.array(), 0, utf8Bytes.position(), StandardCharsets.UTF_8));
                utf8Bytes.position(0);
            }
            unescaped.append(ch);
        }
        i++;
    }
    if (utf8Bytes != null && utf8Bytes.position() > 0) {
        unescaped.append(new String(utf8Bytes.array(), 0, utf8Bytes.position(), StandardCharsets.UTF_8));
    }

    return modified ? unescaped.toString() : escaped;
}

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  . c  om
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()));
}