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.getspout.spout.packet.PacketCustomBlockChunkOverride.java

public void writeData(DataOutputStream output) throws IOException {
    output.writeInt(chunkX);
    output.writeInt(chunkZ);/*from ww  w.j a va 2 s  .c o m*/
    output.writeInt(data.length);
    output.write(data);
}

From source file:br.org.indt.ndg.servlets.PostResultsOpenRosa.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    m_openRosaBD.setPortAndAddress(SystemProperties.getServerAddress());

    InputStreamReader inputStreamReader = new InputStreamReader(request.getInputStream(), "UTF-8");
    boolean success = m_openRosaBD.parseAndPersistResult(inputStreamReader, request.getContentType());

    DataOutputStream dataOutputStream = new DataOutputStream(response.getOutputStream());
    if (success) {
        dataOutputStream.writeInt(SUCCESS);
        log.info("Successfully processed result stream from " + request.getRemoteAddr());
    } else {//from  w  w w  .  ja  v a  2s  .c om
        dataOutputStream.writeInt(FAILURE);
        log.error("Failed processing result stream from " + request.getRemoteAddr());
    }
    dataOutputStream.close();
}

From source file:org.apache.hadoop.hive.serde2.lazy.LazyUtils.java

/**
 * Write out a binary representation of a PrimitiveObject to a byte stream.
 *
 * @param out ByteStream.Output, an unsynchronized version of ByteArrayOutputStream, used as a
 *            backing buffer for the the DataOutputStream
 * @param o the PrimitiveObject//from  ww  w .ja va 2s  .  c o  m
 * @param oi the PrimitiveObjectInspector
 * @throws IOException on error during the write operation
 */
public static void writePrimitive(OutputStream out, Object o, PrimitiveObjectInspector oi) throws IOException {

    DataOutputStream dos = new DataOutputStream(out);

    try {
        switch (oi.getPrimitiveCategory()) {
        case BOOLEAN:
            boolean b = ((BooleanObjectInspector) oi).get(o);
            dos.writeBoolean(b);
            break;

        case BYTE:
            byte bt = ((ByteObjectInspector) oi).get(o);
            dos.writeByte(bt);
            break;

        case SHORT:
            short s = ((ShortObjectInspector) oi).get(o);
            dos.writeShort(s);
            break;

        case INT:
            int i = ((IntObjectInspector) oi).get(o);
            dos.writeInt(i);
            break;

        case LONG:
            long l = ((LongObjectInspector) oi).get(o);
            dos.writeLong(l);
            break;

        case FLOAT:
            float f = ((FloatObjectInspector) oi).get(o);
            dos.writeFloat(f);
            break;

        case DOUBLE:
            double d = ((DoubleObjectInspector) oi).get(o);
            dos.writeDouble(d);
            break;

        case BINARY: {
            BytesWritable bw = ((BinaryObjectInspector) oi).getPrimitiveWritableObject(o);
            out.write(bw.getBytes(), 0, bw.getLength());
            break;
        }

        default:
            throw new RuntimeException("Hive internal error.");
        }
    } finally {
        // closing the underlying ByteStream should have no effect, the data should still be
        // accessible
        dos.close();
    }
}

From source file:org.sakaiproject.search.journal.impl.SegmentListWriter.java

public void write(List<File> segments) throws IOException {

    if (!out.exists() && !out.getParentFile().exists()) {
        if (!out.getParentFile().mkdirs()) {
            throw new IOException("Can't create folder " + out.getParentFile().getPath());
        }/*w  w w . java  2  s  . com*/
    }
    FileOutputStream fout = new FileOutputStream(out);
    DataOutputStream dout = new DataOutputStream(fout);
    dout.write(SegmentListStore.SEGMENT_LIST_SIGNATURE);
    dout.writeInt(SegmentListStore.VERSION_SIGNATURE);
    dout.writeInt(segments.size());
    StringBuilder sb = new StringBuilder();
    sb.append(segments.size()).append(" Segments \n");
    for (File segs : segments) {
        String s = segs.getAbsolutePath();
        sb.append("\t").append(s).append("\n");
        dout.writeUTF(s);
    }
    dout.close();
    fout.close();
    if (log.isDebugEnabled())
        log.debug("Saved: " + sb.toString());
}

From source file:com.microsoft.tfs.client.eclipse.resourcedata.ResourceData.java

/**
 * @return this object serialized to a byte array, or <code>null</code> if
 *         there was a problem serializing
 *///from w  ww  .  ja  v  a 2  s.co m
