Example usage for java.io DataOutputStream writeInt

List of usage examples for java.io DataOutputStream writeInt

Introduction

In this page you can find the example usage for java.io DataOutputStream writeInt.

Prototype

public final void writeInt(int v) throws IOException 

Source Link

Document

Writes an int to the underlying output stream as four bytes, high byte first.

Usage

From source file:org.bdval.cache.TableCache.java

/**
 * Given the specified paramters, save the table to the cache.
 *
 * @param splitId     The split id//from  w w w .  j ava2 s  .c  o  m
 * @param splitType   The Split type
 * @param datasetName The dataset name
 * @param table       the Table to save.
 */
public void saveTableToCache(final int splitId, final String splitType, final String datasetName,
        final Table table) {
    DataOutputStream dataOutput = null;
    try {
        if (!checkTableConfiguration(table)) {
            return;
        }

        final int numColumns = table.getColumnNumber();

        final File cachedTableFile = getCachedTableFile(splitId, splitType, datasetName);
        dataOutput = new DataOutputStream(new FastBufferedOutputStream(new FileOutputStream(cachedTableFile)));

        // Write the number of columns
        dataOutput.writeInt(numColumns);
        LOG.info("Writing " + numColumns + " columns");
        int numWritten = 0;
        for (int i = 0; i < numColumns; i++) {
            // For each column write if it is a "d"ouble or "s"tring column
            final String id = table.getIdentifier(i);
            if (table.getType(i) == String.class) {
                dataOutput.writeUTF("s");
                dataOutput.writeUTF(id);
                final String[] stringColumnData = table.getStrings(id);
                final int numStrings = stringColumnData.length;
                dataOutput.writeInt(numStrings);
                for (final String stringColumnItem : stringColumnData) {
                    // Each String
                    dataOutput.writeUTF(stringColumnItem);
                }
                numWritten++;
            } else if (table.getType(i) == double.class) {
                dataOutput.writeUTF("d");
                dataOutput.writeUTF(id);
                final double[] doubleColumnData = table.getDoubles(id);
                final int numDoubles = doubleColumnData.length;
                dataOutput.writeInt(numDoubles);
                for (final double doubleColumnItem : doubleColumnData) {
                    // Each double
                    dataOutput.writeDouble(doubleColumnItem);
                }
                numWritten++;
            }
        }

        dataOutput.flush();

        LOG.info("Wrote " + numWritten + " columns");
        LOG.info("++ SAVED TABLE TO CACHE for split-id=" + splitId + ", split-type=" + splitType
                + ", dataset-name=" + datasetName);
    } catch (IOException e) {
        LOG.error("Cannot cache table. ", e);
    } catch (InvalidColumnException e) {
        LOG.error("Invalid table data", e);
    } finally {
        IOUtils.closeQuietly(dataOutput);
    }
}

From source file:org.apache.hadoop.hbase.io.hfile.TestHFileBlock.java

