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));
}
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);
}
}
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));
}
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;
}
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));
}
Aggregations