Example usage for java.io DataOutputStream DataOutputStream

List of usage examples for java.io DataOutputStream DataOutputStream

Introduction

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

Prototype

public DataOutputStream(OutputStream out) 

Source Link

Document

Creates a new data output stream to write data to the specified underlying output stream.

Usage

From source file:tachyon.master.Image.java

/**
 * Write a new image to path. This method assumes having a lock on the master info.
 * //from  w w  w .j a v a 2  s .  c  o  m
 * @param info the master info to generate the image
 * @param path the new image path
 * @throws IOException
 */
public static void create(MasterInfo info, String path) throws IOException {
    String tPath = path + ".tmp";
    String parentFolder = path.substring(0, path.lastIndexOf(TachyonURI.SEPARATOR));
    LOG.info("Creating the image file: " + tPath);
    UnderFileSystem ufs = UnderFileSystem.get(path, info.getTachyonConf());
    if (!ufs.exists(parentFolder)) {
        LOG.info("Creating parent folder " + parentFolder);
        ufs.mkdirs(parentFolder, true);
    }
    OutputStream os = ufs.create(tPath);
    DataOutputStream imageOs = new DataOutputStream(os);
    ObjectWriter writer = JsonObject.createObjectMapper().writer();

    info.writeImage(writer, imageOs);
    imageOs.flush();
    imageOs.close();

    LOG.info("Succefully created the image file: " + tPath);
    ufs.delete(path, false);
    ufs.rename(tPath, path);
    ufs.delete(tPath, false);
    LOG.info("Renamed " + tPath + " to " + path);
    // safe to close, nothing created here with scope outside function
    ufs.close();
}

From source file:CounterApp.java

public int getCount() throws Exception {
    java.net.URL url = new java.net.URL(servletURL);
    java.net.URLConnection con = url.openConnection();
    if (sessionCookie != null) {
        con.setRequestProperty("cookie", sessionCookie);
    }// w  ww. j av  a 2 s  . c om
    con.setUseCaches(false);
    con.setDoOutput(true);
    con.setDoInput(true);
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(byteOut);
    out.flush();
    byte buf[] = byteOut.toByteArray();
    con.setRequestProperty("Content-type", "application/octet-stream");
    con.setRequestProperty("Content-length", "" + buf.length);
    DataOutputStream dataOut = new DataOutputStream(con.getOutputStream());
    dataOut.write(buf);
    dataOut.flush();
    dataOut.close();
    DataInputStream in = new DataInputStream(con.getInputStream());
    int count = in.readInt();
    in.close();
    if (sessionCookie == null) {
        String cookie = con.getHeaderField("set-cookie");
        if (cookie != null) {
            sessionCookie = parseCookie(cookie);
            System.out.println("Setting session ID=" + sessionCookie);
        }
    }

    return count;
}

From source file:ImageTransfer.java

public void javaToNative(Object object, TransferData transferData) {
    if (!checkImage(object) || !isSupportedType(transferData)) {
        DND.error(DND.ERROR_INVALID_DATA);
    }//  w w  w .j  av a2 s .co  m
    ImageData imdata = (ImageData) object;
    try {
        // write data to a byte array and then ask super to convert to
        // pMedium
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        DataOutputStream writeOut = new DataOutputStream(out);
        ImageLoader loader = new ImageLoader();
        loader.data = new ImageData[] { imdata };
        loader.save(writeOut, SWT.IMAGE_BMP);
        writeOut.close();
        byte[] buffer = out.toByteArray();
        super.javaToNative(buffer, transferData);
        out.close();
    } catch (IOException e) {
    }
}

From source file:org.jfree.chart.demo.SocketThread.java

@Override
public void run() {
    // TODO Auto-generated method stub
    try {/*from  w w w.  j ava 2  s  . co m*/
        ServerSocket ss = new ServerSocket(4444);
        Socket socket = null;
        while (Islive) {
            System.out.println("Started :) ");
            socket = ss.accept(); /*  */
            System.out.println("Got a client :) ");

            InputStream sin = socket.getInputStream();
            OutputStream sout = socket.getOutputStream();

            in = new DataInputStream(sin);
            out = new DataOutputStream(sout);

            String line = null;

            while (true) {
                line = in.readUTF(); /*    */
                System.out.println("line start sending  = " + line);
                out.writeUTF(data);
                out.flush();
                line = in.readUTF();
                System.out.println(line);
                if (line == "finish")
                    break;
                else {
                    out.writeUTF("ok");
                    out.flush();
                }
            }
        }
        out.writeUTF("finish");
        out.flush();

        in.close();
        out.close();
        socket.close();
    } catch (Exception x) {

        errors += x.toString() + " ;\n";
        error_flag = true;
    }

}

From source file:io.lavagna.model.ProjectMetadata.java

