@Override public byte[] getBytes() { return (ba != null ? ba.bytes() : BytesArray.EMPTY); }
public void setBytes(byte[] data, int size) { this.data.bytes(data, size); } }
public void setBytes(byte[] data, int size) { this.data.bytes(data, size); }
public BytesArray(String source) { bytes(source); }
public BytesArray pop() { Entry entry = entries.remove(0); size -= entry.length; byte[] entryData = new byte[entry.length]; System.arraycopy(data.bytes(), entry.offset, entryData, 0, entry.length); return new BytesArray(entryData, entry.length); }
@Override public void write(DataOutput out) throws IOException { int size = (ba != null ? ba.length() : 0); byte[] bytes = (ba != null ? ba.bytes() : BytesArray.EMPTY); out.writeInt(size); out.write(bytes, 0, size); }
public void process(BytesArray storage) { // no extractors, no lookups if (ObjectUtils.isEmpty(paths)) { return; } results.clear(); if (log.isTraceEnabled()) { log.trace(String.format("About to look for paths [%s] in doc [%s]", Arrays.toString(paths), storage)); } results.addAll(ParsingUtils.values(new JacksonJsonParser(storage.bytes(), 0, storage.length()), paths)); }
@Override public int hashCode() { return WritableComparator.hashBytes(ba.bytes(), ba.length()); }
/** * Generate the stream of bytes as hex pairs separated by ' '. */ @Override public String toString() { return (ba != null ? StringUtils.asUTFString(ba.bytes(), 0, ba.length()) : ""); }
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()); }
@Override public void convert(Object from, BytesArray to) { Assert.isTrue(from == null || from instanceof Tuple, String.format("Unexpected object type, expecting [%s], given [%s]", Tuple.class, from.getClass())); // handle common cases Tuple tuple = (Tuple) from; if (tuple == null || tuple.size() == 0) { to.bytes("{}"); return; } Assert.isTrue(tuple.size() == 1, "When using JSON input, only one field is expected"); super.convert(tuple.getValue(0), to); } }
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(); }
void doWrite(Object value) { // common-case - constants or JDK types if (value instanceof String || jsonInput || value instanceof Number || value instanceof Boolean || value == null) { String valueString = (value == null ? "null" : value.toString()); if (value instanceof String && !jsonInput) { valueString = StringUtils.toJsonString(valueString); } pool.get().bytes(valueString); } else if (value instanceof Date) { String valueString = (value == null ? "null": Long.toString(((Date) value).getTime())); pool.get().bytes(valueString); } else if (value instanceof RawJson) { pool.get().bytes(((RawJson) value).json()); } // library specific type - use the value writer (a bit overkill but handles collections/arrays properly) else { BytesArray ba = pool.get(); JacksonJsonGenerator generator = new JacksonJsonGenerator(new FastByteArrayOutputStream(ba)); valueWriter.write(value, generator); generator.flush(); generator.close(); } }
to.bytes(writable.toString()); return;
BytesArrayPool write(Object object) { pool.reset(); Object value = extractor.field(object); if (value == FieldExtractor.NOT_FOUND) { String obj = (extractor instanceof FieldExplainer ? ((FieldExplainer) extractor).toString(object) : object.toString()); throw new EsHadoopIllegalArgumentException(String.format("[%s] cannot extract value from entity [%s] | instance [%s]", extractor, obj.getClass(), obj)); } else if (value == FieldExtractor.SKIP) { // Skip it return pool; } if (headerValue != null) { pool.get().bytes(headerValue); } if (value instanceof List) { for (Object val : (List) value) { doWrite(val); } } // if/else to save one collection/iterator instance else { doWrite(value); } return pool; }
@Override public void convert(Object from, BytesArray to) { // expect a tuple holding one field - chararray or bytearray Assert.isTrue(from instanceof SinkCall, String.format("Unexpected object type, expecting [%s], given [%s]", SinkCall.class, from.getClass())); // handle common cases SinkCall sinkCall = (SinkCall) from; Tuple rawTuple = sinkCall.getOutgoingEntry().getTuple(); if (rawTuple == null || rawTuple.isEmpty()) { to.bytes("{}"); return; } Assert.isTrue(rawTuple.size() == 1, "When using JSON input, only one field is expected"); // postpone the coercion Tuple tuple = CascadingUtils.coerceToString(sinkCall); super.convert(tuple.getObject(0), to); } }
@Test public void testAddAndReset() throws Exception { BytesArrayPool pool = new BytesArrayPool(); pool.get().bytes("Test"); pool.get().bytes("Data"); pool.get().bytes("Rules"); BytesRef ref = new BytesRef(); assertEquals(13, pool.length()); ref.add(pool); assertEquals("TestDataRules", ref.toString()); BytesRef ref2 = new BytesRef(); pool.reset(); pool.get().bytes("New"); pool.get().bytes("Info"); assertEquals(7, pool.length()); ref2.add(pool); assertEquals("NewInfo", ref2.toString()); } }