protected void testReaderV2Internals() throws IOException {
    if (includesTag) {
        TEST_UTIL.getConfiguration().setInt("hfile.format.version", 3);
    }/*  w  w w  .j a v a  2 s.c o m*/
    for (Compression.Algorithm algo : COMPRESSION_ALGORITHMS) {
        for (boolean pread : new boolean[] { false, true }) {
            LOG.info("testReaderV2: Compression algorithm: " + algo + ", pread=" + pread);
            Path path = new Path(TEST_UTIL.getDataTestDir(), "blocks_v2_" + algo);
            FSDataOutputStream os = fs.create(path);
            HFileContext meta = new HFileContextBuilder().withCompression(algo)
                    .withIncludesMvcc(includesMemstoreTS).withIncludesTags(includesTag)
                    .withBytesPerCheckSum(HFile.DEFAULT_BYTES_PER_CHECKSUM)
                    .withChecksumType(HFile.DEFAULT_CHECKSUM_TYPE).build();
            HFileBlock.Writer hbw = new HFileBlock.Writer(null, meta);
            long totalSize = 0;
            for (int blockId = 0; blockId < 2; ++blockId) {
                DataOutputStream dos = hbw.startWriting(BlockType.DATA);
                for (int i = 0; i < 1234; ++i)
                    dos.writeInt(i);
                hbw.writeHeaderAndData(os);
                totalSize += hbw.getOnDiskSizeWithHeader();
            }
            os.close();

            FSDataInputStream is = fs.open(path);
            meta = new HFileContextBuilder().withHBaseCheckSum(true).withIncludesMvcc(includesMemstoreTS)
                    .withIncludesTags(includesTag).withCompression(algo).build();
            HFileBlock.FSReader hbr = new HFileBlock.FSReaderV2(is, totalSize, meta);
            HFileBlock b = hbr.readBlockData(0, -1, -1, pread);
            is.close();
            assertEquals(0, HFile.getChecksumFailuresCount());

            b.sanityCheck();
            assertEquals(4936, b.getUncompressedSizeWithoutHeader());
            assertEquals(algo == GZ ? 2173 : 4936, b.getOnDiskSizeWithoutHeader() - b.totalChecksumBytes());
            String blockStr = b.toString();

            if (algo == GZ) {
                is = fs.open(path);
                hbr = new HFileBlock.FSReaderV2(is, totalSize, meta);
                b = hbr.readBlockData(0, 2173 + HConstants.HFILEBLOCK_HEADER_SIZE + b.totalChecksumBytes(), -1,
                        pread);
                assertEquals(blockStr, b.toString());
                int wrongCompressedSize = 2172;
                try {
                    b = hbr.readBlockData(0, wrongCompressedSize + HConstants.HFILEBLOCK_HEADER_SIZE, -1,
                            pread);
                    fail("Exception expected");
                } catch (IOException ex) {
                    String expectedPrefix = "On-disk size without header provided is " + wrongCompressedSize
                            + ", but block header contains " + b.getOnDiskSizeWithoutHeader() + ".";
                    assertTrue(
                            "Invalid exception message: '" + ex.getMessage()
                                    + "'.\nMessage is expected to start with: '" + expectedPrefix + "'",
                            ex.getMessage().startsWith(expectedPrefix));
                }
                is.close();
            }
        }
    }
}

From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java

/**
 * Serializes a <code>NodePropBundle</code> to a data output stream
 *
 * @param out the output stream//  www  . j av  a  2  s  .  co m
 * @param bundle the bundle to serialize
 * @throws IOException if an I/O error occurs.
 */
public void writeBundle(DataOutputStream out, NodePropBundle bundle) throws IOException {
    long size = out.size();

    // primaryType and version
    out.writeInt((VERSION_CURRENT << 24) | nsIndex.stringToIndex(bundle.getNodeTypeName().getNamespaceURI()));
    out.writeInt(nameIndex.stringToIndex(bundle.getNodeTypeName().getLocalName()));

    // parentUUID
    writeID(out, bundle.getParentId());

    // definitionId
    out.writeUTF("");

    // mixin types
    Iterator iter = bundle.getMixinTypeNames().iterator();
    while (iter.hasNext()) {
        writeIndexedQName(out, (Name) iter.next());
    }
    writeIndexedQName(out, null);

    // properties
    iter = bundle.getPropertyNames().iterator();
    while (iter.hasNext()) {
        Name pName = (Name) iter.next();
        // skip redundant primaryType, mixinTypes and uuid properties
        if (pName.equals(NameConstants.JCR_PRIMARYTYPE) || pName.equals(NameConstants.JCR_MIXINTYPES)
                || pName.equals(NameConstants.JCR_UUID)) {
            continue;
        }
        NodePropBundle.PropertyEntry pState = bundle.getPropertyEntry(pName);
        if (pState == null) {
            log.error("PropertyState missing in bundle: " + pName);
        } else {
            writeIndexedQName(out, pName);
            writeState(out, pState);
        }
    }
    writeIndexedQName(out, null);

    // write uuid flag
    out.writeBoolean(bundle.isReferenceable());

    // child nodes (list of uuid/name pairs)
    iter = bundle.getChildNodeEntries().iterator();
    while (iter.hasNext()) {
        NodePropBundle.ChildNodeEntry entry = (NodePropBundle.ChildNodeEntry) iter.next();
        writeID(out, entry.getId()); // uuid
        writeQName(out, entry.getName()); // name
    }
    writeID(out, null);

    // write mod count
    writeModCount(out, bundle.getModCount());

    // write shared set
    iter = bundle.getSharedSet().iterator();
    while (iter.hasNext()) {
        writeID(out, (NodeId) iter.next());
    }
    writeID(out, null);

    // set size of bundle
    bundle.setSize(out.size() - size);
}

