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.wso2.carbon.mediator.cache.digest.REQUESTHASHGenerator.java

/**
 * This is an overloaded method for the digest generation for OMElement and request
 *
 * @param element           - OMElement to be subjected to the key generation
 * @param toAddress         - Request To address to be subjected to the key generation
 * @param headers           - Header parameters to be subjected to the key generation
 * @param digestAlgorithm   - digest algorithm as a String
 * @return byte[] representing the calculated digest over the provided element
 * @throws CachingException if there is an io error or the specified algorithm is incorrect
 *///from ww  w  . j a v  a 2  s .  c  o  m
public byte[] getDigest(OMElement element, String toAddress, Map<String, String> headers,
        String digestAlgorithm) throws CachingException {

    byte[] digest = new byte[0];

    try {

        MessageDigest md = MessageDigest.getInstance(digestAlgorithm);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        dos.writeInt(1);
        dos.write(getExpandedName(element).getBytes("UnicodeBigUnmarked"));
        dos.write((byte) 0);
        dos.write((byte) 0);

        dos.write(toAddress.getBytes("UnicodeBigUnmarked"));

        /*String acceptHeader = headers.get("accept");
        acceptHeader = (acceptHeader == null) ? headers.get("Accept") : acceptHeader;
                
        if (acceptHeader != null) {
           dos.write(acceptHeader.getBytes("UnicodeBigUnmarked"));
        }
                
        String contentTypeHeader = headers.get("content-type");
        contentTypeHeader = (contentTypeHeader == null) ? headers.get("Content-Type") : contentTypeHeader;
                
        if (contentTypeHeader != null) {
           dos.write(contentTypeHeader.getBytes("UnicodeBigUnmarked"));
        }*/

        Iterator itr = headers.keySet().iterator();
        while (itr.hasNext()) {
            String key = (String) itr.next();
            String value = headers.get(key);
            dos.write(getDigest(key, value, digestAlgorithm));
        }

        Collection attrs = getAttributesWithoutNS(element);
        dos.writeInt(attrs.size());

        itr = attrs.iterator();
        while (itr.hasNext())
            dos.write(getDigest((OMAttribute) itr.next(), digestAlgorithm));
        OMNode node = element.getFirstOMChild();

        // adjoining Texts are merged,
        // there is  no 0-length Text, and
        // comment nodes are removed.
        int length = 0;
        itr = element.getChildElements();
        while (itr.hasNext()) {
            length++;
            itr.next();
        }
        dos.writeInt(length);

        while (node != null) {
            dos.write(getDigest(node, toAddress, headers, digestAlgorithm));
            node = node.getNextOMSibling();
        }
        dos.close();
        md.update(baos.toByteArray());

        digest = md.digest();

    } catch (NoSuchAlgorithmException e) {
        handleException(
                "Can not locate the algorithm " + "provided for the digest generation : " + digestAlgorithm, e);
    } catch (IOException e) {
        handleException("Error in calculating the " + "digest value for the OMElement : " + element, e);
    }

    return digest;
}

From source file:org.kalypso.shape.shp.SHPFile.java

private byte[] writeRecordAsBytes(final int recordNumber, final ISHPGeometry shape) throws IOException {
    final int contentLength = shape.length() + 4;
    final int recordLength = RECORD_HEADER_BYTES + contentLength;

    final ByteArrayOutputStream out = new ByteArrayOutputStream(recordLength);
    final DataOutputStream os = new DataOutputStream(out);

    /* Record Header */
    os.writeInt(recordNumber);
    os.writeInt(contentLength / 2);//from   w  w w. j a v a2  s  .  co  m

    /* record Content */
    DataUtils.writeLEInt(os, shape.getType().getType());
    shape.write(os);

    // TODO: why flush here?
    os.flush();

    final byte[] byteArray = out.toByteArray();

    Assert.isTrue(recordLength == byteArray.length);

    return byteArray;
}

