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 Buffer position(int newPosition) 

Source Link

Document

Sets the position of this buffer.

Usage

From source file:com.github.ambry.utils.UtilsTest.java

@Test
public void testReadFileToByteBuffer() throws IOException {
    File file = File.createTempFile("test", "1");
    file.deleteOnExit();/*from  w  ww  .jav a 2 s.  co m*/
    FileChannel fileChannel = Utils.openChannel(file, false);
    byte[] referenceBytes = new byte[20];
    new Random().nextBytes(referenceBytes);
    FileUtils.writeByteArrayToFile(file, referenceBytes);

    // fill up fresh byteBuffer
    ByteBuffer buffer = ByteBuffer.allocate(20);
    Utils.readFileToByteBuffer(fileChannel, 0, buffer);
    assertArrayEquals("Data mismatch", referenceBytes, buffer.array());

    // write to byteBuffer based on buffer remaining
    buffer.limit(10);
    buffer.position(0);
    assertEquals("buffer remaining should be 10", 10, buffer.remaining());
    Utils.readFileToByteBuffer(fileChannel, 10, buffer);
    assertEquals("buffer remaining should be 0", 0, buffer.remaining());
    for (int i = 0; i < 10; i++) {
        assertEquals("First 10 bytes in buffer should match last 10 bytes in file", buffer.array()[i],
                referenceBytes[i + 10]);
    }

    // byteBuffer.remaining() + starting offset > file size, exception is expected.
    buffer.clear();
    assertEquals("buffer remaining should be 20", 20, buffer.remaining());
    try {
        Utils.readFileToByteBuffer(fileChannel, 1, buffer);
        fail("Should fail");
    } catch (IOException e) {
    }

    // starting offset exceeds file size, exception is expected.
    buffer.clear();
    assertEquals("buffer remaining should be 20", 20, buffer.remaining());
    try {
        Utils.readFileToByteBuffer(fileChannel, 21, buffer);
        fail("Should fail");
    } catch (IOException e) {
    }
}

From source file:net.pms.util.AudioUtils.java

/**
 * Parses the old RealAudio 1.0 and 2.0 formats that's not supported by
 * neither {@link org.jaudiotagger} nor MediaInfo. Returns {@code false} if
 * {@code channel} isn't one of these formats or the parsing fails.
 * <p>//from  w  w  w. ja  va2 s . c o m
 * Primary references:
 * <ul>
 * <li><a href="https://wiki.multimedia.cx/index.php/RealMedia">RealAudio on
 * MultimediaWiki</a></li>
 * <li><a
 * href="https://github.com/FFmpeg/FFmpeg/blob/master/libavformat/rmdec.c"
 * >FFmpeg rmdec.c</a></li>
 * </ul>
 *
 * @param channel the {@link Channel} containing the input. Size will only
 *            be parsed if {@code channel} is a {@link FileChannel}
 *            instance.
 * @param media the {@link DLNAMediaInfo} instance to write the parsing
 *            results to.
 * @return {@code true} if the {@code channel} input is in RealAudio 1.0 or
 *         2.0 format and the parsing succeeds; false otherwise
 */