From source file:com.linkedin.pinot.common.utils.DataTable.java

private byte[] serializeMetadata() throws Exception {
    if (metadata != null) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos);
        out.writeInt(metadata.size());
        for (Entry<String, String> entry : metadata.entrySet()) {
            byte[] keyBytes = entry.getKey().getBytes(UTF8);
            out.writeInt(keyBytes.length);
            out.write(keyBytes);/*  www . j a v a  2  s  .  c o  m*/
            byte[] valueBytes = entry.getValue().getBytes(UTF8);
            out.writeInt(valueBytes.length);
            out.write(valueBytes);
        }
        return baos.toByteArray();
    }
    return new byte[0];
}

From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java

/**
 * Serializes a <code>PropertyState</code> to the data output stream
 *
 * @param out the output stream//from  w w  w  . j  a  va2s . c  om
 * @param state the property entry to store
 * @throws IOException if an I/O error occurs.
 */
public void writeState(DataOutputStream out, NodePropBundle.PropertyEntry state) throws IOException {
    // type & mod count
    out.writeInt(state.getType() | (state.getModCount() << 16));
    // multiValued
    out.writeBoolean(state.isMultiValued());
    // definitionId
    out.writeUTF("");
    // values
    InternalValue[] values = state.getValues();
    out.writeInt(values.length); // count
    for (int i = 0; i < values.length; i++) {
        InternalValue val = values[i];
        switch (state.getType()) {
        case PropertyType.BINARY:
            BLOBFileValue blobVal = val.getBLOBFileValue();
            long size = blobVal.getLength();
            if (InternalValue.USE_DATA_STORE && dataStore != null) {
                int maxMemorySize = dataStore.getMinRecordLength() - 1;
                if (size < maxMemorySize) {
                    writeSmallBinary(out, blobVal, state, i);
                } else {
                    out.writeInt(BINARY_IN_DATA_STORE);
                    try {
                        val.store(dataStore);
                    } catch (RepositoryException e) {
                        String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " size="
                                + val.getBLOBFileValue().getLength();
                        log.error(msg, e);
                        throw new IOException(msg);
                    }
                    out.writeUTF(val.toString());
                }
                break;
            }
            // special handling required for binary value:
            // spool binary value to file in blob store
            if (size < 0) {
                log.warn("Blob has negative size. Potential loss of data. " + "id={} idx={}", state.getId(),
                        String.valueOf(i));
                out.writeInt(0);
                values[i] = InternalValue.create(new byte[0]);
                blobVal.discard();
            } else if (size > minBlobSize) {
                out.writeInt(BINARY_IN_BLOB_STORE);
                String blobId = state.getBlobId(i);
                if (blobId == null) {
                    try {
                        InputStream in = blobVal.getStream();
                        try {
                            blobId = blobStore.createId(state.getId(), i);
                            blobStore.put(blobId, in, size);
                            state.setBlobId(blobId, i);
                        } finally {
                            IOUtils.closeQuietly(in);
                        }
                    } catch (Exception e) {
                        String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " size="
                                + size;
                        log.error(msg, e);
                        throw new IOException(msg);
                    }
                    try {
                        // replace value instance with value
                        // backed by resource in blob store and delete temp file
                        if (blobStore instanceof ResourceBasedBLOBStore) {
                            values[i] = InternalValue
                                    .create(((ResourceBasedBLOBStore) blobStore).getResource(blobId));
                        } else {
                            values[i] = InternalValue.create(blobStore.get(blobId));
                        }
                    } catch (Exception e) {
                        log.error("Error while reloading blob. truncating. id=" + state.getId() + " idx=" + i
                                + " size=" + size, e);
                        values[i] = InternalValue.create(new byte[0]);
                    }
                    blobVal.discard();
                }
                // store id of blob as property value
                out.writeUTF(blobId); // value
            } else {
                // delete evt. blob
                byte[] data = writeSmallBinary(out, blobVal, state, i);
                // replace value instance with value
                // backed by resource in blob store and delete temp file
                values[i] = InternalValue.create(data);
                blobVal.discard();
            }
            break;
        case PropertyType.DOUBLE:
            out.writeDouble(val.getDouble());
            break;
        case PropertyType.LONG:
            out.writeLong(val.getLong());
            break;
        case PropertyType.BOOLEAN:
            out.writeBoolean(val.getBoolean());
            break;
        case PropertyType.NAME:
            writeQName(out, val.getQName());
            break;
        case PropertyType.REFERENCE:
            writeUUID(out, val.getUUID());
            break;
        default:
            // because writeUTF(String) has a size limit of 64k,
            // we're using write(byte[]) instead
            byte[] bytes = val.toString().getBytes("UTF-8");
            out.writeInt(bytes.length); // length of byte[]
            out.write(bytes); // byte[]
        }
    }
}