public byte[] toByteArray() {
    try {
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        final DataOutputStream dos = new DataOutputStream(os);

        dos.writeUTF(serverItem);
        dos.writeInt(changesetId);

        dos.close();
        return os.toByteArray();
    } catch (final IOException e) {
        log.error("Error serializing", e); //$NON-NLS-1$
        return null;
    }
}

From source file:com.nokia.dempsy.messagetransport.tcp.TcpSender.java

@Override
public synchronized void send(byte[] messageBytes) throws MessageTransportException {
    try {/*www .  j  a  va  2s.c  o  m*/
        DataOutputStream localDataOutputStream = getDataOutputStream();

        localDataOutputStream.writeInt(messageBytes.length);
        localDataOutputStream.flush();
        localDataOutputStream.write(messageBytes);
        localDataOutputStream.flush();
    } catch (IOException ioe) {
        close();
        throw new MessageTransportException(
                "It appears the client " + destination + " is no longer taking calls.", ioe);
    }
}

From source file:org.apache.hadoop.hdfs.server.namenode.FSImageSerialization.java

static void writeINodeUnderConstruction(DataOutputStream out, INodeFileUnderConstruction cons, String path)
        throws IOException {
    writeString(path, out);/*www . j ava  2  s. c  o  m*/
    out.writeLong(cons.getId());
    out.writeShort(cons.getFileReplication());
    out.writeLong(cons.getModificationTime());
    out.writeLong(cons.getPreferredBlockSize());

    writeBlocks(cons.getBlocks(), out);
    cons.getPermissionStatus().write(out);

    writeString(cons.getClientName(), out);
    writeString(cons.getClientMachine(), out);

    out.writeInt(0); //  do not store locations of last block
}

From source file:org.openbaton.nfvo.core.api.KeyManagement.java

private String encodePublicKey(PublicKey publicKey, String user) throws IOException {
    String publicKeyEncoded;//from  w  w w . j a  va2s.  co m
    RSAPublicKey rsaPublicKey = (RSAPublicKey) publicKey;
    ByteArrayOutputStream byteOs = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(byteOs);
    dos.writeInt("ssh-rsa".getBytes().length);
    dos.write("ssh-rsa".getBytes());
    dos.writeInt(rsaPublicKey.getPublicExponent().toByteArray().length);
    dos.write(rsaPublicKey.getPublicExponent().toByteArray());
    dos.writeInt(rsaPublicKey.getModulus().toByteArray().length);
    dos.write(rsaPublicKey.getModulus().toByteArray());
    publicKeyEncoded = new String(encodeBase64(byteOs.toByteArray()));
    return "ssh-rsa " + publicKeyEncoded + " " + user;
}

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

public void serialize(ReadResponse rm, DataOutputStream dos) throws IOException {
    dos.writeUTF(rm.table());//w w  w. j a v  a2 s. c  o m
    dos.writeInt(rm.digest().length);
    dos.write(rm.digest());
    dos.writeBoolean(rm.isDigestQuery());

    if (!rm.isDigestQuery() && rm.row() != null) {
        Row.serializer().serialize(rm.row(), dos);
    }
}

From source file:org.openmrs.module.odkconnector.serialization.serializer.openmrs.ObsSerializer.java

/**
 * Write the data to the output stream./*from w  ww  . j a  va 2  s.  com*/
 *
 * @param stream the output stream
 * @param data   the data that need to be written to the output stream
 * @throws java.io.IOException thrown when the writing process encounter is failing
 */
@Override
public void write(final OutputStream stream, final Object data) throws IOException {
    try {
        Obs obs = (Obs) data;

        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
        DataOutputStream outputStream = new DataOutputStream(stream);
        // write the person id of the observation
        outputStream.writeInt(obs.getPersonId());
        // write the concept name of the observation
        outputStream.writeUTF(obs.getConcept().getDisplayString());
        // write the data type and the value of the observation
        if (obs.getValueDatetime() != null) {
            outputStream.writeByte(TYPE_DATE);
            outputStream.writeUTF(dateFormat.format(obs.getValueDatetime()));
        } else if (obs.getValueCoded() != null) {
            outputStream.writeByte(TYPE_STRING);
            outputStream.writeUTF(obs.getValueCoded().getDisplayString());
        } else if (obs.getValueNumeric() != null) {
            outputStream.writeByte(TYPE_DOUBLE);
            outputStream.writeDouble(obs.getValueNumeric());
        } else {
            outputStream.writeByte(TYPE_STRING);
            outputStream.writeUTF(obs.getValueAsString(Context.getLocale()));
        }
        // write the datetime of the observation
        outputStream.writeUTF(dateFormat.format(obs.getObsDatetime()));
    } catch (IOException e) {
        log.info("Writing obs information failed!", e);
    }
}