use of java.io.ByteArrayOutputStream in project blueprints by tinkerpop.
the class TinkerMetadataWriterTest method streamToByteArray.
private byte[] streamToByteArray(final InputStream in) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] data = new byte[16384];
while ((nRead = in.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
return buffer.toByteArray();
}
use of java.io.ByteArrayOutputStream in project glitch-hq-android by tinyspeck.
the class BitmapUtil method SaveJPEG.
public static boolean SaveJPEG(Bitmap bm, String sRelativePath) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bos);
byte[] data = bos.toByteArray();
try {
File file = new File(Environment.getExternalStorageDirectory(), sRelativePath);
if (!file.exists())
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
fos.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
use of java.io.ByteArrayOutputStream in project commons by twitter.
the class ServerSetImplTest method testJsonCodecCompatibility.
@Test
public void testJsonCodecCompatibility() throws IOException {
ServiceInstance instance = new ServiceInstance(new Endpoint("foo", 1000), ImmutableMap.of("http", new Endpoint("foo", 8080)), Status.ALIVE).setShard(42);
ByteArrayOutputStream legacy = new ByteArrayOutputStream();
JsonCodec.create(ServiceInstance.class, new GsonBuilder().setExclusionStrategies(JsonCodec.getThriftExclusionStrategy()).create()).serialize(instance, legacy);
ByteArrayOutputStream results = new ByteArrayOutputStream();
ServerSetImpl.createJsonCodec().serialize(instance, results);
assertEquals(legacy.toString(), results.toString());
results = new ByteArrayOutputStream();
ServerSetImpl.createJsonCodec().serialize(instance, results);
assertEquals("{\"serviceEndpoint\":{\"host\":\"foo\",\"port\":1000}," + "\"additionalEndpoints\":{\"http\":{\"host\":\"foo\",\"port\":8080}}," + "\"status\":\"ALIVE\"," + "\"shard\":42}", results.toString());
}
use of java.io.ByteArrayOutputStream in project commons by twitter.
the class TokenTypeAttributeImplTest method testCannotSerialize.
@Test(expected = NotSerializableException.class)
public void testCannotSerialize() throws IOException {
// Make sure that serialization throws an exception.
ObjectOutputStream oos = new ObjectOutputStream(new ByteArrayOutputStream());
oos.writeObject(new TokenTypeAttributeImpl());
oos.close();
}
use of java.io.ByteArrayOutputStream in project commons by twitter.
the class TokenStreamSerializerTest method testVInt.
@Test
public void testVInt() throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
TokenStreamSerializer.AttributeOutputStream out = new TokenStreamSerializer.AttributeOutputStream(baos);
final int N = 10000;
final int[] expected = new int[N];
Random rnd = new Random();
for (int i = 0; i < N; i++) {
expected[i] = rnd.nextInt();
out.writeVInt(expected[i]);
}
out.flush();
byte[] data = baos.toByteArray();
ByteArrayInputStream bais = new ByteArrayInputStream(data);
TokenStreamSerializer.AttributeInputStream in = new TokenStreamSerializer.AttributeInputStream(bais);
for (int i = 0; i < N; i++) {
assertEquals(expected[i], in.readVInt());
}
}
Aggregations