Example usage for java.io DataOutputStream write

List of usage examples for java.io DataOutputStream write

Introduction

In this page you can find the example usage for java.io DataOutputStream write.

Prototype

public synchronized void write(int b) throws IOException 

Source Link

Document

Writes the specified byte (the low eight bits of the argument b) to the underlying output stream.

Usage

From source file:com.dbay.apns4j.tools.ApnsTools.java

@Deprecated
public final static byte[] generateData(int id, int expire, byte[] token, byte[] payload) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    DataOutputStream os = new DataOutputStream(bos);
    try {//from www .j ava 2s . c  o m
        os.writeByte(Command.SEND);
        os.writeInt(id);
        os.writeInt(expire);
        os.writeShort(token.length);
        os.write(token);
        os.writeShort(payload.length);
        os.write(payload);
        os.flush();
        return bos.toByteArray();
    } catch (IOException e) {
        e.printStackTrace();
    }
    throw new RuntimeException();
}

From source file:edu.harvard.iq.dvn.unf.Base64Encoding.java

/**
 *
 * @param digest byte array/* w w w.ja v  a 2s.com*/
 * @param enc String with final encoding
 * @return String the encoded base64 of digest
 * @throws UnsupportedEncodingException
 */
public static String tobase64(byte[] digest, String enc) throws UnsupportedEncodingException {

    ByteArrayOutputStream btstream = new ByteArrayOutputStream();
    //this make sure is written in big-endian

    DataOutputStream stream = new DataOutputStream(btstream);

    byte[] tobase64 = null;
    byte[] revdigest = new byte[digest.length];
    revdigest = changeByteOrder(digest, ByteOrder.nativeOrder());
    try {

        stream.write(revdigest);
        stream.flush();

        tobase64 = Base64.encodeBase64(btstream.toByteArray());

    } catch (IOException io) {
        tobase64 = Base64.encodeBase64(digest);
    }

    return new String(tobase64, enc);
}

From source file:org.apache.flink.runtime.metrics.dump.MetricDumpSerialization.java

private static void serializeString(DataOutputStream dos, String string) throws IOException {
    byte[] bytes = string.getBytes();
    dos.writeInt(bytes.length);//from www.  ja  v  a  2s .  com
    dos.write(bytes);
}

From source file:com.iflytek.android.framework.volley.toolbox.HurlStack.java

private static void addBodyIfExists(HttpURLConnection connection, Request<?> request)
        throws IOException, AuthFailureError {
    byte[] body = request.getBody();
    if (body != null) {
        connection.setDoOutput(true);//from  w  w  w  . jav  a2s  . c  o  m
        VolleyLog.e("======3:" + request.getBodyContentType());
        connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
        DataOutputStream out = new DataOutputStream(connection.getOutputStream());
        out.write(body);
        out.close();
    }
}

From source file:com.inmobi.conduit.distcp.tools.mapred.TestUniformSizeInputFormat.java

private static int createFile(String path, int fileSize) throws Exception {
    FileSystem fileSystem = null;
    DataOutputStream outputStream = null;
    try {/*  w w  w.j a  v a 2 s  .  c om*/
        fileSystem = cluster.getFileSystem();
        outputStream = fileSystem.create(new Path(path), true, 0);
        int size = (int) Math.ceil(fileSize + (1 - random.nextFloat()) * fileSize);
        outputStream.write(new byte[size]);
        return size;
    } finally {
        IOUtils.cleanup(null, fileSystem, outputStream);
    }
}

From source file:com.android.volley.toolbox.http.HurlStack.java

private static void addBodyIfExists(HttpURLConnection connection, Request<?> request)
        throws IOException, AuthFailureError {
    if (request.containsFile()) {
        setConnectionParametersForMultipartRequest(connection, request);
    } else {/* w  ww. ja  v  a 2  s. c  om*/
        byte[] body = request.getBody();
        if (body != null) {
            connection.setDoOutput(true);
            connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getBodyContentType());
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(body);
            out.close();
        }
    }
}

From source file:com.iflytek.android.framework.volley.toolbox.HurlStack.java

@SuppressWarnings("deprecation")
/* package */static void setConnectionParametersForRequest(HttpURLConnection connection, Request<?> request)
        throws IOException, AuthFailureError {
    VolleyLog.e("======setConnectionParametersForRequest:");
    switch (request.getMethod()) {
    case Method.DEPRECATED_GET_OR_POST:
        // This is the deprecated way that needs to be handled for backwards
        // compatibility.
        // If the request's post body is null, then the assumption is that
        // the request is
        // GET. Otherwise, it is assumed that the request is a POST.
        byte[] postBody = request.getPostBody();
        if (postBody != null) {
            // Prepare output. There is no need to set Content-Length
            // explicitly,
            // since this is handled by HttpURLConnection using the size of
            // the prepared
            // output stream.
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.addRequestProperty(HEADER_CONTENT_TYPE, request.getPostBodyContentType());
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            out.write(postBody);
            out.close();//ww w .j  a  va 2s  .  c  o  m
        }
        break;
    case Method.GET:
        // Not necessary to set the request method because connection
        // defaults to GET but
        // being explicit here.
        connection.setRequestMethod("GET");
        break;
    case Method.DELETE:
        connection.setRequestMethod("DELETE");
        break;
    case Method.POST:

        connection.setRequestMethod("POST");
        addBodyIfExists(connection, request);
        break;
    case Method.PUT:
        connection.setRequestMethod("PUT");
        addBodyIfExists(connection, request);
        break;
    case Method.HEAD:
        connection.setRequestMethod("HEAD");
        break;
    case Method.OPTIONS:
        connection.setRequestMethod("OPTIONS");
        break;
    case Method.TRACE:
        connection.setRequestMethod("TRACE");
        break;
    case Method.PATCH:
        connection.setRequestMethod("PATCH");
        addBodyIfExists(connection, request);
        break;
    default:
        throw new IllegalStateException("Unknown method type.");
    }
}

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  va  2 s. c  o 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:org.eclipse.swt.snippets.Snippet319.java

static byte[] convertToByteArray(MyType type) {
    DataOutputStream dataOutStream = null;
    try {//from   w ww . java2 s  .co  m
        ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream();
        dataOutStream = new DataOutputStream(byteOutStream);
        byte[] bytes = type.name.getBytes();
        dataOutStream.writeInt(bytes.length);
        dataOutStream.write(bytes);
        dataOutStream.writeLong(type.time);
        return byteOutStream.toByteArray();
    } catch (IOException e) {
        return null;
    } finally {
        if (dataOutStream != null) {
            try {
                dataOutStream.close();
            } catch (IOException e) {
            }
        }
    }
}

From source file:net.timewalker.ffmq4.storage.data.impl.BlockBasedDataStoreTools.java

private static void initAllocationTable(File atFile, int blockCount, int blockSize, boolean forceSync)
        throws DataStoreException {
    log.debug("Creating allocation table (size=" + blockCount + ") ...");

    // Create the file
    try {// ww  w .  j  a  va  2  s  . c om
        FileOutputStream outFile = new FileOutputStream(atFile);
        DataOutputStream out = new DataOutputStream(new BufferedOutputStream(outFile));

        out.writeInt(blockCount); // Block count
        out.writeInt(blockSize); // Block size
        out.writeInt(-1); // First block index
        for (int n = 0; n < blockCount; n++)
            out.write(EMPTY_BLOCK);
        out.flush();
        if (forceSync)
            outFile.getFD().sync();

        out.close();
    } catch (IOException e) {
        throw new DataStoreException("Cannot initialize allocation table " + atFile.getAbsolutePath(), e);
    }
}