public static BytesReference readXContent(final Reader reader, final XContentType xContentType) throws IOException { BytesReference retVal; XContentParser parser = null; try { parser = XContentFactory.xContent(xContentType).createParser(NamedXContentRegistry.EMPTY, SearchGuardDeprecationHandler.INSTANCE, reader); parser.nextToken(); final XContentBuilder builder = XContentFactory.jsonBuilder(); builder.copyCurrentStructure(parser); retVal = BytesReference.bytes(builder); } finally { if (parser != null) { parser.close(); } } //validate Settings.builder().loadFromStream("dummy.json", new ByteArrayInputStream(BytesReference.toBytes(retVal)), true).build(); return retVal; }
private static BytesReference readXContent(final Reader reader, final XContentType xContentType) throws IOException { BytesReference retVal; XContentParser parser = null; try { parser = XContentFactory.xContent(xContentType).createParser(NamedXContentRegistry.EMPTY, SearchGuardDeprecationHandler.INSTANCE, reader); parser.nextToken(); final XContentBuilder builder = XContentFactory.jsonBuilder(); builder.copyCurrentStructure(parser); retVal = BytesReference.bytes(builder); } finally { if (parser != null) { parser.close(); } } //validate Settings.builder().loadFromStream("dummy.json", new ByteArrayInputStream(BytesReference.toBytes(retVal)), true).build(); return retVal; }
/** * The source of the document if exists. */ public byte[] source() { if (source == null) { return null; } if (sourceAsBytes != null) { return sourceAsBytes; } this.sourceAsBytes = BytesReference.toBytes(sourceRef()); return this.sourceAsBytes; }
/** Return the uncompressed bytes. */ public byte[] uncompressed() { try { return BytesReference.toBytes(CompressorFactory.uncompress(new BytesArray(bytes))); } catch (IOException e) { throw new IllegalStateException("Cannot decompress compressed string", e); } }
public static byte[] toBytes(ClusterState state) throws IOException { BytesStreamOutput os = new BytesStreamOutput(); state.writeTo(os); return BytesReference.toBytes(os.bytes()); }
/** * Create a {@link CompressedXContent} out of a {@link ToXContent} instance. */ public CompressedXContent(ToXContent xcontent, XContentType type, ToXContent.Params params) throws IOException { BytesStreamOutput bStream = new BytesStreamOutput(); OutputStream compressedStream = CompressorFactory.COMPRESSOR.streamOutput(bStream); CRC32 crc32 = new CRC32(); OutputStream checkedStream = new CheckedOutputStream(compressedStream, crc32); try (XContentBuilder builder = XContentFactory.contentBuilder(type, checkedStream)) { builder.startObject(); xcontent.toXContent(builder, params); builder.endObject(); } this.bytes = BytesReference.toBytes(bStream.bytes()); this.crc32 = (int) crc32.getValue(); assertConsistent(); }
/** * Create a {@link CompressedXContent} out of a serialized {@link ToXContent} * that may already be compressed. */ public CompressedXContent(BytesReference data) throws IOException { Compressor compressor = CompressorFactory.compressor(data); if (compressor != null) { // already compressed... this.bytes = BytesReference.toBytes(data); this.crc32 = crc32(new BytesArray(uncompressed())); } else { BytesStreamOutput out = new BytesStreamOutput(); try (OutputStream compressedOutput = CompressorFactory.COMPRESSOR.streamOutput(out)) { data.writeTo(compressedOutput); } this.bytes = BytesReference.toBytes(out.bytes()); this.crc32 = crc32(data); } assertConsistent(); }
@Override public Object cqlValue(Object value) { if (value == null) { return null; } BytesReference br = valueForDisplay(value); return ByteBuffer.wrap(BytesReference.toBytes(br)); } }
@Override public List<DependencyLink> apply(SearchResponse response) { if (response.getHits() == null) return ImmutableList.of(); ImmutableList.Builder<DependencyLink> unmerged = ImmutableList.builder(); for (SearchHit hit : response.getHits()) { DependencyLink link = Codec.JSON.readDependencyLink(hit.getSourceRef().toBytes()); unmerged.add(link); } return unmerged.build(); } }
public static byte[] copyToBytesFromClasspath(String path) throws IOException { try (InputStream is = Streams.class.getResourceAsStream(path)) { if (is == null) { throw new FileNotFoundException("Resource [" + path + "] not found in classpath"); } try (BytesStreamOutput out = new BytesStreamOutput()) { Streams.copy(is, out); return BytesReference.toBytes(out.bytes()); } } }
public static byte[] toBytes(ClusterState state) throws IOException { BytesStreamOutput os = new BytesStreamOutput(); state.writeTo(os); return BytesReference.toBytes(os.bytes()); }
public static byte[] toBytes(ClusterState state) throws IOException { BytesStreamOutput os = new BytesStreamOutput(); state.writeTo(os); return BytesReference.toBytes(os.bytes()); }
public static byte[] toBytes(ClusterState state) throws IOException { BytesStreamOutput os = new BytesStreamOutput(); state.writeTo(os); return os.bytes().toBytes(); }
protected byte[] toBytes(BulkRequest request) { BytesStreamOutput out = new BytesStreamOutput(); try { request.writeTo(out); return BytesReference.toBytes(out.bytes()); } catch (IOException e) { throw new NuxeoException("Cannot write elasticsearch bulk request " + request, e); } }
public void testWriteToOutputStream() throws IOException { int length = randomIntBetween(10, PAGE_SIZE * 4); BytesReference pbr = newBytesReference(length); BytesStreamOutput out = new BytesStreamOutput(); pbr.writeTo(out); assertEquals(pbr.length(), out.size()); assertArrayEquals(BytesReference.toBytes(pbr), BytesReference.toBytes(out.bytes())); out.close(); }
public void testSliceWriteToOutputStream() throws IOException { int length = randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 5)); BytesReference pbr = newBytesReference(length); int sliceOffset = randomIntBetween(1, length / 2); int sliceLength = length - sliceOffset; BytesReference slice = pbr.slice(sliceOffset, sliceLength); BytesStreamOutput sliceOut = new BytesStreamOutput(sliceLength); slice.writeTo(sliceOut); assertEquals(slice.length(), sliceOut.size()); assertArrayEquals(BytesReference.toBytes(slice), BytesReference.toBytes(sliceOut.bytes())); sliceOut.close(); }
public void testToBytes() throws IOException { int[] sizes = {0, randomInt(PAGE_SIZE), PAGE_SIZE, randomIntBetween(2, PAGE_SIZE * randomIntBetween(2, 5))}; for (int i = 0; i < sizes.length; i++) { BytesReference pbr = newBytesReference(sizes[i]); byte[] bytes = BytesReference.toBytes(pbr); assertEquals(sizes[i], bytes.length); for (int j = 0; j < bytes.length; j++) { assertEquals(bytes[j], pbr.get(j)); } } }
@Override public void sendResponse(Throwable error) throws IOException { BytesStreamOutput stream = new BytesStreamOutput(); writeResponseExceptionHeader(stream); RemoteTransportException tx = new RemoteTransportException(targetTransport.nodeName(), targetTransport.boundAddress().boundAddresses()[0], action, error); stream.writeThrowable(tx); sendResponseData(stream.bytes().toBytes()); sourceTransportServiceAdapter.onResponseSent(requestId, action, error); }
public void testIterator() throws IOException { int length = randomIntBetween(10, PAGE_SIZE * randomIntBetween(2, 8)); BytesReference pbr = newBytesReference(length); BytesRefIterator iterator = pbr.iterator(); BytesRef ref; BytesRefBuilder builder = new BytesRefBuilder(); while((ref = iterator.next()) != null) { builder.append(ref); } assertArrayEquals(BytesReference.toBytes(pbr), BytesRef.deepCopyOf(builder.toBytesRef()).bytes); }
@Override public void sendResponse(Exception exception) throws IOException { BytesStreamOutput stream = new BytesStreamOutput(); stream.setVersion(version); writeResponseExceptionHeader(stream); RemoteTransportException tx = new RemoteTransportException(targetTransport.nodeName(), targetTransport.boundAddress().boundAddresses()[0], action, exception); stream.writeException(tx); sendResponseData(BytesReference.toBytes(stream.bytes())); sourceTransportServiceAdapter.onResponseSent(requestId, action, exception); }