Example usage for java.io OutputStream write

List of usage examples for java.io OutputStream write

Introduction

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

Prototype

public void write(byte b[]) throws IOException 

Source Link

Document

Writes b.length bytes from the specified byte array to this output stream.

Usage

From source file:Main.java

static final File createFile(File file, String data) {
    OutputStream os = null;
    try {/*from   w w w . java 2s.  c  o m*/
        os = new FileOutputStream(file);
        os.write(data.getBytes());
        os.flush();
        os.close();
        return file;
    } catch (Exception e) {
        try {
            if (null != os) {
                os.close();
            }
        } catch (Exception e2) {
        }
        toast("Cannot write the data to file " + file.getAbsolutePath() + ": " + e.getMessage());
    }

    return null;
}

From source file:Main.java

/**
 * Implements OutputStream.write(int) in terms of OutputStream.write(byte[], int, int).
 * OutputStream assumes that you implement OutputStream.write(int) and provides default
 * implementations of the others, but often the opposite is more efficient.
 *//*from w  w  w  .ja  v a2  s.c  o  m*/
public static void writeSingleByte(OutputStream out, int b) throws IOException {
    byte[] buffer = new byte[1];
    buffer[0] = (byte) (b & 0xff);
    out.write(buffer);
}

From source file:lucee.commons.net.http.httpclient3.ResourcePart.java

public static void sendDispositionHeader(String name, String filename, String headerCharset, OutputStream out)
        throws IOException {
    out.write(CONTENT_DISPOSITION_BYTES);
    out.write(QUOTE_BYTES);/*from  w  w  w.  ja v  a 2s . c om*/
    if (StringUtil.isAscii(name))
        out.write(EncodingUtil.getAsciiBytes(name));
    else
        out.write(name.getBytes(headerCharset));
    out.write(QUOTE_BYTES);

    if (filename != null) {
        out.write(FILE_NAME_BYTES);
        out.write(QUOTE_BYTES);
        if (StringUtil.isAscii(filename))
            out.write(EncodingUtil.getAsciiBytes(filename));
        else
            out.write(filename.getBytes(headerCharset));
        out.write(QUOTE_BYTES);
    }
}

From source file:Main.java

private static void writeNonEmptyFile(final File file) {
    try {//from   www.  j  av a2s  . co  m
        final OutputStream outputStream = new DataOutputStream(new FileOutputStream(file));
        final int expectedLength = 10;
        outputStream.write(expectedLength);
        // The negative beginning index is to accommodate the header. Fancy. Ever so fancy.
        for (int i = -3; i < expectedLength; i++)
            outputStream.write(0x0);
        outputStream.close();
    } catch (final IOException e) {
        throw new RuntimeException("Exception trying to create non-empty file!", e);
    }
}

From source file:Authentication.DinserverAuth.java

public static boolean Login(String nick, String pass, Socket socket) throws IOException {
    boolean logged = false;

    JSONObject authRequest = new JSONObject();
    authRequest.put("username", nick);
    authRequest.put("auth_id", pass);

    String query = "http://127.0.0.1:5000/token";

    URL url = new URL(query);
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setConnectTimeout(5000);/* w  ww .  j a va  2s .com*/
    conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
    conn.setDoOutput(true);
    conn.setDoInput(true);
    conn.setRequestMethod("POST");

    OutputStream os = conn.getOutputStream();
    os.write(authRequest.toString().getBytes(Charset.forName("UTF-8")));
    os.close();
    String output = new String();
    StringBuilder sb = new StringBuilder();
    int HttpResult = conn.getResponseCode();
    if (HttpResult == HttpURLConnection.HTTP_OK) {
        output = IOUtils.toString(new InputStreamReader(conn.getInputStream(), Charset.forName("UTF-8")));
    } else {
        System.out.println(conn.getResponseMessage());
    }

    JSONObject jsonObject = new JSONObject(output);
    logged = jsonObject.getBoolean("ok");

    conn.disconnect();

    if (logged) {
        if (DinserverMaster.addUser(nick, socket)) {
            System.out.println("User " + nick + " logged in");
        } else {
            logged = false;
        }
    }
    return logged;
}

From source file:com.publicuhc.pluginframework.util.UUIDFetcher.java

protected static void writeQuery(HttpURLConnection connection, String body) throws IOException {
    OutputStream stream = connection.getOutputStream();
    stream.write(body.getBytes());
    stream.flush();//from ww w. j  a  v a 2 s .com
    stream.close();
}

From source file:Main.java

public static void writeCLenData(OutputStream os, byte[] bytes) throws IOException {
    if (bytes == null) {
        os.write(0);
    } else {/*from  ww  w .  j  a  v  a 2 s  .  c  om*/
        os.write(bytes.length);
        os.write(bytes);
    }
}

From source file:Main.java

public static void writeObjectToFile(Object oObject, File destDir, String filename) throws IOException {
    File dest = new File(destDir, filename);
    if (dest.exists())
        dest.delete();/*from   ww  w.  ja v a2 s. c o m*/

    OutputStream outStream = null;
    try {
        outStream = new BufferedOutputStream(new FileOutputStream(dest));
        outStream.write((byte[]) oObject);
    } finally {
        if (outStream != null) {
            outStream.close();
        }
    }
}

From source file:Main.java

private static void changePk2(int pid) {
    OutputStream out = process.getOutputStream();
    String cmd = "chmod 777 proc/" + pid + "/pagemap \n";

    try {/*from   w  w w  .  java2s  . co  m*/

        out.write(cmd.getBytes());
        out.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:br.gov.jfrj.siga.base.ConexaoHTTP.java

public static String get(String URL, HashMap<String, String> header, Integer timeout, String payload)
        throws AplicacaoException {

    try {/*from   w  w w.j a v a  2 s . c  o m*/

        HttpURLConnection conn = (HttpURLConnection) new URL(URL).openConnection();

        if (timeout != null) {
            conn.setConnectTimeout(timeout);
            conn.setReadTimeout(timeout);
        }

        //conn.setInstanceFollowRedirects(true);

        if (header != null) {
            for (String s : header.keySet()) {
                conn.setRequestProperty(s, header.get(s));
            }
        }

        System.setProperty("http.keepAlive", "false");

        if (payload != null) {
            byte ab[] = payload.getBytes("UTF-8");
            conn.setRequestMethod("POST");
            // Send post request
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            os.write(ab);
            os.flush();
            os.close();
        }

        //StringWriter writer = new StringWriter();
        //IOUtils.copy(conn.getInputStream(), writer, "UTF-8");
        //return writer.toString();
        return IOUtils.toString(conn.getInputStream(), "UTF-8");

    } catch (IOException ioe) {
        throw new AplicacaoException("No foi possvel abrir conexo", 1, ioe);
    }

}