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