public static boolean parseRealAudio(ReadableByteChannel channel, DLNAMediaInfo media) {
    final byte[] magicBytes = { 0x2E, 0x72, 0x61, (byte) 0xFD };
    ByteBuffer buffer = ByteBuffer.allocate(8);
    buffer.order(ByteOrder.BIG_ENDIAN);
    DLNAMediaAudio audio = new DLNAMediaAudio();
    try {
        int count = channel.read(buffer);
        if (count < 4) {
            LOGGER.trace("Input is too short to be RealAudio");
            return false;
        }
        buffer.flip();
        byte[] signature = new byte[4];
        buffer.get(signature);
        if (!Arrays.equals(magicBytes, signature)) {
            if (LOGGER.isTraceEnabled()) {
                LOGGER.trace("Input signature ({}) mismatches RealAudio version 1.0 or 2.0",
                        new String(signature, StandardCharsets.US_ASCII));
            }
            return false;
        }
        media.setContainer(FormatConfiguration.RA);
        short version = buffer.getShort();
        int reportedHeaderSize = 0;
        int reportedDataSize = 0;
        if (version == 3) {
            audio.setCodecA(FormatConfiguration.REALAUDIO_14_4);
            audio.getAudioProperties().setNumberOfChannels(1);
            audio.getAudioProperties().setSampleFrequency(8000);
            short headerSize = buffer.getShort();

            buffer = ByteBuffer.allocate(headerSize);
            channel.read(buffer);
            buffer.flip();
            buffer.position(8);
            int bytesPerMinute = buffer.getShort() & 0xFFFF;
            reportedDataSize = buffer.getInt();
            byte b = buffer.get();
            if (b != 0) {
                byte[] title = new byte[b & 0xFF];
                buffer.get(title);
                String titleString = new String(title, StandardCharsets.US_ASCII);
                audio.setSongname(titleString);
                audio.setAudioTrackTitleFromMetadata(titleString);
            }
            b = buffer.get();
            if (b != 0) {
                byte[] artist = new byte[b & 0xFF];
                buffer.get(artist);
                audio.setArtist(new String(artist, StandardCharsets.US_ASCII));
            }
            audio.setBitRate(bytesPerMinute * 8 / 60);
            media.setBitrate(bytesPerMinute * 8 / 60);
        } else if (version == 4 || version == 5) {
            buffer = ByteBuffer.allocate(14);
            channel.read(buffer);
            buffer.flip();
            buffer.get(signature);
            if (!".ra4".equals(new String(signature, StandardCharsets.US_ASCII))) {
                LOGGER.debug("Invalid RealAudio 2.0 signature \"{}\"",
                        new String(signature, StandardCharsets.US_ASCII));
                return false;
            }
            reportedDataSize = buffer.getInt();
            buffer.getShort(); //skip version repeated
            reportedHeaderSize = buffer.getInt();

            buffer = ByteBuffer.allocate(reportedHeaderSize);
            channel.read(buffer);
            buffer.flip();
            buffer.getShort(); // skip codec flavor
            buffer.getInt(); // skip coded frame size
            buffer.getInt(); // skip unknown
            long bytesPerMinute = buffer.getInt() & 0xFFFFFFFFL;
            buffer.getInt(); // skip unknown
            buffer.getShort(); // skip sub packet
            buffer.getShort(); // skip frame size
            buffer.getShort(); // skip sub packet size
            buffer.getShort(); // skip unknown
            if (version == 5) {
                buffer.position(buffer.position() + 6); // skip unknown
            }
            short sampleRate = buffer.getShort();
            buffer.getShort(); // skip unknown
            short sampleSize = buffer.getShort();
            short nrChannels = buffer.getShort();
            byte[] fourCC;
            if (version == 4) {
                buffer.position(buffer.get() + buffer.position()); // skip interleaver id
                fourCC = new byte[buffer.get()];
                buffer.get(fourCC);
            } else {
                buffer.getFloat(); // skip deinterlace id
                fourCC = new byte[4];
                buffer.get(fourCC);
            }
            String fourCCString = new String(fourCC, StandardCharsets.US_ASCII).toLowerCase(Locale.ROOT);
            switch (fourCCString) {
            case "lpcJ":
                audio.setCodecA(FormatConfiguration.REALAUDIO_14_4);
                break;
            case "28_8":
                audio.setCodecA(FormatConfiguration.REALAUDIO_28_8);
                break;
            case "dnet":
                audio.setCodecA(FormatConfiguration.AC3);
                break;
            case "sipr":
                audio.setCodecA(FormatConfiguration.SIPRO);
                break;
            case "cook":
                audio.setCodecA(FormatConfiguration.COOK);
            case "atrc":
                audio.setCodecA(FormatConfiguration.ATRAC);
            case "ralf":
                audio.setCodecA(FormatConfiguration.RALF);
            case "raac":
                audio.setCodecA(FormatConfiguration.AAC_LC);
            case "racp":
                audio.setCodecA(FormatConfiguration.HE_AAC);
            default:
                LOGGER.debug("Unknown RealMedia codec FourCC \"{}\" - parsing failed", fourCCString);
                return false;
            }

            if (buffer.hasRemaining()) {
                parseRealAudioMetaData(buffer, audio, version);
            }

            audio.setBitRate((int) (bytesPerMinute * 8 / 60));
            media.setBitrate((int) (bytesPerMinute * 8 / 60));
            audio.setBitsperSample(sampleSize);
            audio.getAudioProperties().setNumberOfChannels(nrChannels);
            audio.getAudioProperties().setSampleFrequency(sampleRate);
        } else {
            LOGGER.error("Could not parse RealAudio format - unknown format version {}", version);
            return false;
        }

        media.getAudioTracksList().add(audio);
        long fileSize = 0;
        if (channel instanceof FileChannel) {
            fileSize = ((FileChannel) channel).size();
            media.setSize(fileSize);
        }
        // Duration is estimated based on bitrate and might not be accurate
        if (audio.getBitRate() > 0) {
            int dataSize;
            if (fileSize > 0 && reportedHeaderSize > 0) {
                int fullHeaderSize = reportedHeaderSize + (version == 3 ? 8 : 16);
                if (reportedDataSize > 0) {
                    dataSize = (int) Math.min(reportedDataSize, fileSize - fullHeaderSize);
                } else {
                    dataSize = (int) (fileSize - fullHeaderSize);
                }
            } else {
                dataSize = reportedDataSize;
            }
            media.setDuration((double) dataSize / audio.getBitRate() * 8);
        }

    } catch (IOException e) {
        LOGGER.debug("Error while trying to parse RealAudio version 1 or 2: {}", e.getMessage());
        LOGGER.trace("", e);
        return false;
    }
    if (PMS.getConfiguration() != null
            && !PMS.getConfiguration().getAudioThumbnailMethod().equals(CoverSupplier.NONE)
            && (StringUtils.isNotBlank(media.getFirstAudioTrack().getSongname())
                    || StringUtils.isNotBlank(media.getFirstAudioTrack().getArtist()))) {
        ID3v1Tag tag = new ID3v1Tag();
        if (StringUtils.isNotBlank(media.getFirstAudioTrack().getSongname())) {
            tag.setTitle(media.getFirstAudioTrack().getSongname());
        }
        if (StringUtils.isNotBlank(media.getFirstAudioTrack().getArtist())) {
            tag.setArtist(media.getFirstAudioTrack().getArtist());
        }
        try {
            media.setThumb(DLNAThumbnail.toThumbnail(CoverUtil.get().getThumbnail(tag), 640, 480, ScaleType.MAX,
                    ImageFormat.SOURCE, false));
        } catch (IOException e) {
            LOGGER.error("An error occurred while generating thumbnail for RealAudio source: [\"{}\", \"{}\"]",
                    tag.getFirstTitle(), tag.getFirstArtist());
        }
    }
    media.setThumbready(true);
    media.setMediaparsed(true);

    return true;
}

