public static String serializeToBase64(Serializable object) { if (object == null) { return StringUtils.EMPTY; } FastByteArrayOutputStream baos = new FastByteArrayOutputStream(); ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(baos); oos.writeObject(object); } catch (IOException ex) { throw new EsHadoopSerializationException("Cannot serialize object " + object, ex); } finally { close(oos); } return DatatypeConverter.printBase64Binary(baos.bytes().bytes()); }
private static String readValueAsString(Parser parser) { FastByteArrayOutputStream out = new FastByteArrayOutputStream(256); JacksonJsonGenerator generator = new JacksonJsonGenerator(out); traverse(parser, generator); generator.close(); return out.toString(); }
public static BytesArray asBytes(BytesArray ba, InputStream in) throws IOException { BytesArray buf = unwrapStreamBuffer(in); if (buf != null) { ba.bytes(buf); return ba; } FastByteArrayOutputStream bos = new FastByteArrayOutputStream(ba); byte[] buffer = new byte[1024]; int read = 0; try { while ((read = in.read(buffer)) != -1) { bos.write(buffer, 0, read); } } finally { try { in.close(); } catch (IOException ex) { // ignore } // non needed but used to avoid the warnings bos.close(); } return bos.bytes(); }
public static ContentBuilder generate(ValueWriter writer) { return new ContentBuilder(new JacksonJsonGenerator(new FastByteArrayOutputStream()), writer); }
private void writableTypeToJson(Writable obj) { ContentBuilder.generate(out, new WritableValueWriter(false)).value(obj).flush().close(); System.out.println(out.bytes()); } }
@After public void after() { out.reset(); }
@BeforeClass public static void beforeClass() { out = new FastByteArrayOutputStream(); }
public void testHandleUnknown() { final String message = "True Belief"; Object obj = new Object() { @Override public String toString() { return message; } }; ContentBuilder.generate(out, new JdkValueWriter(true)).value(obj).flush().close(); assertEquals(message, out.bytes().toString()); }
@Before public void start() { out.reset(); }
static BytesArray writeSerializablePartition(PartitionDefinition def) throws IOException { FastByteArrayOutputStream out = new FastByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(out); try { oos.writeObject(def); oos.flush(); return out.bytes(); } finally { oos.close(); } }
public static BytesArray asBytes(BytesArray ba, InputStream in) throws IOException { BytesArray buf = unwrapStreamBuffer(in); if (buf != null) { ba.bytes(buf); return ba; } FastByteArrayOutputStream bos = new FastByteArrayOutputStream(ba); byte[] buffer = new byte[1024]; int read = 0; try { while ((read = in.read(buffer)) != -1) { bos.write(buffer, 0, read); } } finally { try { in.close(); } catch (IOException ex) { // ignore } // non needed but used to avoid the warnings bos.close(); } return bos.bytes(); }
@Override public String toString() { FastByteArrayOutputStream out = new FastByteArrayOutputStream(256); JacksonJsonGenerator generator = new JacksonJsonGenerator(out); generator.writeBeginObject(); toJson(generator); generator.writeEndObject(); generator.close(); return out.toString(); } }
@Override protected OutputStream openWrite() throws IOException { this.exists = true; this.fileBytes.reset(); return new FastByteArrayOutputStream(fileBytes); }
private String jdkTypeToJson(Object obj) { ContentBuilder.generate(out, new JdkValueWriter(false)).value(obj).flush().close(); return out.bytes().toString(); }
@Before public void start() { out.reset(); }
static BytesArray writeWritablePartition(PartitionDefinition def) throws IOException { FastByteArrayOutputStream out = new FastByteArrayOutputStream(); DataOutputStream da = new DataOutputStream(out); try { def.write(da); da.flush(); return out.bytes(); } finally { da.close(); } }
public static BytesArray asBytes(BytesArray ba, InputStream in) throws IOException { BytesArray buf = unwrapStreamBuffer(in); if (buf != null) { ba.bytes(buf); return ba; } FastByteArrayOutputStream bos = new FastByteArrayOutputStream(ba); byte[] buffer = new byte[1024]; int read = 0; try { while ((read = in.read(buffer)) != -1) { bos.write(buffer, 0, read); } } finally { try { in.close(); } catch (IOException ex) { // ignore } // non needed but used to avoid the warnings bos.close(); } return bos.bytes(); }
static QueryBuilder randomRawQueryStringQuery(Random rand, int level) { QueryBuilder query = randomQuery(rand); FastByteArrayOutputStream out = new FastByteArrayOutputStream(256); Generator generator = new JacksonJsonGenerator(out); generator.writeBeginObject(); query.toJson(generator); generator.writeEndObject(); generator.close(); try { return new RawQueryBuilder(out.toString().trim(), false); } catch (IOException e) { throw new IllegalArgumentException("failed to parse raw query", e); } }
@BeforeClass public static void beforeClass() { out = new FastByteArrayOutputStream(); }
private String pigTypeToJson(PigTuple obj) { ContentBuilder.generate(out, new PigValueWriter(false)).value(obj).flush().close(); return out.bytes().toString(); }