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:com.kitware.tangoproject.paraviewtangorecorder.PointCloudActivity.java

private void writePoseToFile(int numPoints) {

    File mainDir = new File(mMainDirPath);
    if (!mainDir.exists()) {
        boolean created = mainDir.mkdir();
        if (created) {
            Log.i(TAG, "Folder: \"" + mMainDirPath + "\" created\n");
        }// w ww  .j a  v a 2s  .  co m
    }

    File dir = new File(mSaveDirAbsPath);
    if (!dir.exists()) {
        boolean created = dir.mkdir();
        if (created) {
            Log.i(TAG, "Folder: \"" + mSaveDirAbsPath + "\" created\n");
        }
    }
    String poseFileName = "pc_" + mNowTimeString + "_poses.vtk";
    mFilenameBuffer.add(mSaveDirAbsPath + poseFileName);
    File file = new File(dir, poseFileName);

    try {
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

        out.write(("# vtk DataFile Version 3.0\n" + "vtk output\n" + "BINARY\n" + "DATASET POLYDATA\n"
                + "POINTS " + numPoints + " float\n").getBytes());

        for (int i = 0; i < numPoints; i++) {
            out.writeFloat(mPosePositionBuffer.get(i)[0]);
            out.writeFloat(mPosePositionBuffer.get(i)[1]);
            out.writeFloat(mPosePositionBuffer.get(i)[2]);
        }

        out.write(("\nLINES 1 " + String.valueOf(numPoints + 1) + "\n").getBytes());
        out.writeInt(numPoints);
        for (int i = 0; i < numPoints; i++) {
            out.writeInt(i);
        }

        out.write(("\nPOINT_DATA " + String.valueOf(numPoints) + "\n" + "FIELD FieldData 2\n" + "orientation 4 "
                + String.valueOf(numPoints) + " float\n").getBytes());

        for (int i = 0; i < numPoints; i++) {
            out.writeFloat(mPoseOrientationBuffer.get(i)[0]);
            out.writeFloat(mPoseOrientationBuffer.get(i)[1]);
            out.writeFloat(mPoseOrientationBuffer.get(i)[2]);
            out.writeFloat(mPoseOrientationBuffer.get(i)[3]);
        }

        out.write(("\ntimestamp 1 " + String.valueOf(numPoints) + " float\n").getBytes());
        for (int i = 0; i < numPoints; i++) {
            out.writeFloat(mPoseTimestampBuffer.get(i));
        }

        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.kitware.tangoproject.paraviewtangorecorder.PointCloudActivity.java

private void writePointCloudToFile(TangoXyzIjData xyzIj, byte[] buffer,
        ArrayList<TangoCoordinateFramePair> framePairs) {

    ByteBuffer myBuffer = ByteBuffer.allocate(xyzIj.xyzCount * 3 * 4);
    myBuffer.order(ByteOrder.LITTLE_ENDIAN);
    myBuffer.put(buffer, xyzIj.xyzParcelFileDescriptorOffset, myBuffer.capacity());

    File mainDir = new File(mMainDirPath);
    if (!mainDir.exists()) {
        boolean created = mainDir.mkdir();
        if (created) {
            Log.i(TAG, "Folder: \"" + mMainDirPath + "\" created\n");
        }/*from  w  ww . jav a2  s.co  m*/
    }

    File dir = new File(mSaveDirAbsPath);
    if (!dir.exists()) {
        boolean created = dir.mkdir();
        if (created) {
            Log.i(TAG, "Folder: \"" + mSaveDirAbsPath + "\" created\n");
        }
    }

    mFilename = "pc_" + mNowTimeString + "_" + String.format("%03d", mNumberOfFilesWritten) + ".vtk";
    mFilenameBuffer.add(mSaveDirAbsPath + mFilename);
    File file = new File(dir, mFilename);

    try {

        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(file)));

        out.write(("# vtk DataFile Version 3.0\n" + "vtk output\n" + "BINARY\n" + "DATASET POLYDATA\n"
                + "POINTS " + xyzIj.xyzCount + " float\n").getBytes());

        for (int i = 0; i < xyzIj.xyzCount; i++) {

            out.writeFloat(myBuffer.getFloat(3 * i * 4));
            out.writeFloat(myBuffer.getFloat((3 * i + 1) * 4));
            out.writeFloat(myBuffer.getFloat((3 * i + 2) * 4));
        }

        out.write(("\nVERTICES 1 " + String.valueOf(xyzIj.xyzCount + 1) + "\n").getBytes());
        out.writeInt(xyzIj.xyzCount);
        for (int i = 0; i < xyzIj.xyzCount; i++) {
            out.writeInt(i);
        }

        out.write(("\nFIELD FieldData 1\n" + "timestamp 1 1 float\n").getBytes());
        out.writeFloat((float) xyzIj.timestamp);

        out.close();
        mNumberOfFilesWritten++;
        mTimeToTakeSnap = false;

    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.veronicadb.core.memorygraph.storage.SimpleLocalFileStorageSink.java

@SuppressWarnings("resource")
@Override//from w ww . ja va  2  s.  c  om
public long writeGraphBlock(VSubGraph graph) throws VStorageFailureException {
    long time = System.currentTimeMillis();
    long graphId = graph.getGraphId();
    File graphShardFile = new File(storageDirectory, graphId + "_" + time + ".vr");
    DataOutputStream stream = null;
    OutputStream baseStream = null;
    try {
        baseStream = new BufferedOutputStream(new FileOutputStream(graphShardFile), BUFFER_SIZE);
        if (compress) {
            baseStream = (OutputStream) compressor.getDeclaredConstructor(OutputStream.class)
                    .newInstance(baseStream);
        }
        stream = new DataOutputStream(baseStream);
    } catch (FileNotFoundException e) {
        throw new VStorageFailureException(SimpleLocalFileStorageSink.class, "Storage file not found", e);
    } catch (InstantiationException | IllegalAccessException | IllegalArgumentException
            | InvocationTargetException | NoSuchMethodException | SecurityException e) {
        throw new VStorageFailureException(SimpleLocalFileStorageSink.class,
                "Failed to initialize pluggable compressor", e);
    }
    try {
        writeElementId(graphId, stream);
        List<VVertex> vertices = graph.getShardVertices();
        stream.writeInt(vertices.size());
        writeGraphBloom(graph, stream);
        writeVertices(vertices, stream);
    } catch (IOException e) {
        throw new VStorageFailureException(SimpleLocalFileStorageSink.class, "Graph block write failed", e);
    } finally {
        try {
            stream.close();
            baseStream.close();
        } catch (IOException e) {
            throw new VStorageFailureException(SimpleLocalFileStorageSink.class,
                    "Failed to close shard file stream", e);
        }
    }
    return time;
}

From source file:bobs.is.compress.sevenzip.SevenZOutputFile.java

private void writeFileWindowsAttributes(final DataOutput header) throws IOException {
    int numWindowsAttributes = 0;
    for (final SevenZArchiveEntry entry : files) {
        if (entry.getHasWindowsAttributes()) {
            ++numWindowsAttributes;//from   w  ww  . j  a v  a2 s  .  c  o m
        }
    }
    if (numWindowsAttributes > 0) {
        header.write(NID.kWinAttributes);

        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        final DataOutputStream out = new DataOutputStream(baos);
        if (numWindowsAttributes != files.size()) {
            out.write(0);
            final BitSet attributes = new BitSet(files.size());
            for (int i = 0; i < files.size(); i++) {
                attributes.set(i, files.get(i).getHasWindowsAttributes());
            }
            writeBits(out, attributes, files.size());
        } else {
            out.write(1); // "allAreDefined" == true
        }
        out.write(0);
        for (final SevenZArchiveEntry entry : files) {
            if (entry.getHasWindowsAttributes()) {
                out.writeInt(Integer.reverseBytes(entry.getWindowsAttributes()));
            }
        }
        out.flush();
        final byte[] contents = baos.toByteArray();
        writeUint64(header, contents.length);
        header.write(contents);
    }
}

From source file:org.apache.jxtadoop.hdfs.server.datanode.DataXceiver.java

/**
 * Reads the metadata and sends the data in one 'DATA_CHUNK'.
 * @param in//from  w  w w.j a  va  2  s .co m
 */
void readMetadata(DataInputStream in) throws IOException {
    LOG.debug("Mathod called : readMetadata()");
    Block block = new Block(in.readLong(), 0, in.readLong());
    MetaDataInputStream checksumIn = null;
    DataOutputStream out = null;

    try {

        checksumIn = datanode.data.getMetaDataInputStream(block);

        long fileSize = checksumIn.getLength();

        if (fileSize >= 1L << 31 || fileSize <= 0) {
            throw new IOException("Unexpected size for checksumFile of block" + block);
        }

        byte[] buf = new byte[(int) fileSize];
        IOUtils.readFully(checksumIn, buf, 0, buf.length);

        //out = new DataOutputStream(
        //         NetUtils.getOutputStream(s, datanode.socketWriteTimeout));
        out = new DataOutputStream(s.getOutputStream());

        out.writeByte(DataTransferProtocol.OP_STATUS_SUCCESS);
        out.writeInt(buf.length);
        out.write(buf);

        //last DATA_CHUNK
        out.writeInt(0);
    } finally {
        LOG.debug("Finalizing : readMetadata()");
        IOUtils.closeStream(out);
        IOUtils.closeStream(checksumIn);
    }
}

From source file:org.grails.gsp.compiler.GroovyPageParser.java

public void writeHtmlParts(File filename) throws IOException {
    DataOutputStream dataOut = null;
    try {/*from w w  w  .  j  a v a2s .  c  o m*/
        dataOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
        dataOut.writeInt(htmlParts.size());
        for (String part : htmlParts) {
            dataOut.writeUTF(part);
        }
    } finally {
        SpringIOUtils.closeQuietly(dataOut);
    }
}

From source file:org.apache.jmeter.protocol.mqttws.client.MqttPublisher.java

public byte[] createPayload(String message, String useTimeStamp, String useNumSeq, String type_value,
        String format, String charset) throws IOException, NumberFormatException {
    ByteArrayOutputStream b = new ByteArrayOutputStream();
    DataOutputStream d = new DataOutputStream(b);
    // flags     
    byte flags = 0x00;
    if ("TRUE".equals(useTimeStamp))
        flags |= 0x80;//ww  w.  jav a2  s.  c om
    if ("TRUE".equals(useNumSeq))
        flags |= 0x40;
    if (MQTTPublisherGui.INT.equals(type_value))
        flags |= 0x20;
    if (MQTTPublisherGui.LONG.equals(type_value))
        flags |= 0x10;
    if (MQTTPublisherGui.FLOAT.equals(type_value))
        flags |= 0x08;
    if (MQTTPublisherGui.DOUBLE.equals(type_value))
        flags |= 0x04;
    if (MQTTPublisherGui.STRING.equals(type_value))
        flags |= 0x02;
    if (!"TEXT".equals(type_value)) {
        d.writeByte(flags);
    }
    // TimeStamp
    if ("TRUE".equals(useTimeStamp)) {
        Date date = new java.util.Date();
        d.writeLong(date.getTime());
    }
    // Number Sequence
    if ("TRUE".equals(useNumSeq)) {
        d.writeInt(numSeq++);

    }
    // Value            
    if (MQTTPublisherGui.INT.equals(type_value)) {
        d.writeInt(Integer.parseInt(message));
    } else if (MQTTPublisherGui.LONG.equals(type_value)) {
        d.writeLong(Long.parseLong(message));
    } else if (MQTTPublisherGui.DOUBLE.equals(type_value)) {
        d.writeDouble(Double.parseDouble(message));
    } else if (MQTTPublisherGui.FLOAT.equals(type_value)) {
        d.writeDouble(Float.parseFloat(message));
    } else if (MQTTPublisherGui.STRING.equals(type_value)) {
        d.write(message.getBytes());
    } else if ("TEXT".equals(type_value)) {
        d.write(message.getBytes());
    } else if ("TEXT_POOL".equals(type_value)) {
        String random_message = createRandomMessageFromPool(message);
        d.write(random_message.getBytes());
    }

    // Format: Encoding        
    if (MQTTPublisherGui.BINARY.equals(format)) {
        BinaryCodec encoder = new BinaryCodec();
        return encoder.encode(b.toByteArray());
    } else if (MQTTPublisherGui.BASE64.equals(format)) {
        return Base64.encodeBase64(b.toByteArray());
    } else if (MQTTPublisherGui.BINHEX.equals(format)) {
        Hex encoder = new Hex();
        return encoder.encode(b.toByteArray());
    } else if (MQTTPublisherGui.PLAIN_TEXT.equals(format)) {
        String s = new String(b.toByteArray(), charset);
        return s.getBytes();

    } else
        return b.toByteArray();
}

From source file:org.codehaus.groovy.grails.web.pages.GroovyPageParser.java

public void writeHtmlParts(File filename) throws IOException {
    DataOutputStream dataOut = null;
    try {//  ww  w.jav a  2s .c  o m
        dataOut = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(filename)));
        dataOut.writeInt(htmlParts.size());
        for (String part : htmlParts) {
            dataOut.writeUTF(part);
        }
    } finally {
        IOUtils.closeQuietly(dataOut);
    }
}