From source file:com.linkedin.databus.core.DbusEventV2.java

private DbusEventPart getDbusEventPart(int dbusEventPartStartPos) {
    ByteBuffer dbusEventPartBB;
    dbusEventPartBB = _buf.asReadOnlyBuffer().order(_buf.order());
    dbusEventPartBB.position(dbusEventPartStartPos);
    DbusEventPart dbusEventPart = DbusEventPart.decode(dbusEventPartBB);
    return dbusEventPart;
}

From source file:io.warp10.continuum.gts.GTSDecoder.java

/**
 * Return an encoder with all data from the last value retrieved (post call to next())
 * onwards/*from  ww  w.j  ava  2 s . c  o m*/
 * 
 * @param safeMetadata Is it safe to reuse the Metadata?
 */
public GTSEncoder getEncoder(boolean safeMetadata) throws IOException {

    if (!nextCalled) {
        throw new IOException(
                "Can only get an encoder for a decoder on which 'next' was called at least once.");
    }

    //
    // Copy the remaining data into a new ByteBuffer
    //

    ByteBuffer bb = this.buffer.duplicate();
    bb.position(this.position);

    int offset = 0;
    int len = bb.remaining();
    byte[] bytes = null;

    if (bb.hasArray()) {
        bytes = bb.array();
        offset = bb.arrayOffset() + bb.position();
    } else {
        bytes = new byte[bb.remaining()];
        bb.get(bytes);
    }

    //
    // Create an encoder with the same base timestamp and wrapping key, providing a sizing hint
    //

    GTSEncoder encoder = new GTSEncoder(this.baseTimestamp, this.wrappingKey, bb.remaining());

    if (safeMetadata) {
        encoder.safeSetMetadata(this.getMetadata());
    } else {
        encoder.setMetadata(this.getMetadata());
    }

    //
    // Set initial values
    //

    encoder.initialize(this.previousLastTimestamp, this.previousLastGeoXPPoint, this.previousLastElevation,
            this.previousLastLongValue, this.previousLastDoubleValue, this.previousLastBDValue,
            this.previousLastStringValue);

    //
    // Copy the encoded data
    //

    encoder.stream.write(bytes, offset, len);

    //
    // Put the encoder into 'safe delta' mode, because we don't know what the last
    // value/ts/elevation/location were, we can't use delta encoding for now
    //

    encoder.safeDelta();

    encoder.setCount(this.count);

    return encoder;
}

