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.mobisocial.corral.CorralDownloadClient.java

private static String hashToString(long hash) {
    try {//from   w  ww  .  j a  v a  2  s . c o m
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        dos.writeLong(hash);
        dos.writeInt(-4);
        byte[] data = bos.toByteArray();
        return Base64.encodeToString(data, Base64.DEFAULT).substring(0, 11);
    } catch (IOException e) {
        return null;
    }
}

From source file:Messenger.TorLib.java

/**
 *  This method Creates a socket, then sends the inital SOCKS request info
 *  It stops before reading so that other methods may
 *  differently interpret the results.  It returns the open socket.
 *
 * @param targetHostname The hostname of the destination host.
 * @param targetPort The port to connect to
 * @param req SOCKS/TOR request code/*from  www . j av a 2s . com*/
 * @return An open Socket that has been sent the SOCK4a init codes.
 * @throws IOException from any Socket problems
 */
static Socket TorSocketPre(String targetHostname, int targetPort, byte req) throws IOException {

    Socket s;
    //      System.out.println("Opening connection to "+targetHostname+":"+targetPort+
    //            " via proxy "+proxyAddr+":"+proxyPort+" of type "+req);
    s = new Socket(proxyAddr, proxyPort);
    DataOutputStream os = new DataOutputStream(s.getOutputStream());
    os.writeByte(SOCKS_VERSION);
    os.writeByte(req);
    // 2 bytes
    os.writeShort(targetPort);
    // 4 bytes, high byte first
    os.writeInt(SOCKS4A_FAKEIP);
    os.writeByte(SOCKS_DELIM);
    os.writeBytes(targetHostname);
    os.writeByte(SOCKS_DELIM);
    return (s);
}

From source file:org.jax.haplotype.io.SnpStreamUtil.java

/**
 * Write the snp positions as a binary file using the given base directory
 * @param chromosome//from   w  w  w  . j  a va2  s.co m
 *          the chromosome to write
 * @throws IOException
 *          if the write operation fails
 */
private static void writeBinarySnpPositions(StrainChromosome chromosome, DataOutputStream dataOutputStream,
        StreamDirection streamDirection) throws IOException {
    SingleNucleotidePolymorphism[] snps = chromosome.getSingleNucleotidePolymorphisms();
    dataOutputStream.write(StreamDirection.streamDirectionToByte(streamDirection));
    dataOutputStream.writeInt(chromosome.getChromosomeNumber());
    dataOutputStream.writeLong(snps[0].getPositionInBasePairs());
    dataOutputStream
            .writeLong(1L + snps[snps.length - 1].getPositionInBasePairs() - snps[0].getPositionInBasePairs());
    dataOutputStream.writeLong(snps.length);
    if (streamDirection == StreamDirection.FORWARD) {
        for (int i = 0; i < snps.length; i++) {
            dataOutputStream.writeLong(snps[i].getPositionInBasePairs());
        }
    } else {
        assert streamDirection == StreamDirection.REVERSE;
        for (int i = snps.length - 1; i >= 0; i--) {
            dataOutputStream.writeLong(snps[i].getPositionInBasePairs());
        }
    }
}

From source file:uk.ac.ebi.mdk.io.ReactionMatrixIO.java

public static void writeCompressedBasicStoichiometricMatrix(StoichiometricMatrix<?, ?> s, OutputStream writer)
        throws IOException {

    DataOutputStream out = new DataOutputStream(new BufferedOutputStream(writer, 2048));

    int n = s.getMoleculeCount();
    int m = s.getReactionCount();

    out.writeInt(n);
    out.writeInt(m);/*from   w  w w  . ja v  a2 s. c  o  m*/

    for (int j = 0; j < m; j++) {
        out.writeUTF(s.getReaction(j).toString());
    }

    for (int i = 0; i < n; i++) {
        out.writeUTF(s.getMolecule(i).toString());
    }

    out.writeBoolean(convertDoubles);
    out.writeInt(s.getNonNullCount());

    for (int i = 0; i < n; i++) {

        for (int j = 0; j < m; j++) {

            // if the value is null
            if (convertDoubles) {
                int value = s.get(i, j).intValue();
                if (value != 0) {
                    out.writeInt(i);
                    out.writeInt(j);
                    out.writeInt(value);
                }
            } else {
                double value = s.get(i, j);
                if (value != 0d) {
                    out.writeInt(i);
                    out.writeInt(j);
                    out.writeDouble(value);
                }
            }

        }
    }

    out.close();
}

From source file:org.wso2.carbon.analytics.data.commons.utils.AnalyticsCommonUtils.java