From source file:com.linkedin.pinot.common.utils.DataTable.java

private byte[] serializeDictionary() throws Exception {
    if (dictionary != null) {
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos);
        out.writeInt(dictionary.size());
        for (String key : dictionary.keySet()) {
            byte[] bytes = key.getBytes(UTF8);
            out.writeInt(bytes.length);//from   ww  w .j  a v  a2  s.c  o  m
            out.write(bytes);
            Map<Integer, String> map = dictionary.get(key);
            out.writeInt(map.size());
            for (Entry<Integer, String> entry : map.entrySet()) {
                out.writeInt(entry.getKey());
                byte[] valueBytes = entry.getValue().getBytes(UTF8);
                out.writeInt(valueBytes.length);
                out.write(valueBytes);
            }
        }
        return baos.toByteArray();
    }
    return new byte[0];
}

From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java

/**
 * Serializes a <code>NodePropBundle</code> to a data output stream
 *
 * @param out the output stream/*from  ww  w .j  a  v  a 2  s .  co  m*/
 * @param bundle the bundle to serialize
 * @throws IOException if an I/O error occurs.
 */
public void writeBundle(DataOutputStream out, NodePropBundle bundle) throws IOException {
    long size = out.size();

    // primaryType and version
    out.writeInt((VERSION_CURRENT << 24) | nsIndex.stringToIndex(bundle.getNodeTypeName().getNamespaceURI()));
    out.writeInt(nameIndex.stringToIndex(bundle.getNodeTypeName().getLocalName()));

    // parentUUID
    writeID(out, bundle.getParentId());

    // definitionId
    out.writeUTF(bundle.getNodeDefId().toString());

    // mixin types
    for (Name name : bundle.getMixinTypeNames()) {
        writeIndexedQName(out, name);
    }
    writeIndexedQName(out, null);

    // properties
    for (Name pName : bundle.getPropertyNames()) {
        // skip redundant primaryType, mixinTypes and uuid properties
        if (pName.equals(NameConstants.JCR_PRIMARYTYPE) || pName.equals(NameConstants.JCR_MIXINTYPES)
                || pName.equals(NameConstants.JCR_UUID)) {
            continue;
        }
        NodePropBundle.PropertyEntry pState = bundle.getPropertyEntry(pName);
        if (pState == null) {
            log.error("PropertyState missing in bundle: " + pName);
        } else {
            writeIndexedQName(out, pName);
            writeState(out, pState);
        }
    }
    writeIndexedQName(out, null);

    // write uuid flag
    out.writeBoolean(bundle.isReferenceable());

    // child nodes (list of uuid/name pairs)
    for (NodePropBundle.ChildNodeEntry entry : bundle.getChildNodeEntries()) {
        writeID(out, entry.getId()); // uuid
        writeQName(out, entry.getName()); // name
    }
    writeID(out, null);

    // write mod count
    writeModCount(out, bundle.getModCount());

    // write shared set
    for (NodeId nodeId : bundle.getSharedSet()) {
        writeID(out, nodeId);
    }
    writeID(out, null);

    // set size of bundle
    bundle.setSize(out.size() - size);
}

From source file:PersistentRankingMIDlet.java

private byte[] toByteArray(BookInfo bookInfo) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream os = new DataOutputStream(baos);

    os.writeUTF(bookInfo.isbn);//from   w ww .ja va2s  . c  o  m
    os.writeUTF(bookInfo.title == null ? "" : bookInfo.title);
    os.writeInt(bookInfo.ranking);
    os.writeInt(bookInfo.reviews);
    os.writeInt(bookInfo.lastRanking);
    os.writeInt(bookInfo.lastReviews);

    return baos.toByteArray();
}

