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.hyperic.hq.agent.client.AgentConnection.java

private AgentStreamPair sendCommandHeadersWithRetries(String cmdName, int cmdVersion, AgentRemoteValue arg,
        int maxRetries) throws IOException {
    IOException ex = null;//from  w  ww  . jav a  2 s.  c om
    AgentStreamPair streamPair = null;
    Socket s = null;
    int tries = 0;
    while (tries++ < maxRetries) {
        try {
            s = getSocket();
            streamPair = new SocketStreamPair(s, s.getInputStream(), s.getOutputStream());
            DataOutputStream outputStream = new DataOutputStream(streamPair.getOutputStream());
            outputStream.writeInt(_agentAPI.getVersion());
            outputStream.writeInt(cmdVersion);
            outputStream.writeUTF(cmdName);
            arg.toStream(outputStream);
            outputStream.flush();
            return streamPair;
        } catch (IOException e) {
            ex = e;
            close(s);
        }
        if (tries >= maxRetries) {
            break;
        }
        try {
            Thread.sleep(SLEEP_TIME);
        } catch (InterruptedException e) {
            log.debug(e, e);
        }
    }
    if (ex != null) {
        IOException toThrow = new IOException(ex.getMessage() + ", retried " + MAX_RETRIES + " times");
        // call initCause instead of constructor to be java 1.5 compat
        toThrow.initCause(ex);
        throw toThrow;
    }
    return streamPair;
}

From source file:SearchMixedRecordDataTypeExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);/*from   ww w .  j av a 2  s .  c o  m*/
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
        } catch (Exception error) {
            alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            byte[] outputRecord;
            String outputString[] = { "Adam", "Bob", "Mary" };
            int outputInteger[] = { 15, 10, 5 };
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            for (int x = 0; x < 3; x++) {
                outputDataStream.writeUTF(outputString[x]);
                outputDataStream.writeInt(outputInteger[x]);
                outputDataStream.flush();
                outputRecord = outputStream.toByteArray();
                recordstore.addRecord(outputRecord, 0, outputRecord.length);
                outputStream.reset();
            }
            outputStream.close();
            outputDataStream.close();
        } catch (Exception error) {
            alert = new Alert("Error Writing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            String inputString;
            byte[] byteInputData = new byte[300];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            if (recordstore.getNumRecords() > 0) {
                filter = new Filter("Mary");
                recordEnumeration = recordstore.enumerateRecords(filter, null, false);
                while (recordEnumeration.hasNextElement()) {
                    recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
                    inputString = inputDataStream.readUTF() + " " + inputDataStream.readInt();
                    alert = new Alert("Reading", inputString, null, AlertType.WARNING);
                    alert.setTimeout(Alert.FOREVER);
                    display.setCurrent(alert);
                }
            }
            inputStream.close();
        } catch (Exception error) {
            alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            recordstore.closeRecordStore();
        } catch (Exception error) {
            alert = new Alert("Error Closing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        if (RecordStore.listRecordStores() != null) {
            try {
                RecordStore.deleteRecordStore("myRecordStore");
                filter.filterClose();
                recordEnumeration.destroy();
            } catch (Exception error) {
                alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }
        }
    }
}

From source file:com.facebook.infrastructure.db.Column.java

public void serialize(IColumn column, DataOutputStream dos) throws IOException {
    dos.writeUTF(column.name());/*from   w  w w .j  av  a2  s  .  c  o  m*/
    dos.writeBoolean(column.isMarkedForDelete());
    dos.writeLong(column.timestamp());
    dos.writeInt(column.value().length);
    dos.write(column.value());
}

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

protected void writeVertexEdge(DataOutputStream stream, VVertex vertex) throws IOException {
    stream.writeInt(vertex.getEdges().size());
    for (VEdge edge : vertex.getEdges()) {
        writeElementId(stream, edge);//from  ww  w  . jav  a  2  s  .  c om
        writeElementLabel(stream, edge);
        long otherVertex = edge.getOtherVertex(vertex.getId());
        boolean direction = edge.isInV(otherVertex);
        stream.writeBoolean(direction);
        boolean sharedShard = edge.getOutVGraph().getShardId() == edge.getGraphShard().getShardId();
        stream.writeBoolean(sharedShard);
        if (!sharedShard) {
            if (vertex.getGraphShard().getShardId() == edge.getGraphShard().getShardId()) {
                writeElementId(edge.getOutVGraph().getShardId(), stream);
            } else {
                writeElementId(edge.getGraphShard().getShardId(), stream);
            }
        }
        writeElementId(otherVertex, stream);
    }
}

From source file:com.rapidminer.cryptography.hashing.DigesterProvider.java

/**
 * Writes the provided value to the provided {@link DataOutputStream}.
 *//*w  w w  .j  a v a 2 s . com*/
