Search in sources :

Example 76 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project blueprints by tinkerpop.

the class GraphMLWriterTest method testNormal.

public void testNormal() throws Exception {
    TinkerGraph g = new TinkerGraph();
    GraphMLReader.inputGraph(g, GraphMLReader.class.getResourceAsStream("graph-example-1.xml"));
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GraphMLWriter w = new GraphMLWriter(g);
    w.setNormalize(true);
    w.outputGraph(bos);
    String expected = streamToString(GraphMLWriterTest.class.getResourceAsStream("graph-example-1-normalized.xml"));
    //System.out.println(expected);
    assertEquals(expected.replace("\n", "").replace("\r", ""), bos.toString().replace("\n", "").replace("\r", ""));
}
Also used : TinkerGraph(com.tinkerpop.blueprints.impls.tg.TinkerGraph) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 77 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project blueprints by tinkerpop.

the class GraphSONWriterTest method outputGraphWithEmbeddedTypes.

@Test
public void outputGraphWithEmbeddedTypes() throws JSONException, IOException {
    Graph g = TinkerGraphFactory.createTinkerGraph();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    GraphSONWriter writer = new GraphSONWriter(g);
    writer.outputGraph(stream, null, null, GraphSONMode.EXTENDED);
    stream.flush();
    stream.close();
    String jsonString = new String(stream.toByteArray());
    ObjectMapper m = new ObjectMapper();
    JsonNode rootNode = m.readValue(jsonString, JsonNode.class);
    // ensure that the JSON conforms to basic structure and that the right
    // number of graph elements are present.  other tests already cover element formatting
    Assert.assertNotNull(rootNode);
    Assert.assertTrue(rootNode.has(GraphSONTokens.MODE));
    Assert.assertEquals("EXTENDED", rootNode.get(GraphSONTokens.MODE).asText());
    Assert.assertTrue(rootNode.has(GraphSONTokens.VERTICES));
    ArrayNode vertices = (ArrayNode) rootNode.get(GraphSONTokens.VERTICES);
    Assert.assertEquals(6, vertices.size());
    Assert.assertTrue(rootNode.has(GraphSONTokens.EDGES));
    ArrayNode edges = (ArrayNode) rootNode.get(GraphSONTokens.EDGES);
    Assert.assertEquals(6, edges.size());
}
Also used : Graph(com.tinkerpop.blueprints.Graph) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 78 with ByteArrayOutputStream

use of java.io.ByteArrayOutputStream in project glitch-hq-android by tinyspeck.

the class ServerRequestTask method readURL.

private String readURL(InputStream is) {
    try {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int r;
        while ((r = is.read()) != -1) {
            baos.write(r);
        }
        return new String(baos.toByteArray());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return "";
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 79 with ByteArrayOutputStream

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

the class TokenStreamSerializer method serialize.

/**
   * Serialize the given TwitterTokenStream into a byte array using the provided AttributeSerializer(s).
   * Note that this method doesn't serialize the CharSequence of the TwitterTokenStream - the caller
   * has to take care of serializing this if necessary.
   */
public final byte[] serialize(final TwitterTokenStream twitterTokenStream) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    AttributeOutputStream output = new AttributeOutputStream(baos);
    for (AttributeSerializer serializer : attributeSerializers) {
        serializer.initialize(twitterTokenStream, CURRENT_VERSION);
    }
    int numTokens = 0;
    while (twitterTokenStream.incrementToken()) {
        serializeAttributes(output);
        numTokens++;
    }
    output.flush();
    byte[] data = baos.toByteArray();
    baos.close();
    baos = new ByteArrayOutputStream(8 + data.length);
    output = new AttributeOutputStream(baos);
    output.writeVInt(CURRENT_VERSION.ordinal());
    output.writeInt(attributeSerializersFingerprint);
    output.writeVInt(numTokens);
    output.write(data);
    output.flush();
    return baos.toByteArray();
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 80 with ByteArrayOutputStream

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

the class Base64ZlibCodec method encode.

/**
   * Encodes a set of bytes.
   *
   * @param plain the non-encoded bytes
   * @return the encoded string
   */
public static String encode(byte[] plain) {
    final ByteArrayOutputStream out = new ByteArrayOutputStream(plain.length / ESTIMATED_PLAINTEXT_TO_ENCODED_RATIO);
    final OutputStream w = getDeflatingEncodingStream(out);
    try {
        w.write(plain);
        w.close();
        return out.toString(BASE64_TEXT_ENCODING);
    } catch (UnsupportedEncodingException e) {
        throw reportUnsupportedEncoding();
    } catch (IOException e) {
        throw Throwables.propagate(e);
    }
}
Also used : OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DeflaterOutputStream(java.util.zip.DeflaterOutputStream) Base64OutputStream(org.apache.commons.codec.binary.Base64OutputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

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