From source file:org.apache.camel.converter.crypto.CryptoDataFormat.java

private void inlineInitVector(OutputStream outputStream, byte[] iv) throws IOException {
    if (inline) {
        DataOutputStream dout = new DataOutputStream(outputStream);
        dout.writeInt(iv.length);
        outputStream.write(iv);//from  ww  w  .j  ava2 s  . c  o m
        outputStream.flush();
    }
}

From source file:J2MESearchMixedRecordDataTypeExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);/*from  w  w  w.  j  av  a 2 s. c o  m*/
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
            byte[] outputRecord;
            String outputString[] = { "A", "B", "M" };
            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();
            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();
            recordstore.closeRecordStore();
            if (RecordStore.listRecordStores() != null) {
                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.serenegiant.media.TLMediaEncoder.java

/**
 * write frame header/*from w ww  .  j a  v a  2 s .  c o  m*/
 * @param presentation_time_us
 * @param size
 * @throws IOException
 */
/*package*/static void writeHeader(final DataOutputStream out, final int sequence, final int frame_number,
        final long presentation_time_us, final int size, final int flag) throws IOException {

    out.writeInt(sequence);
    out.writeInt(frame_number);
    out.writeLong(presentation_time_us);
    out.writeInt(size);
    out.writeInt(flag);
    //
    out.write(RESERVED, 0, 40);
}

From source file:org.apache.cassandra.db.SliceByNamesReadCommand.java

@Override
public void serialize(ReadCommand rm, DataOutputStream dos) throws IOException {
    SliceByNamesReadCommand realRM = (SliceByNamesReadCommand) rm;
    dos.writeBoolean(realRM.isDigestQuery());
    dos.writeUTF(realRM.table);/*www  . jav  a  2 s.  c  o  m*/
    dos.writeUTF(realRM.key);
    realRM.columnParent.serialize(dos);
    dos.writeInt(realRM.columnNames.size());
    if (realRM.columnNames.size() > 0) {
        for (byte[] cName : realRM.columnNames) {
            ColumnSerializer.writeName(cName, dos);
        }
    }
}

From source file:J2MESortMixedRecordDataTypeExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);/*from ww w  . j a  v a2s  .  c  om*/
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
            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();

            String[] inputString = new String[3];
            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();
            recordstore.closeRecordStore();
            if (RecordStore.listRecordStores() != null) {
                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.apache.tez.examples.TopK.java

private UserPayload createPayload(int num) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    dos.writeInt(num);
    dos.close();//from  w w  w.  j a  va 2 s .co m
    bos.close();
    ByteBuffer buffer = ByteBuffer.wrap(bos.toByteArray());
    return UserPayload.create(buffer);
}

From source file:org.teatrove.teaapps.contexts.EncodingContext.java

/**
 * Encode the given integer array into Base64 format.
 * /*from w w w.  j  a  v a 2s  .c o  m*/
 * @param input The array of integers to encode
 * 
 * @return The Base64 encoded data
 * 
 * @throws IOException if an error occurs encoding the array stream
 * 
 * @see #decodeIntArray(String)
 */
public String encodeIntArray(int[] input) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bos);
    int length = input.length;
    dos.writeInt(length);
    for (int i = 0; i < length; i++) {
        dos.writeInt(input[i]);
    }

    return new String(Base64.encodeBase64URLSafe(bos.toByteArray()));
}

From source file:com.bigdata.dastor.db.RowMutation.java

private void freezeTheMaps(Map<String, ColumnFamily> map, DataOutputStream dos) throws IOException {
    int size = map.size();
    dos.writeInt(size);
    if (size > 0) {
        Set<String> keys = map.keySet();
        for (String key : keys) {
            dos.writeUTF(key);//w ww.j a  v  a 2  s.  c  o m
            ColumnFamily cf = map.get(key);
            if (cf != null) {
                ColumnFamily.serializer().serialize(cf, dos);
            }
        }
    }
}