Example usage for java.io DataOutputStream flush

List of usage examples for java.io DataOutputStream flush

Introduction

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

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this data output stream.

Usage

From source file:Main.java

private static byte[] ExecuteCommand(String command, Boolean useroot, boolean forcenew) throws Exception {
    Process p;//from w  ww.  java 2 s  .co  m
    DataOutputStream stdin;
    InputStream stdout;
    ByteArrayOutputStream baos;
    int read;
    byte[] buffer;
    /*
     * If for any reason the command does not print anything we are stuck forever.
     * Make sure that we print SOMETHING ALWAYS!
     */
    command = "RESULT=$(" + command + "); if [[ $RESULT == '' ]]; then echo '#null#';else echo $RESULT;fi\n";

    p = getProcess(useroot, forcenew);

    stdin = new DataOutputStream(p.getOutputStream());
    stdout = p.getInputStream();
    buffer = new byte[BUFF_LEN];
    baos = new ByteArrayOutputStream();

    stdin.writeBytes(command);

    while (true) {
        read = stdout.read(buffer);
        baos.write(buffer, 0, read);
        if (read < BUFF_LEN) {
            //we have read everything
            break;
        }
    }
    if (forcenew) {
        stdin.writeBytes("exit\n");
        stdin.flush();
        stdin.close();
    }

    //p.waitFor();

    return baos.toByteArray();
}

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);
    }/*from w  w  w .  j  a v  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:CounterServer.java

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession(true);
    int count = 1;
    Integer i = (Integer) session.getAttribute(COUNTER_KEY);
    if (i != null) {
        count = i.intValue() + 5;/*from   ww  w  . j  a  va2 s.com*/
    }
    session.setAttribute(COUNTER_KEY, new Integer(count));
    DataInputStream in = new DataInputStream(req.getInputStream());
    resp.setContentType("application/octet-stream");
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(byteOut);
    out.writeInt(count);
    out.flush();
    byte[] buf = byteOut.toByteArray();
    resp.setContentLength(buf.length);
    ServletOutputStream servletOut = resp.getOutputStream();
    servletOut.write(buf);
    servletOut.close();
}

From source file:Main.java

public static String uploadFile(File file, String RequestURL) {
    String BOUNDARY = UUID.randomUUID().toString();
    String PREFIX = "--", LINE_END = "\r\n";
    String CONTENT_TYPE = "multipart/form-data";

    try {/*w w w .jav  a 2 s  .c om*/
        URL url = new URL(RequestURL);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(TIME_OUT);
        conn.setConnectTimeout(TIME_OUT);
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Charset", CHARSET);
        conn.setRequestProperty("connection", "keep-alive");
        conn.setRequestProperty("Content-Type", CONTENT_TYPE + ";boundary=" + BOUNDARY);
        if (file != null) {

            OutputStream outputSteam = conn.getOutputStream();

            DataOutputStream dos = new DataOutputStream(outputSteam);
            StringBuffer sb = new StringBuffer();
            sb.append(PREFIX);
            sb.append(BOUNDARY);
            sb.append(LINE_END);

            sb.append("Content-Disposition: form-data; name=\"img\"; filename=\"" + file.getName() + "\""
                    + LINE_END);
            sb.append("Content-Type: application/octet-stream; charset=" + CHARSET + LINE_END);
            sb.append(LINE_END);
            dos.write(sb.toString().getBytes());
            InputStream is = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = is.read(bytes)) != -1) {
                dos.write(bytes, 0, len);
            }
            is.close();
            dos.write(LINE_END.getBytes());
            byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END).getBytes();
            dos.write(end_data);
            dos.flush();

            int res = conn.getResponseCode();
            Log.e(TAG, "response code:" + res);
            if (res == 200) {
                return SUCCESS;
            }
        }
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return FAILURE;
}

From source file:dualcontrol.CryptoClientDemo.java

