Search in sources :

Example 1 with ProgressOutputStream

use of com.box.androidsdk.content.utils.ProgressOutputStream in project box-android-sdk by box.

the class BoxRequestMultipart method writeBody.

protected void writeBody(HttpURLConnection connection, ProgressListener listener) throws BoxException {
    try {
        connection.setChunkedStreamingMode(0);
        connection.setDoOutput(true);
        connection.setUseCaches(false);
        this.outputStream = connection.getOutputStream();
        for (Map.Entry<String, String> entry : this.fields.entrySet()) {
            this.writePartHeader(new String[][] { { "name", entry.getKey() } });
            this.writeOutput(entry.getValue());
        }
        this.writePartHeader(new String[][] { { "name", "filename" }, { "filename", this.filename } }, "application/octet-stream");
        OutputStream fileContentsOutputStream = this.outputStream;
        if (listener != null) {
            fileContentsOutputStream = new ProgressOutputStream(this.outputStream, listener, this.fileSize);
        }
        byte[] buffer = new byte[BUFFER_SIZE];
        int n = this.inputStream.read(buffer);
        while (n != -1) {
            fileContentsOutputStream.write(buffer, 0, n);
            n = this.inputStream.read(buffer);
        }
        if (LOGGER.isLoggable(Level.FINE)) {
            this.loggedRequest.append("<File Contents Omitted>");
        }
        this.writeBoundary();
        this.writeOutput("--");
    } catch (IOException e) {
        throw new BoxException("Couldn't connect to the Box API due to a network error.", e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (Exception e) {
            }
        }
    }
}
Also used : BoxException(com.box.androidsdk.content.BoxException) OutputStream(java.io.OutputStream) ProgressOutputStream(com.box.androidsdk.content.utils.ProgressOutputStream) ProgressOutputStream(com.box.androidsdk.content.utils.ProgressOutputStream) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException) BoxException(com.box.androidsdk.content.BoxException)

Aggregations

BoxException (com.box.androidsdk.content.BoxException)1 ProgressOutputStream (com.box.androidsdk.content.utils.ProgressOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1