Search in sources :

Example 86 with OutputStream

use of java.io.OutputStream in project platform_frameworks_base by android.

the class FileRotatorTest method touch.

private void touch(String... names) throws IOException {
    for (String name : names) {
        final OutputStream out = new FileOutputStream(new File(mBasePath, name));
        out.close();
    }
}
Also used : DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 87 with OutputStream

use of java.io.OutputStream in project platform_frameworks_base by android.

the class FileRotatorTest method testThrowRestoresBackup.

public void testThrowRestoresBackup() throws Exception {
    final FileRotator rotate = new FileRotator(mBasePath, PREFIX, MINUTE_IN_MILLIS, DAY_IN_MILLIS);
    final RecordingReader reader = new RecordingReader();
    long currentTime = TEST_TIME;
    // first, write some valid data
    rotate.combineActive(reader, writer("foo"), currentTime);
    reader.assertRead();
    assertReadAll(rotate, "foo");
    try {
        // now, try writing which will throw
        reader.reset();
        rotate.combineActive(reader, new Writer() {

            public void write(OutputStream out) throws IOException {
                new DataOutputStream(out).writeUTF("bar");
                throw new NullPointerException("yikes");
            }
        }, currentTime);
        fail("woah, somehow able to write exception");
    } catch (IOException e) {
    // expected from above
    }
    // assert that we read original data, and that it's still intact after
    // the failed write above.
    reader.assertRead("foo");
    assertReadAll(rotate, "foo");
}
Also used : DataOutputStream(java.io.DataOutputStream) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Writer(com.android.internal.util.FileRotator.Writer)

Example 88 with OutputStream

use of java.io.OutputStream in project platform_frameworks_base by android.

the class NetworkStatsCollectionTest method stageFile.

/**
     * Copy a {@link Resources#openRawResource(int)} into {@link File} for
     * testing purposes.
     */