private void call(Properties properties, MockableConsole console, String hostAddress, int port, byte[] data)
        throws Exception {
    Socket socket = SSLContexts.create(false, "cryptoclient.ssl", properties, console).getSocketFactory()
            .createSocket(hostAddress, port);
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    dos.writeShort(data.length);/*from   w  w  w  .ja va  2  s .co  m*/
    dos.write(data);
    dos.flush();
    DataInputStream dis = new DataInputStream(socket.getInputStream());
    byte[] ivBytes = new byte[dis.readShort()];
    dis.readFully(ivBytes);
    byte[] bytes = new byte[dis.readShort()];
    dis.readFully(bytes);
    if (new String(data).contains("DECRYPT")) {
        System.err.printf("INFO CryptoClientDemo decrypted %s\n", new String(bytes));
    } else {
        System.out.printf("%s:%s\n", Base64.encodeBase64String(ivBytes), Base64.encodeBase64String(bytes));
    }
    socket.close();
}

From source file:org.floggy.synchronization.jme.server.SynchronizationServletTest.java

/**
* DOCUMENT ME!/*from   ww w. j a va  2s.c  o  m*/
*
* @param synchronizableClass DOCUMENT ME!
*
* @return DOCUMENT ME!
*
* @throws Exception DOCUMENT ME!
*/
public byte[] getContent(Class synchronizableClass) throws Exception {
    SynchronizableMetadataManager.init();

    SynchronizableMetadata metadata = SynchronizableMetadataManager
            .getSynchronizableMetadata(synchronizableClass.getName());

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    DataOutputStream dos = new DataOutputStream(baos);

    dos.flush();
    dos.writeUTF(metadata.toJSON());

    return baos.toByteArray();
}

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();
    out.close();//w  ww.ja v  a2  s .c o  m
}

From source file:com.webarch.common.net.http.HttpService.java

/**
 * ?url/*from   w w w  . j  av a 2s .c o m*/
 *
 * @param inputStream ?
 * @param fileName    ??
 * @param fileType    
 * @param contentType 
 * @param identity    
 * @return 
 */
public static String uploadMediaFile(String requestUrl, FileInputStream inputStream, String fileName,
        String fileType, String contentType, String identity) {
    String lineEnd = System.getProperty("line.separator");
    String twoHyphens = "--";
    String boundary = "*****";
    String result = null;
    try {
        //?
        URL submit = new URL(requestUrl);
        HttpURLConnection conn = (HttpURLConnection) submit.openConnection();
        //
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        //?
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Connection", "Keep-Alive");
        conn.setRequestProperty("Charset", DEFAULT_CHARSET);
        conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
        //??
        DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
        dos.writeBytes(twoHyphens + boundary + lineEnd);
        dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + fileName
                + ";Content-Type=\"" + contentType + lineEnd);
        dos.writeBytes(lineEnd);

        byte[] buffer = new byte[8192]; // 8k
        int count = 0;
        while ((count = inputStream.read(buffer)) != -1) {
            dos.write(buffer, 0, count);
        }
        inputStream.close(); //?
        dos.writeBytes(lineEnd);
        dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
        dos.flush();

        InputStream is = conn.getInputStream();
        InputStreamReader isr = new InputStreamReader(is, DEFAULT_CHARSET);
        BufferedReader br = new BufferedReader(isr);
        result = br.readLine();
        dos.close();
        is.close();
    } catch (IOException e) {
        logger.error("", e);
    }
    return result;
}

From source file:com.paymaya.sdk.android.common.network.AndroidClient.java

private void write(OutputStream os, byte[] body) throws IOException {
    DataOutputStream out = new DataOutputStream(os);
    out.write(body);//from   w  ww  .ja  va 2s .  c  o  m
    out.flush();
    out.close();
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.bincas.BinaryCasWriter.java

private void writeHeader(OutputStream aOS) throws IOException {
    byte[] header = new byte[] { 'D', 'K', 'P', 'r', 'o', '1' };
    DataOutputStream dataOS = new DataOutputStream(aOS);
    dataOS.write(header);//from   ww w.j  ava 2s  .  c o  m
    dataOS.flush();
}