private void writeBytes(Object value, DataOutputStream dos) throws IOException, JEPFunctionException {
    if (value instanceof String) {
        dos.writeUTF((String) value);
    } else if (value instanceof Integer) {
        dos.writeInt((int) value);
    } else if (value instanceof Long) {
        dos.writeLong((long) value);
    } else if (value instanceof Float) {
        dos.writeFloat((float) value);
    } else if (value instanceof GregorianCalendar) {
        dos.writeLong(((GregorianCalendar) value).getTimeInMillis());
    } else if (value instanceof Date) {
        dos.writeLong(((Date) value).getTime());
    } else if (value instanceof Double) {
        dos.writeDouble((double) value);
    } else if (value instanceof UnknownValue) {
        dos.writeDouble(Double.NaN);
    } else {
        // should not happen
        throw new JEPFunctionException("Unknown input type: " + value.getClass());
    }
}

From source file:SortMixedRecordDataTypeExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);/*from  ww  w.  ja v  a 2  s .  c o  m*/
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
        } catch (Exception error) {
            alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            byte[] outputRecord;
            String outputString[] = { "Mary", "Bob", "Adam" };
            int outputInteger[] = { 15, 10, 5 };
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            for (int x = 0; x < 3; x++) {
                outputDataStream.writeUTF(outputString[x]);
                outputDataStream.writeInt(outputInteger[x]);
                outputDataStream.flush();
                outputRecord = outputStream.toByteArray();
                recordstore.addRecord(outputRecord, 0, outputRecord.length);
                outputStream.reset();
            }
            outputStream.close();
            outputDataStream.close();
        } catch (Exception error) {
            alert = new Alert("Error Writing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            String[] inputString = new String[3];
            int z = 0;
            byte[] byteInputData = new byte[300];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            StringBuffer buffer = new StringBuffer();
            comparator = new Comparator();
            recordEnumeration = recordstore.enumerateRecords(null, comparator, false);
            while (recordEnumeration.hasNextElement()) {
                recordstore.getRecord(recordEnumeration.nextRecordId(), byteInputData, 0);
                buffer.append(inputDataStream.readUTF());
                buffer.append(inputDataStream.readInt());
                buffer.append("\n");
                inputDataStream.reset();
            }
            alert = new Alert("Reading", buffer.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
            inputDataStream.close();
            inputStream.close();
        } catch (Exception error) {
            alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            recordstore.closeRecordStore();
        } catch (Exception error) {
            alert = new Alert("Error Closing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        if (RecordStore.listRecordStores() != null) {
            try {
                RecordStore.deleteRecordStore("myRecordStore");
                comparator.compareClose();
                recordEnumeration.destroy();
            } catch (Exception error) {
                alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }
        }
    }
}

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

protected void writeGraphBloom(VSubGraph graph, DataOutputStream stream) throws IOException {
    byte[] bloomBytes = graph.getBloomBytes();
    stream.writeInt(bloomBytes.length);
    stream.write(bloomBytes);//from   ww w. j  ava 2  s .c om
}

From source file:com.opensoc.json.serialization.JSONKafkaSerializer.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public byte[] toBytes(JSONObject input) {

    ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
    DataOutputStream data = new DataOutputStream(outputBuffer);

    Iterator it = input.entrySet().iterator();
    try {//from ww w  .  jav  a 2  s. c  om

        // write num of entries into output. 
        //each KV pair is counted as an entry
        data.writeInt(input.size());

        // Write every single entry in hashmap
        //Assuming key to be String.
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = (Entry<String, Object>) it.next();
            putObject(data, entry.getKey());
            putObject(data, entry.getValue());
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return outputBuffer.toByteArray();
}

From source file:hd3gtv.embddb.network.DataBlock.java

byte[] getBytes(Protocol protocol) throws IOException {
    checkIfNotEmpty();/*from   ww  w  . j  ava2 s  .c om*/

    ByteArrayOutputStream byte_array_out_stream = new ByteArrayOutputStream(Protocol.BUFFER_SIZE);

    DataOutputStream dos = new DataOutputStream(byte_array_out_stream);
    dos.write(Protocol.APP_SOCKET_HEADER_TAG);
    dos.writeInt(Protocol.VERSION);

    /**
     * Start header name
     */
    dos.writeByte(0);
    byte[] request_name_data = request_name.getBytes(Protocol.UTF8);
    dos.writeInt(request_name_data.length);
    dos.write(request_name_data);

    /**
     * Start datas payload
     */
    dos.writeByte(1);

    /**
     * Get datas from zip
     */
    ZipOutputStream zos = new ZipOutputStream(dos);
    zos.setLevel(3);
    entries.forEach(entry -> {
        try {
            entry.toZip(zos);
        } catch (IOException e) {
            log.error("Can't add to zip", e);
        }
    });
    zos.flush();
    zos.finish();
    zos.close();

    dos.flush();
    dos.close();

    byte[] result = byte_array_out_stream.toByteArray();

    if (log.isTraceEnabled()) {
        log.trace("Make raw datas for " + request_name + Hexview.LINESEPARATOR + Hexview.tracelog(result));
    }

    return result;
}