From source file:com.trifork.riak.RiakClient.java

public ByteString[] store(RiakObject[] values, RequestMeta meta) throws IOException {

    RiakConnection c = getConnection();/*  w  ww .  j  a v a  2  s.com*/
    try {
        BulkReader reader = new BulkReader(c, values.length);
        Thread worker = new Thread(reader);
        worker.start();

        DataOutputStream dout = c.getOutputStream();

        for (int i = 0; i < values.length; i++) {
            RiakObject value = values[i];

            RPB.RpbPutReq.Builder builder = RPB.RpbPutReq.newBuilder().setBucket(value.getBucketBS())
                    .setKey(value.getKeyBS()).setContent(value.buildContent());

            if (value.getVclock() != null) {
                builder.setVclock(value.getVclock());
            }

            builder.setReturnBody(false);

            if (meta != null) {

                if (meta.writeQuorum != null) {
                    builder.setW(meta.writeQuorum.intValue());
                }

                if (meta.durableWriteQuorum != null) {
                    builder.setDw(meta.durableWriteQuorum.intValue());
                }
            }

            RpbPutReq req = builder.build();

            int len = req.getSerializedSize();
            dout.writeInt(len + 1);
            dout.write(MSG_PutReq);
            req.writeTo(dout);
        }

        dout.flush();

        try {
            worker.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return reader.vclocks;
    } finally {
        release(c);
    }
}

From source file:ClassFile.java

public void write(DataOutputStream dos, ConstantPoolInfo pool[]) throws IOException, Exception {
    dos.writeShort(ConstantPoolInfo.indexOf(name, pool));
    dos.writeInt(data.length);
    dos.write(data, 0, data.length);/*from  w w w. jav a  2  s  .  c om*/
}