private void stageFile(int rawId, File file) throws Exception {
    new File(file.getParent()).mkdirs();
    InputStream in = null;
    OutputStream out = null;
    try {
        in = getContext().getResources().openRawResource(rawId);
        out = new FileOutputStream(file);
        Streams.copy(in, out);
    } finally {
        IoUtils.closeQuietly(in);
        IoUtils.closeQuietly(out);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataOutputStream(java.io.DataOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 89 with OutputStream

use of java.io.OutputStream in project androidquery by androidquery.

the class AbstractAjaxCallback method httpDo.

private void httpDo(HttpUriRequest hr, String url, AjaxStatus status) throws ClientProtocolException, IOException {
    DefaultHttpClient client = getClient();
    if (proxyHandle != null) {
        proxyHandle.applyProxy(this, hr, client);
    }
    if (AGENT != null) {
        hr.addHeader("User-Agent", AGENT);
    } else if (AGENT == null && GZIP) {
        hr.addHeader("User-Agent", "gzip");
    }
    if (headers != null) {
        for (String name : headers.keySet()) {
            hr.addHeader(name, headers.get(name));
        }
    }
    if (GZIP && (headers == null || !headers.containsKey("Accept-Encoding"))) {
        hr.addHeader("Accept-Encoding", "gzip");
    }
    if (ah != null) {
        ah.applyToken(this, hr);
    }
    String cookie = makeCookie();
    if (cookie != null) {
        hr.addHeader("Cookie", cookie);
    }
    HttpParams hp = hr.getParams();
    if (proxy != null)
        hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
    if (timeout > 0) {
        hp.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout);
        hp.setParameter(CoreConnectionPNames.SO_TIMEOUT, timeout);
    }
    if (!redirect) {
        hp.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
    }
    HttpContext context = new BasicHttpContext();
    CookieStore cookieStore = new BasicCookieStore();
    context.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
    request = hr;
    if (abort) {
        throw new IOException("Aborted");
    }
    if (SIMULATE_ERROR) {
        throw new IOException("Simulated Error");
    }
    HttpResponse response = null;
    try {
        //response = client.execute(hr, context);
        response = execute(hr, client, context);
    } catch (HttpHostConnectException e) {
        //if proxy is used, automatically retry without proxy
        if (proxy != null) {
            AQUtility.debug("proxy failed, retrying without proxy");
            hp.setParameter(ConnRoutePNames.DEFAULT_PROXY, null);
            //response = client.execute(hr, context);
            response = execute(hr, client, context);
        } else {
            throw e;
        }
    }
    byte[] data = null;
    String redirect = url;
    int code = response.getStatusLine().getStatusCode();
    String message = response.getStatusLine().getReasonPhrase();
    String error = null;
    HttpEntity entity = response.getEntity();
    File file = null;
    File tempFile = null;
    if (code < 200 || code >= 300) {
        InputStream is = null;
        try {
            if (entity != null) {
                is = entity.getContent();
                byte[] s = toData(getEncoding(entity), is);
                error = new String(s, "UTF-8");
                AQUtility.debug("error", error);
            }
        } catch (Exception e) {
            AQUtility.debug(e);
        } finally {
            AQUtility.close(is);
        }
    } else {
        HttpHost currentHost = (HttpHost) context.getAttribute(ExecutionContext.HTTP_TARGET_HOST);
        HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST);
        redirect = currentHost.toURI() + currentReq.getURI();
        int size = Math.max(32, Math.min(1024 * 64, (int) entity.getContentLength()));
        OutputStream os = null;
        InputStream is = null;
        try {
            file = getPreFile();
            if (file == null) {
                os = new PredefinedBAOS(size);
            } else {
                //file.createNewFile();
                tempFile = makeTempFile(file);
                os = new BufferedOutputStream(new FileOutputStream(tempFile));
            }
            is = entity.getContent();
            boolean gzip = "gzip".equalsIgnoreCase(getEncoding(entity));
            if (gzip) {
                is = new GZIPInputStream(is);
            }
            int contentLength = (int) entity.getContentLength();
            //AQUtility.debug("gzip response", entity.getContentEncoding());
            copy(is, os, contentLength, tempFile, file);
            if (file == null) {
                data = ((PredefinedBAOS) os).toByteArray();
            } else {
                if (!file.exists() || file.length() == 0) {
                    file = null;
                }
            }
        } finally {
            AQUtility.close(is);
            AQUtility.close(os);
        }
    }
    AQUtility.debug("response", code);
    if (data != null) {
        AQUtility.debug(data.length, url);
    }
    status.code(code).message(message).error(error).redirect(redirect).time(new Date()).data(data).file(file).client(client).context(context).headers(response.getAllHeaders());
}
Also used : HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpEntity(org.apache.http.HttpEntity) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) DataOutputStream(java.io.DataOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) GZIPInputStream(java.util.zip.GZIPInputStream) HttpHost(org.apache.http.HttpHost) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) PredefinedBAOS(com.androidquery.util.PredefinedBAOS) BufferedOutputStream(java.io.BufferedOutputStream) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpHostConnectException(org.apache.http.conn.HttpHostConnectException) ClientProtocolException(org.apache.http.client.ClientProtocolException) IOException(java.io.IOException) Date(java.util.Date) CookieStore(org.apache.http.client.CookieStore) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicCookieStore(org.apache.http.impl.client.BasicCookieStore) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

Example 90 with OutputStream

use of java.io.OutputStream in project androidannotations by androidannotations.

the class ViewServer method writeValue.

private static boolean writeValue(Socket client, String value) {
    boolean result;
    BufferedWriter out = null;
    try {
        OutputStream clientStream = client.getOutputStream();
        out = new BufferedWriter(new OutputStreamWriter(clientStream), 8 * 1024);
        out.write(value);
        out.write("\n");
        out.flush();
        result = true;
    } catch (Exception e) {
        result = false;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                result = false;
            }
        }
    }
    return result;
}
Also used : OutputStream(java.io.OutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) IOException(java.io.IOException) BufferedWriter(java.io.BufferedWriter)

Aggregations

OutputStream (java.io.OutputStream)4178 IOException (java.io.IOException)1673 FileOutputStream (java.io.FileOutputStream)1331 InputStream (java.io.InputStream)1304 File (java.io.File)925 ByteArrayOutputStream (java.io.ByteArrayOutputStream)830 Test (org.junit.Test)735 BufferedOutputStream (java.io.BufferedOutputStream)477 FileInputStream (java.io.FileInputStream)431 Socket (java.net.Socket)375 ByteArrayInputStream (java.io.ByteArrayInputStream)233 URL (java.net.URL)231 OutputStreamWriter (java.io.OutputStreamWriter)230 HttpURLConnection (java.net.HttpURLConnection)189 BufferedInputStream (java.io.BufferedInputStream)178 InputStreamReader (java.io.InputStreamReader)176 BufferedReader (java.io.BufferedReader)159 FileNotFoundException (java.io.FileNotFoundException)158 GZIPOutputStream (java.util.zip.GZIPOutputStream)157 Path (java.nio.file.Path)155