From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java

/**
 * Write a small binary value and return the data.
 *
 * @param out the output stream to write
 * @param blobVal the binary value//w ww.j  a  va 2  s. c  om
 * @param state the property state (for error messages)
 * @param i the index (for error messages)
 * @return the data
 * @throws IOException if the data could not be read
 */
private byte[] writeSmallBinary(DataOutputStream out, InternalValue value, NodePropBundle.PropertyEntry state,
        int i) throws IOException {
    try {
        int size = (int) value.getLength();
        out.writeInt(size);
        byte[] data = new byte[size];
        DataInputStream in = new DataInputStream(value.getStream());
        try {
            in.readFully(data);
        } finally {
            IOUtils.closeQuietly(in);
        }
        out.write(data, 0, data.length);
        return data;
    } catch (Exception e) {
        String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " value=" + value;
        log.error(msg, e);
        throw new IOException(msg);
    }
}

From source file:com.ebay.erl.mobius.core.collection.BigTupleList.java

/**
 * Flush {@link Tuple}s in {@link #buffer_in_memory} into
 * disk, and new local file will be created by {@link #newLocalFile()}
 * and store the {@link File} reference in {@link #buffer_on_disk} for
 * future reference./*  w w  w  .j a  v  a2  s . c o  m*/
 */
private void flushToDisk() {
    this.flushing = true;
    File localFile;

    if (this.buffer_in_memory.size() == 0) {
        // no tuple in memory
        return;
    }
    long start = System.currentTimeMillis();
    long availableMemory = this.availableMemory();

    String message = Thread.currentThread().toString() + " BID[" + this._ID + "] "
            + "writing in-memory tuples (" + getNumberFormat().format(this.buffer_in_memory.size())
            + " entries) into disk, " + "available memory:" + availableMemory / _MB + "MB.";

    LOGGER.info(message);
    if (this.reporter != null) {
        this.reporter.setStatus(message);
        this.reporter.progress();
    }

    try {
        // check if we still have enough local space to prevent 
        // full of disk exception.
        long freeDiskSpace = this.workOutput.getFreeSpace() / _MB;
        if (freeDiskSpace < 300) {
            // less than 300MB free space left, throw
            // exceptions
            throw new IOException("Not enough space left (" + freeDiskSpace + "MB remaining) on "
                    + this.workOutput.getAbsolutePath() + ".");
        }

        localFile = this.newLocalFile();
        DataOutputStream out = new DataOutputStream(
                new GZIPOutputStream(new BufferedOutputStream(new FileOutputStream(localFile))));

        // write the tuple schema in the header
        String[] tupleSchema = this.buffer_in_memory.get(0).getSchema();
        out.writeInt(tupleSchema.length);
        if (tupleSchema.length == 0)
            throw new IllegalArgumentException("Tuple with empty schema!");
        for (String aColumn : tupleSchema) {
            out.writeUTF(aColumn);
        }

        // write number of tuple in this file
        out.writeLong(this.buffer_in_memory.size());

        if (this.comparator != null) {
            // sort the Tuple in memory first
            Collections.sort(this.buffer_in_memory, this.comparator);
        }

        // write all the tuple in memory buffer
        long counts = 0L;
        for (Tuple aTuple : this.buffer_in_memory) {
            aTuple.write(out);
            counts++;
            if (counts % 5000 == 0 && this.reporter != null)// report every 5000 IO
                this.reporter.progress();
        }
        out.flush();
        out.close();

        // clear memory buffer
        this.buffer_in_memory.clear();

        long end = System.currentTimeMillis();

        LOGGER.info(Thread.currentThread().toString() + " BID[" + this._ID + "] " + "Write has completed, cost "
                + ((end - start) / 1000) + " seconds, " + "available memory:" + this.availableMemory() / _MB
                + "MB, " + "wrote to:" + localFile.getAbsolutePath() + "(size:"
                + localFile.getTotalSpace() / _MB + "MB) , " + "in memory tuples numbers:"
                + this.buffer_in_memory.size());

        this.flushing = false;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}