private static String hash(SortedMap<Integer, CardLabel> labels,
        SortedMap<Integer, LabelListValueWithMetadata> labelListValues,
        Map<ColumnDefinition, BoardColumnDefinition> columnsDefinition) {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream daos = new DataOutputStream(baos);

    try {/*from w  w  w .j a va2  s.  com*/

        for (CardLabel cl : labels.values()) {
            hash(daos, cl);
        }

        for (LabelListValueWithMetadata l : labelListValues.values()) {
            hash(daos, l);
        }

        for (BoardColumnDefinition b : columnsDefinition.values()) {
            hash(daos, b);
        }

        daos.flush();
        return DigestUtils.sha256Hex(new ByteArrayInputStream(baos.toByteArray()));
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}

From source file:info.magnolia.cms.exchange.simple.Transporter.java

/**
 * http form multipart form post/*from  w ww .j  a  v a 2 s  . c  om*/
 * @param connection
 * @param activationContent
 * @throws ExchangeException
 */
public static void transport(URLConnection connection, ActivationContent activationContent)
        throws ExchangeException {
    FileInputStream fis = null;
    DataOutputStream outStream = null;
    try {
        byte[] buffer = new byte[BUFFER_SIZE];
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setUseCaches(false);
        connection.setRequestProperty("Content-type", "multipart/form-data; boundary=" + BOUNDARY);
        connection.setRequestProperty("Cache-Control", "no-cache");

        outStream = new DataOutputStream(connection.getOutputStream());
        outStream.writeBytes("--" + BOUNDARY + "\r\n");

        // set all resources from activationContent
        Iterator fileNameIterator = activationContent.getFiles().keySet().iterator();
        while (fileNameIterator.hasNext()) {
            String fileName = (String) fileNameIterator.next();
            fis = new FileInputStream(activationContent.getFile(fileName));
            outStream.writeBytes("content-disposition: form-data; name=\"" + fileName + "\"; filename=\""
                    + fileName + "\"\r\n");
            outStream.writeBytes("content-type: application/octet-stream" + "\r\n\r\n");
            while (true) {
                synchronized (buffer) {
                    int amountRead = fis.read(buffer);
                    if (amountRead == -1) {
                        break;
                    }
                    outStream.write(buffer, 0, amountRead);
                }
            }
            fis.close();
            outStream.writeBytes("\r\n" + "--" + BOUNDARY + "\r\n");
        }
        outStream.flush();
        outStream.close();

        log.debug("Activation content sent as multipart/form-data");
    } catch (Exception e) {
        throw new ExchangeException(
                "Simple exchange transport failed: " + ClassUtils.getShortClassName(e.getClass()), e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                log.error("Exception caught", e);
            }
        }
        if (outStream != null) {
            try {
                outStream.close();
            } catch (IOException e) {
                log.error("Exception caught", e);
            }
        }
    }

}

From source file:jp.xii.relog.customlibrary.DoSuCommand.java

public boolean init() {
    boolean ret = false;

    try {/*from w  w w . j  a  va 2 s  .c  o  m*/
        //su
        _process = Runtime.getRuntime().exec("su");

        //?
        _outputStream = new DataOutputStream(_process.getOutputStream());
        _inputStream = new DataInputStream(_process.getInputStream());

        //???
        ret = true;
        //         if(!write("su -v\n")){
        //         }else{
        //            String[] results = read().split("\n");
        //            for (String line : results) {
        //               if(line.length() > 0){
        //                  //???????
        //                  ret = true;
        //               }
        //            }
        //         }
    } catch (IOException e) {
    }

    return ret;
}

From source file:com.scaleunlimited.classify.vectors.WritableComparableVectorTest.java

@Test
public void testToFromStream() throws Exception {
    WritableComparableVector vector1 = new WritableComparableVector(makeVector());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    vector1.write(dos);//from   w  ww  .j  ava2  s.c om
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    DataInputStream dis = new DataInputStream(bais);
    WritableComparableVector vector2 = new WritableComparableVector(null);
    vector2.readFields(dis);
    compareVectors(vector1.getVector(), vector2.getVector());
}

From source file:com.hippo.httpclient.JsonPoster.java

@Override
public void onOutput(HttpURLConnection conn) throws Exception {
    super.onOutput(conn);

    DataOutputStream out = new DataOutputStream(conn.getOutputStream());
    out.write(mJSON.toString().getBytes("utf-8"));
    out.flush();/*from   ww  w .  j a va2  s  .  c o m*/
    out.close();
}

From source file:IOUtilities.java

/**
 * Returns a DataOutputStream object based on the given OutputStream.
 * If the given OutputStream is already an instance of DataOutputStream,
 * the same (given) OutputStream is casted to DataOutputStream and returned,
 * otherwise, a new wrapper DataOutputStream is created and returned.
 *///  w w w.  j  av a 2  s  . c o  m

public static DataOutputStream maybeCreateDataOutputStream(OutputStream out) {
    if (out instanceof DataOutputStream)
        return (DataOutputStream) out;
    else
        return new DataOutputStream(out);
}