From source file:com.ery.ertc.estorm.util.Bytes.java

/**
 * Returns a new byte array, copied from the given {@code buf}, from the
 * index 0 (inclusive) to the limit (exclusive), regardless of the current
 * position. The position and the other index parameters are not changed.
 * /*from   w w  w. j a v a  2s.com*/
 * @param buf
 *            a byte buffer
 * @return the byte array
 * @see #getBytes(ByteBuffer)
 */
public static byte[] toBytes(ByteBuffer buf) {
    ByteBuffer dup = buf.duplicate();
    dup.position(0);
    return readBytes(dup);
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.por.PORFileReaderSpi.java

@Override
public boolean canDecodeInput(BufferedInputStream stream) throws IOException {
    if (stream == null) {
        throw new IllegalArgumentException("file == null!");
    }/*from w  w w. j ava  2s. co  m*/

    dbgLog.fine("applying the por test\n");

    byte[] b = new byte[POR_HEADER_SIZE];

    if (stream.markSupported()) {
        stream.mark(0);
    }

    int nbytes = stream.read(b, 0, POR_HEADER_SIZE);

    //printHexDump(b, "hex dump of the byte-array");

    if (nbytes == 0) {
        throw new IOException();
    } else if (nbytes < 491) {
        // size test
        dbgLog.fine("this file is NOT spss-por type");
        return false;
    }

    if (stream.markSupported()) {
        stream.reset();
    }

    boolean DEBUG = false;

    //windows [0D0A]=>   [1310] = [CR/LF]
    //unix    [0A]  =>   [10]
    //mac     [0D]  =>   [13]
    // 3char  [0D0D0A]=> [131310] spss for windows rel 15
    // expected results
    // unix    case: [0A]   : [80], [161], [242], [323], [404], [485]
    // windows case: [0D0A] : [81], [163], [245], [327], [409], [491]
    //  : [0D0D0A] : [82], [165], [248], [331], [414], [495]

    // convert b into a ByteBuffer

    ByteBuffer buff = ByteBuffer.wrap(b);
    byte[] nlch = new byte[36];
    int pos1;
    int pos2;
    int pos3;
    int ucase = 0;
    int wcase = 0;
    int mcase = 0;
    int three = 0;
    int nolines = 6;
    int nocols = 80;
    for (int i = 0; i < nolines; ++i) {
        int baseBias = nocols * (i + 1);
        // 1-char case
        pos1 = baseBias + i;
        buff.position(pos1);
        dbgLog.finer("\tposition(1)=" + buff.position());
        int j = 6 * i;
        nlch[j] = buff.get();

        if (nlch[j] == 10) {
            ucase++;
        } else if (nlch[j] == 13) {
            mcase++;
        }

        // 2-char case
        pos2 = baseBias + 2 * i;
        buff.position(pos2);
        dbgLog.finer("\tposition(2)=" + buff.position());

        nlch[j + 1] = buff.get();
        nlch[j + 2] = buff.get();

        // 3-char case
        pos3 = baseBias + 3 * i;
        buff.position(pos3);
        dbgLog.finer("\tposition(3)=" + buff.position());

        nlch[j + 3] = buff.get();
        nlch[j + 4] = buff.get();
        nlch[j + 5] = buff.get();

        dbgLog.finer(i + "-th iteration position =" + nlch[j] + "\t" + nlch[j + 1] + "\t" + nlch[j + 2]);
        dbgLog.finer(i + "-th iteration position =" + nlch[j + 3] + "\t" + nlch[j + 4] + "\t" + nlch[j + 5]);

        if ((nlch[j + 3] == 13) && (nlch[j + 4] == 13) && (nlch[j + 5] == 10)) {
            three++;
        } else if ((nlch[j + 1] == 13) && (nlch[j + 2] == 10)) {
            wcase++;
        }

        buff.rewind();
    }
    if (three == nolines) {
        dbgLog.fine("0D0D0A case");
        windowsNewLine = false;
    } else if ((ucase == nolines) && (wcase < nolines)) {
        dbgLog.fine("0A case");
        windowsNewLine = false;
    } else if ((ucase < nolines) && (wcase == nolines)) {
        dbgLog.fine("0D0A case");
    } else if ((mcase == nolines) && (wcase < nolines)) {
        dbgLog.fine("0D case");
        windowsNewLine = false;
    }

    buff.rewind();
    int PORmarkPosition = POR_MARK_POSITION_DEFAULT;
    if (windowsNewLine) {
        PORmarkPosition = PORmarkPosition + 5;
    } else if (three == nolines) {
        PORmarkPosition = PORmarkPosition + 10;
    }

    byte[] pormark = new byte[8];
    buff.position(PORmarkPosition);
    buff.get(pormark, 0, 8);
    String pormarks = new String(pormark);

    //dbgLog.fine("pormark =>" + pormarks + "<-");
    dbgLog.fine(
            "pormark[hex: 53 50 53 53 50 4F 52 54 == SPSSPORT] =>" + new String(Hex.encodeHex(pormark)) + "<-");

    if (pormarks.equals(POR_MARK)) {
        dbgLog.fine("this file is spss-por type");
        return true;
    } else {
        dbgLog.fine("this file is NOT spss-por type");
    }
    return false;
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.por.PORFileReaderSpi.java

@Override
public boolean canDecodeInput(Object source) throws IOException {
    if (!(source instanceof BufferedInputStream)) {
        return false;
    }//from  w  ww.  j  a  v a 2  s . c om
    if (source == null) {
        throw new IllegalArgumentException("source == null!");
    }
    BufferedInputStream stream = (BufferedInputStream) source;
    dbgLog.fine("applying the por test\n");

    byte[] b = new byte[POR_HEADER_SIZE];

    if (stream.markSupported()) {
        stream.mark(0);
    }

    int nbytes = stream.read(b, 0, POR_HEADER_SIZE);

    //printHexDump(b, "hex dump of the byte-array");

    if (nbytes == 0) {
        throw new IOException();
    } else if (nbytes < 491) {
        // size test
        dbgLog.fine("this file is NOT spss-por type");
        return false;
    }

    if (stream.markSupported()) {
        stream.reset();
    }

    boolean DEBUG = false;

    //windows [0D0A]=>   [1310] = [CR/LF]
    //unix    [0A]  =>   [10]
    //mac     [0D]  =>   [13]
    // 3char  [0D0D0A]=> [131310] spss for windows rel 15
    // expected results
    // unix    case: [0A]   : [80], [161], [242], [323], [404], [485]
    // windows case: [0D0A] : [81], [163], [245], [327], [409], [491]
    //  : [0D0D0A] : [82], [165], [248], [331], [414], [495]

    // convert b into a ByteBuffer

    ByteBuffer buff = ByteBuffer.wrap(b);
    byte[] nlch = new byte[36];
    int pos1;
    int pos2;
    int pos3;
    int ucase = 0;
    int wcase = 0;
    int mcase = 0;
    int three = 0;
    int nolines = 6;
    int nocols = 80;
    for (int i = 0; i < nolines; ++i) {
        int baseBias = nocols * (i + 1);
        // 1-char case
        pos1 = baseBias + i;
        buff.position(pos1);
        dbgLog.finer("\tposition(1)=" + buff.position());
        int j = 6 * i;
        nlch[j] = buff.get();

        if (nlch[j] == 10) {
            ucase++;
        } else if (nlch[j] == 13) {
            mcase++;
        }

        // 2-char case
        pos2 = baseBias + 2 * i;
        buff.position(pos2);
        dbgLog.finer("\tposition(2)=" + buff.position());

        nlch[j + 1] = buff.get();
        nlch[j + 2] = buff.get();

        // 3-char case
        pos3 = baseBias + 3 * i;
        buff.position(pos3);
        dbgLog.finer("\tposition(3)=" + buff.position());

        nlch[j + 3] = buff.get();
        nlch[j + 4] = buff.get();
        nlch[j + 5] = buff.get();

        dbgLog.finer(i + "-th iteration position =" + nlch[j] + "\t" + nlch[j + 1] + "\t" + nlch[j + 2]);
        dbgLog.finer(i + "-th iteration position =" + nlch[j + 3] + "\t" + nlch[j + 4] + "\t" + nlch[j + 5]);

        if ((nlch[j + 3] == 13) && (nlch[j + 4] == 13) && (nlch[j + 5] == 10)) {
            three++;
        } else if ((nlch[j + 1] == 13) && (nlch[j + 2] == 10)) {
            wcase++;
        }

        buff.rewind();
    }
    if (three == nolines) {
        dbgLog.fine("0D0D0A case");
        windowsNewLine = false;
    } else if ((ucase == nolines) && (wcase < nolines)) {
        dbgLog.fine("0A case");
        windowsNewLine = false;
    } else if ((ucase < nolines) && (wcase == nolines)) {
        dbgLog.fine("0D0A case");
    } else if ((mcase == nolines) && (wcase < nolines)) {
        dbgLog.fine("0D case");
        windowsNewLine = false;
    }

    buff.rewind();
    int PORmarkPosition = POR_MARK_POSITION_DEFAULT;
    if (windowsNewLine) {
        PORmarkPosition = PORmarkPosition + 5;
    } else if (three == nolines) {
        PORmarkPosition = PORmarkPosition + 10;
    }

    byte[] pormark = new byte[8];
    buff.position(PORmarkPosition);
    buff.get(pormark, 0, 8);
    String pormarks = new String(pormark);

    dbgLog.fine(
            "pormark[hex: 53 50 53 53 50 4F 52 54 == SPSSPORT] =>" + new String(Hex.encodeHex(pormark)) + "<-");

    if (pormarks.equals(POR_MARK)) {
        dbgLog.fine("this file is spss-por type");
        return true;
    } else {
        dbgLog.fine("this file is NOT spss-por type");
    }
    return false;
}

From source file:au.org.ala.delta.intkey.model.IntkeyDatasetFileReader.java

/**
 * Read taxon data/*from  w ww.  j  a va2  s  .  c  o m*/
 * 
 * @param itemFileHeader
 *            Items (taxa) file header data
 * @param itemBinFile
 *            Items (taxa) file
 * @param taxa
 *            List of object representation of taxa, ordered by taxon
 *            number. The calling method must set this data on the object
 *            representation of the intkey dataset.
 */
private static void readTaxonData(ItemsFileHeader itemFileHeader, BinFile itemBinFile, List<Item> taxa) {

    int numItems = itemFileHeader.getNItem();

    for (int i = 0; i < numItems; i++) {
        ItemData itemData = new IntkeyItemData(i + 1);
        Item item = new Item(itemData);
        taxa.add(item);
    }

    // READ TAXON NAMES - rpTnam
    seekToRecord(itemBinFile, itemFileHeader.getRpTnam());

    List<Integer> taxonNameOffsets = readIntegerList(itemBinFile, numItems + 1);

    int recordsSpannedByOffsets = recordsSpannedByBytes(taxonNameOffsets.size() * Constants.SIZE_INT_IN_BYTES);

    seekToRecord(itemBinFile, itemFileHeader.getRpTnam() + recordsSpannedByOffsets);

    ByteBuffer nameBuffer = itemBinFile.readByteBuffer(taxonNameOffsets.get(taxonNameOffsets.size() - 1));
    nameBuffer.position(0);

    for (int i = 0; i < numItems; i++) {
        int start = taxonNameOffsets.get(i);
        int end = taxonNameOffsets.get(i + 1);
        int nameLength = end - start;
        byte[] nameArray = new byte[nameLength];
        nameBuffer.get(nameArray);
        taxa.get(i).setDescription(BinFileEncoding.decode(nameArray));
    }

    readTaxonLinksFiles(itemFileHeader, itemBinFile, taxa);
}

From source file:com.mortardata.pig.storage.DynamoDBStorage.java

private WriteRequestWithCapacity getWriteRequestWithCapacity(Tuple tuple) throws IOException {
    ResourceFieldSchema[] fields = this.schema.getFields();
    Map<String, AttributeValue> dynamoItem = new HashMap<String, AttributeValue>();
    int dataSize = 0;
    int dynamoItemSize = 0;
    int tupleSize = tuple.size();
    for (int i = 0; i < tupleSize; i++) {
        Object field = tuple.get(i);
        AttributeValue dynamoValue = null;

        switch (DataType.findType(field)) {

        case DataType.NULL:
            // dynamodb does not support null values
            // simply don't write field
            reportCounter(DYNAMO_COUNTER_NULL_FIELDS_DISCARDED, 1);
            break;
        case DataType.BOOLEAN:
            if (((Boolean) field).booleanValue()) {
                dynamoValue = new AttributeValue().withN("1");
            } else {
                dynamoValue = new AttributeValue().withN("0");
            }//w  w  w . j a v  a2s.c  o m
            dataSize += 1;
            dynamoItemSize += 1;
            break;
        case DataType.INTEGER:
        case DataType.LONG:
        case DataType.FLOAT:
        case DataType.DOUBLE:
            String numAsString = field.toString();
            dynamoValue = new AttributeValue().withN(numAsString);
            dataSize += numAsString.length();
            dynamoItemSize += numAsString.length();
            break;
        case DataType.BYTEARRAY:
            byte[] b = ((DataByteArray) field).get();
            ByteBuffer buffer = ByteBuffer.allocate(b.length);
            buffer.put(b, 0, b.length);
            buffer.position(0);
            dynamoValue = new AttributeValue().withB(buffer);
            dataSize += b.length;
            dynamoItemSize += b.length;
            break;
        case DataType.CHARARRAY:
            String fieldStr = (String) field;
            int fieldLen = fieldStr.length();
            if (fieldLen > 0) {
                dynamoValue = new AttributeValue().withS(fieldStr);
                dataSize += fieldLen;
                dynamoItemSize += fieldLen;
            } else {
                // DynamoDB cannot handle empty strings
                reportCounter(DYNAMO_COUNTER_EMPTY_STRING_FIELDS_DISCARDED, 1);
            }
            break;
        case DataType.BYTE:
            ByteBuffer buf = ByteBuffer.allocate(1);
            buf.put((Byte) field);
            buf.position(0);
            dynamoValue = new AttributeValue().withB(buf);
            dataSize += 1;
            dynamoItemSize += 1;
            break;
        case DataType.MAP:
        case DataType.TUPLE:
        case DataType.BAG:
            throw new RuntimeException("DynamoDBStorage does not support Maps, Tuples or Bags");
        }

        if (dynamoValue != null) {
            ResourceFieldSchema fieldSchema = fields[i];
            String fieldName = fieldSchema.getName();
            if (fieldName == null) {
                throw new IllegalArgumentException(
                        "Cannot write a field with no name (element " + i + " )  FieldSchema: " + fields);
            }
            dynamoItemSize += fieldName.length();
            dynamoItem.put(fieldName, dynamoValue);
        }
    }

    // check for max item size
    if (dynamoItemSize > DYNAMO_MAX_ITEM_SIZE_IN_BYTES) {
        throw new RuntimeException("Item size " + dynamoItemSize + " bytes is larger than max dynamo item size "
                + DYNAMO_MAX_ITEM_SIZE_IN_BYTES + ". Aborting. Item: " + dynamoItem);
    }

    WriteRequest writeRequest = new WriteRequest().withPutRequest(new PutRequest().withItem(dynamoItem));

    return new WriteRequestWithCapacity(writeRequest, dynamoItemSize, dataSize);

}

From source file:com.koda.integ.hbase.blockcache.OffHeapBlockCacheOld.java

/**
 * Store external.//  w w  w .  j a  va  2s.com
 *
 * @param blockName the block name
 * @param buf the buf
 * @param inMemory the in memory
 * @throws IOException Signals that an I/O exception has occurred.
 */
@SuppressWarnings("unused")
private void storeExternal(String blockName, Cacheable buf, boolean inMemory) throws IOException {
    // If external storage is disable - bail out
    if (overflowExtEnabled == false)
        return;
    // Check if we have  already this block in external storage cache
    if (extStorageCache.contains(blockName))
        return;

    ByteBuffer buffer = extStorageCache.getLocalBufferWithAddress().getBuffer();
    deserializer.set(buf.getDeserializer());
    buffer.clear();

    buffer.position(4);
    buffer.put(inMemory ? (byte) 1 : (byte) 0);
    buf.serialize(buffer);
    buffer.putInt(0, buffer.position() - 4);

    StorageHandle handle = storage.storeData(buffer);

    try {
        extStorageCache.put(blockName, handle);
    } catch (Exception e) {
        throw new IOException(e);
    }

}