Search in sources :

Example 56 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project commons by twitter.

the class AssetHandlerTest method unzip.

private static String unzip(ByteArrayOutputStream streamData) throws IOException {
    ByteArrayInputStream in = new ByteArrayInputStream(streamData.toByteArray());
    GZIPInputStream unzip = new GZIPInputStream(in);
    return new String(ByteStreams.toByteArray(unzip));
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 57 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project enroscar by stanfy.

the class BaseContentHandler method getContent.

@Override
public final Object getContent(final URLConnection uConn) throws IOException {
    final ContentControlUrlConnection connection = ContentControlUrlConnection.from(uConn);
    if (connection == null) {
        throw new IllegalArgumentException("Connection is not wrapped with " + ContentControlUrlConnection.class);
    }
    // try to get input stream
    InputStream responseStream = null;
    try {
        responseStream = connection.getInputStream();
    } catch (final IOException responseStreamException) {
        if (Utils.isDebugRest(context)) {
            Log.v(TAG, "Cannot get input stream, message: " + responseStreamException.getMessage() + ", try to use error stream");
        }
        final URLConnection orig = UrlConnectionWrapper.unwrap(connection);
        if (orig instanceof HttpURLConnection) {
            responseStream = ((HttpURLConnection) orig).getErrorStream();
        }
        // no error stream?
        if (responseStream == null) {
            throw responseStreamException;
        }
    }
    // we have input => wrap it for reading
    InputStream source = IoUtils.getUncompressedInputStream(connection.getContentEncoding(), buffersPool.bufferize(responseStream));
    if (Utils.isDebugRestResponse(context)) {
        // source is now closed, don't worry
        final String responseString = IoUtils.streamToString(source, buffersPool);
        Log.d(TAG, responseString);
        source = new ByteArrayInputStream(responseString.getBytes(IoUtils.UTF_8_NAME));
    }
    try {
        return getContent(connection, source, connection.getEntityType());
    } finally {
        // do not forget to close the source
        IoUtils.closeQuietly(source);
    }
}
Also used : ContentControlUrlConnection(com.stanfy.enroscar.net.ContentControlUrlConnection) HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection)

Example 58 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project adt4j by sviperll.

the class MainTest method testSerialization.

@Test
public void testSerialization() throws IOException, ClassNotFoundException {
    UserKey userKey1 = UserKey.valueOf(1);
    ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
    ObjectOutputStream outputStream = new ObjectOutputStream(byteArray);
    outputStream.writeObject(userKey1);
    ObjectInputStream inputStream = new ObjectInputStream(new ByteArrayInputStream(byteArray.toByteArray()));
    UserKey userKey2 = (UserKey) inputStream.readObject();
    assertTrue("userKey1.equals(userKey2)", userKey1.equals(userKey2));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) UserKey(com.github.sviperll.adt4j.examples.UserKey) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) Test(org.junit.Test)

Example 59 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project jpHolo by teusink.

the class LocalFilesystem method writeToFileAtURL.

@Override
public long writeToFileAtURL(LocalFilesystemURL inputURL, String data, int offset, boolean isBinary) throws IOException, NoModificationAllowedException {
    boolean append = false;
    if (offset > 0) {
        this.truncateFileAtURL(inputURL, offset);
        append = true;
    }
    byte[] rawData;
    if (isBinary) {
        rawData = Base64.decode(data, Base64.DEFAULT);
    } else {
        rawData = data.getBytes();
    }
    ByteArrayInputStream in = new ByteArrayInputStream(rawData);
    try {
        byte[] buff = new byte[rawData.length];
        FileOutputStream out = new FileOutputStream(this.filesystemPathForURL(inputURL), append);
        try {
            in.read(buff, 0, buff.length);
            out.write(buff, 0, rawData.length);
            out.flush();
        } finally {
            // Always close the output
            out.close();
        }
    } catch (NullPointerException e) {
        // This is a bug in the Android implementation of the Java Stack
        NoModificationAllowedException realException = new NoModificationAllowedException(inputURL.toString());
        throw realException;
    }
    return rawData.length;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FileOutputStream(java.io.FileOutputStream)

Example 60 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project commons by twitter.

the class AssetHandlerTest method doGet.

private Request doGet(String suppliedChecksum, String supportedEncodings, int expectedResponseCode, boolean expectRead) throws Exception {
    HttpServletRequest req = createMock(HttpServletRequest.class);
    HttpServletResponse resp = createMock(HttpServletResponse.class);
    if (expectRead) {
        expect(inputSupplier.getInput()).andReturn(new ByteArrayInputStream(TEST_DATA.getBytes()));
    }
    expect(req.getHeader("If-None-Match")).andReturn(suppliedChecksum);
    resp.setStatus(expectedResponseCode);
    if (expectedResponseCode == SC_OK) {
        expect(req.getHeader("Accept-Encoding")).andReturn(supportedEncodings);
        resp.setHeader("Cache-Control", "public,max-age=" + CACHE_CONTROL_MAX_AGE_SECS);
        resp.setHeader("ETag", TEST_DATA_CHECKSUM);
        resp.setContentType(CONTENT_TYPE);
        if (supportedEncodings != null && supportedEncodings.contains("gzip")) {
            resp.setHeader("Content-Encoding", "gzip");
        }
    }
    return new Request(req, resp, expectPayload(resp));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)8004 Test (org.junit.Test)2746 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2096 InputStream (java.io.InputStream)1831 IOException (java.io.IOException)1612 ObjectInputStream (java.io.ObjectInputStream)672 DataInputStream (java.io.DataInputStream)653 X509Certificate (java.security.cert.X509Certificate)412 ObjectOutputStream (java.io.ObjectOutputStream)376 CertificateFactory (java.security.cert.CertificateFactory)369 ArrayList (java.util.ArrayList)354 File (java.io.File)315 HashMap (java.util.HashMap)279 Certificate (java.security.cert.Certificate)239 DataOutputStream (java.io.DataOutputStream)226 InputStreamReader (java.io.InputStreamReader)216 FileInputStream (java.io.FileInputStream)211 Document (org.w3c.dom.Document)186 Test (org.testng.annotations.Test)181 Map (java.util.Map)162