Search in sources :

Example 91 with ByteArrayOutputStream

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();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 92 with ByteArrayOutputStream

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;
}
Also used : FileOutputStream(java.io.FileOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException)

Example 93 with ByteArrayOutputStream

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());
}
Also used : Endpoint(com.twitter.thrift.Endpoint) GsonBuilder(com.google.gson.GsonBuilder) ServiceInstance(com.twitter.thrift.ServiceInstance) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest) Test(org.junit.Test)

Example 94 with ByteArrayOutputStream

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();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) Test(org.junit.Test)

Example 95 with ByteArrayOutputStream

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());
    }
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)8438 Test (org.junit.Test)2232 ByteArrayInputStream (java.io.ByteArrayInputStream)2148 IOException (java.io.IOException)2037 PrintStream (java.io.PrintStream)800 InputStream (java.io.InputStream)765 ObjectOutputStream (java.io.ObjectOutputStream)759 DataOutputStream (java.io.DataOutputStream)705 ObjectInputStream (java.io.ObjectInputStream)361 File (java.io.File)331 OutputStream (java.io.OutputStream)318 HashMap (java.util.HashMap)279 ArrayList (java.util.ArrayList)264 FileInputStream (java.io.FileInputStream)211 OutputStreamWriter (java.io.OutputStreamWriter)207 DataInputStream (java.io.DataInputStream)198 Test (org.testng.annotations.Test)184 PrintWriter (java.io.PrintWriter)162 URL (java.net.URL)160 Map (java.util.Map)158