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:com.onedrive.api.internal.MultipartRelatedHttpMessageConverter.java

private static void writeNewLine(OutputStream os) throws IOException {
    os.write('\r');
    os.write('\n');
}

From source file:com.wx.kernel.util.HttpKit.java

/**
 * Send POST request/*from w  w  w .  j a va  2  s.  co  m*/
 */
public static String post(String url, Map<String, String> queryParas, String data,
        Map<String, String> headers) {
    HttpURLConnection conn = null;
    try {
        conn = getHttpConnection(buildUrlWithQueryString(url, queryParas), POST, headers);
        conn.connect();

        OutputStream out = conn.getOutputStream();
        out.write(data.getBytes(CHARSET));
        out.flush();
        out.close();

        return readResponseString(conn);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (conn != null) {
            conn.disconnect();
        }
    }
}

From source file:net.cbtltd.server.WebService.java

/**
 * Gets the connection to the JSON server.
 *
 * @param url the connection URL.//from w w w.j  a v  a2s  . co  m
 * @param rq the request object.
 * @return the JSON string returned by the message.
 * @throws Throwable the exception thrown by the method.
 */
private static final String getConnection(URL url, String rq) throws Throwable {
    String jsonString = "";
    HttpURLConnection connection = null;
    try {
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        //connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/json");

        if (rq != null) {
            connection.setRequestProperty("Accept", "application/json");
            connection.connect();
            byte[] outputBytes = rq.getBytes("UTF-8");
            OutputStream os = connection.getOutputStream();
            os.write(outputBytes);
        }

        if (connection.getResponseCode() != 200) {
            throw new RuntimeException("HTTP:" + connection.getResponseCode() + "URL " + url);
        }
        BufferedReader br = new BufferedReader(new InputStreamReader((connection.getInputStream())));
        String line;
        while ((line = br.readLine()) != null) {
            jsonString += line;
        }
    } catch (Throwable x) {
        throw new RuntimeException(x.getMessage());
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
    return jsonString;
}

From source file:net.alteiar.utils.file.SerializableFile.java

/**
 * //  w  w w .  j  ava2  s  .  co  m
 * @param data
 * @param dest
 *            - the file must exist
 * @throws IOException
 */
private static void writeByteToFile(byte[] data, File dest) throws IOException {
    OutputStream os = null;

    IOException ex = null;

    try {
        os = new FileOutputStream(dest);
        os.write(data);
        os.flush();
    } catch (IOException e) {
        ex = e;
    } finally {
        try {
            os.close();
        } catch (IOException e) {
            ex = e;
        }
    }

    if (ex != null) {
        throw ex;
    }
}

From source file:Main.java

/**
 * Configures the connection to serve as a transparent proxy to a remote machine.
 * The given streams must belong to a socket created by createSocket().
 *
 * @param inputStream inputStream of the socket we want to configure
 * @param outputStream outputStream of the socket we want to configure
 * @param remoteAddress address of the remote machine (as you would type in a browser
 *      in a machine that the device is connected to via adb)
 * @param remotePort port on which to connect
 * @return if the configuration suceeded
 * @throws IOException/* w  w w.j a  v a2 s  . c om*/
 */
public static boolean configureConnection(InputStream inputStream, OutputStream outputStream,
        String remoteAddress, int remotePort) throws IOException {
    String cmd = "tcp:" + remotePort + ":" + remoteAddress;
    cmd = String.format("%04X", cmd.length()) + cmd;

    byte[] buf = new byte[ADB_RESPONSE_SIZE];
    outputStream.write(cmd.getBytes());
    int read = inputStream.read(buf);
    if (read != ADB_RESPONSE_SIZE || !ADB_OK.equals(new String(buf))) {
        Log.w(LOG_TAG, "adb cmd failed.");
        return false;
    }
    return true;
}

From source file:me.vertretungsplan.parser.DSBMobileParser.java

private static String encode(String input) throws IOException {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    OutputStream gzipOs = new GZIPOutputStream(os);
    gzipOs.write(input.getBytes(ENCODING));
    gzipOs.close();//w  w  w.j av  a2 s .c  o m
    byte[] outputBytes = os.toByteArray();
    return Base64.encodeBase64String(outputBytes);
}

From source file:CopyUtils.java

/**
 * Copy bytes from a <code>byte[]</code> to an <code>OutputStream</code>.
 * @param input the byte array to read from
 * @param output the <code>OutputStream</code> to write to
 * @throws IOException In case of an I/O problem
 *//*w  w  w  .  jav a 2  s . c  o m*/
public static void copy(byte[] input, OutputStream output) throws IOException {
    output.write(input);
}

From source file:FileUtils.java

public static void writeFile(File file, byte[] data) throws IOException {

    OutputStream stream = new FileOutputStream(file);
    try {// w ww .ja  v a  2 s  .  co m
        stream.write(data);
    } finally {
        try {
            stream.flush();
        } finally {
            stream.close();
        }
    }
}

From source file:Base64.java

private static void writeBytes(final File file, final byte[] data) {
    try {/*from w w  w.  j av  a 2  s .co  m*/
        final OutputStream fos = new FileOutputStream(file);
        final OutputStream os = new BufferedOutputStream(fos);
        os.write(data);
        os.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.onedrive.api.internal.MultipartRelatedHttpMessageConverter.java

private static void writeEnd(OutputStream os, byte[] boundary) throws IOException {
    os.write('-');
    os.write('-');
    os.write(boundary);//from w  ww .  j a  va  2 s .  co m
    os.write('-');
    os.write('-');
    writeNewLine(os);
}