/**
 * This method is used to generate an UUID from the target table name to make sure that it is a compact
 * name that can be fitted in all the supported RDBMSs. For example, Oracle has a table name
 * length of 30. So we must translate source table names to hashed strings, which here will have
 * a very low probability of clashing./*from  ww w .j  av a2s . co  m*/
 */
public static String generateTableUUID(String tableName) {
    try {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(byteOut);
        /* we've to limit it to 64 bits */
        dout.writeInt(tableName.hashCode());
        dout.close();
        byteOut.close();
        String result = Base64.getEncoder().encodeToString(byteOut.toByteArray());
        result = result.replace('=', '_');
        result = result.replace('+', '_');
        result = result.replace('/', '_');
        /* a table name must start with a letter */
        return ANALYTICS_USER_TABLE_PREFIX + result;
    } catch (IOException e) {
        /* this will never happen */
        throw new RuntimeException(e);
    }
}

From source file:uk.ac.ox.webauth.Token.java

/**
 * Convenience method for getting a byte array with a unix timestamp, i.e.
 * number of seconds since the epoch./*from   www . j ava  2 s.  com*/
 * @param   date    The date to base the timestamp on, such as System.currentTimeMillis().
 * @return  A unix time stamp in an integer saved in network byte order in
 *          a byte array.
 */
public static byte[] unixTimestampBytes(long date) {
    ByteArrayOutputStream arrayStream = new ByteArrayOutputStream();
    try {
        DataOutputStream dataStream = new DataOutputStream(arrayStream);
        dataStream.writeInt((int) (date / 1000L));
    } catch (IOException ioe) {
        /* should never happen as it's a ByteArrayOutputStream */
        ioe.printStackTrace();
    }
    return arrayStream.toByteArray();
}

From source file:org.hyperic.hq.measurement.agent.server.SenderThread.java

private static String encodeRecord(Record record) throws IOException {
    ByteArrayOutputStream bOs;/*from w w  w  . jav  a 2  s. co  m*/
    DataOutputStream dOs;

    bOs = new ByteArrayOutputStream();
    dOs = new DataOutputStream(bOs);

    dOs.writeInt(record.derivedID);
    dOs.writeLong(record.data.getTimestamp());
    dOs.writeInt(record.dsnId);
    dOs.writeDouble(record.data.getValue());
    return Base64.encode(bOs.toByteArray());
}

From source file:com.aliyun.odps.ogg.handler.datahub.DatahubHandler.java

public static void recordSendTimes(int times, HandlerProperties handlerProperties) {
    DataOutputStream out = null;
    OggAlarm oggAlarm = handlerProperties.getOggAlarm();
    try {/* ww  w.j  a  v a 2  s. co  m*/
        out = new DataOutputStream(new FileOutputStream(handlerProperties.getHandlerInfoFileName(), false));
        out.writeInt(handlerProperties.getSkipSendTimes());
    } catch (IOException e) {
        oggAlarm.error("Error writing handler info file. sendTimesInTx=" + times + ".", e);
        logger.error("Error writing handler info file. sendTimesInTx=" + times + ".", e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                oggAlarm.error("Close handler info file failed. sendTimesInTx=" + times + ".", e);
                logger.error("Close handler info file failed. sendTimesInTx=" + times + ".", e);
            }
        }
    }
}

From source file:org.wso2.carbon.analytics.datasource.core.util.GenericUtils.java

/**
 * This method is used to generate an UUID from the target table name, to make sure, it is a compact
 * name that can be fitted in all the supported RDBMSs. For example, Oracle has a table name
 * length of 30. So we must translate source table names to hashed strings, which here will have
 * a very low probability of clashing.//from w  w w  . jav a  2 s . c o m
 */
public static String generateTableUUID(int tenantId, String tableName) {
    try {
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(byteOut);
        dout.writeInt(tenantId);
        /* we've to limit it to 64 bits */
        dout.writeInt(tableName.hashCode());
        dout.close();
        byteOut.close();
        String result = Base64.encode(byteOut.toByteArray());
        result = result.replace('=', '_');
        result = result.replace('+', '_');
        result = result.replace('/', '_');
        /* a table name must start with a letter */
        return ANALYTICS_USER_TABLE_PREFIX + result;
    } catch (IOException e) {
        /* this will never happen */
        throw new RuntimeException(e);
    }
}

From source file:marytts.util.io.FileUtils.java

public static void writeBinaryFile(int[] x, DataOutputStream d) throws IOException {
    for (int i = 0; i < x.length; i++)
        d.writeInt(x[i]);
}