private static int crc32(BytesReference data) { OutputStream dummy = new OutputStream() { @Override public void write(int b) throws IOException { // no-op } @Override public void write(byte[] b, int off, int len) throws IOException { // no-op } }; CRC32 crc32 = new CRC32(); try { data.writeTo(new CheckedOutputStream(dummy, crc32)); } catch (IOException bogus) { // cannot happen throw new Error(bogus); } return (int) crc32.getValue(); }
public static byte[] writeMultiLineFormat(MultiSearchRequest multiSearchRequest, XContent xContent) throws IOException { ByteArrayOutputStream output = new ByteArrayOutputStream(); for (SearchRequest request : multiSearchRequest.requests()) { try (XContentBuilder xContentBuilder = XContentBuilder.builder(xContent)) { writeSearchRequestParams(request, xContentBuilder); BytesReference.bytes(xContentBuilder).writeTo(output); } output.write(xContent.streamSeparator()); try (XContentBuilder xContentBuilder = XContentBuilder.builder(xContent)) { if (request.source() != null) { request.source().toXContent(xContentBuilder, ToXContent.EMPTY_PARAMS); } else { xContentBuilder.startObject(); xContentBuilder.endObject(); } BytesReference.bytes(xContentBuilder).writeTo(output); } output.write(xContent.streamSeparator()); } return output.toByteArray(); }
/** * Writes the bytes reference, including a length header. */ public void writeBytesReference(@Nullable BytesReference bytes) throws IOException { if (bytes == null) { writeVInt(0); return; } writeVInt(bytes.length()); bytes.writeTo(this); }
/** * Writes an optional bytes reference including a length header. Use this if you need to differentiate between null and empty bytes * references. Use {@link #writeBytesReference(BytesReference)} and {@link StreamInput#readBytesReference()} if you do not. */ public void writeOptionalBytesReference(@Nullable BytesReference bytes) throws IOException { if (bytes == null) { writeVInt(0); return; } writeVInt(bytes.length() + 1); bytes.writeTo(this); }
private void writeTo(final T obj, final String blobName, final CheckedConsumer<BytesArray, IOException> consumer) throws IOException { final BytesReference bytes = write(obj); try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { final String resourceDesc = "ChecksumBlobStoreFormat.writeBlob(blob=\"" + blobName + "\")"; try (OutputStreamIndexOutput indexOutput = new OutputStreamIndexOutput(resourceDesc, blobName, outputStream, BUFFER_SIZE)) { CodecUtil.writeHeader(indexOutput, codec, VERSION); try (OutputStream indexOutputOutputStream = new IndexOutputOutputStream(indexOutput) { @Override public void close() throws IOException { // this is important since some of the XContentBuilders write bytes on close. // in order to write the footer we need to prevent closing the actual index input. } }) { bytes.writeTo(indexOutputOutputStream); } CodecUtil.writeFooter(indexOutput); } consumer.accept(new BytesArray(outputStream.toByteArray())); } }
public void writeText(Text text) throws IOException { if (!text.hasBytes()) { final String string = text.string(); spare.copyChars(string); writeInt(spare.length()); write(spare.bytes(), 0, spare.length()); } else { BytesReference bytes = text.bytes(); writeInt(bytes.length()); bytes.writeTo(this); } }
/** * Add the given bytes to the translog with the specified sequence number; returns the location the bytes were written to. * * @param data the bytes to write * @param seqNo the sequence number associated with the operation * @return the location the bytes were written to * @throws IOException if writing to the translog resulted in an I/O exception */ public synchronized Translog.Location add(final BytesReference data, final long seqNo) throws IOException { ensureOpen(); final long offset = totalOffset; try { data.writeTo(outputStream); } catch (final Exception ex) { closeWithTragicEvent(ex); throw ex; } totalOffset += data.length(); if (minSeqNo == SequenceNumbers.NO_OPS_PERFORMED) { assert operationCounter == 0; } if (maxSeqNo == SequenceNumbers.NO_OPS_PERFORMED) { assert operationCounter == 0; } minSeqNo = SequenceNumbers.min(minSeqNo, seqNo); maxSeqNo = SequenceNumbers.max(maxSeqNo, seqNo); operationCounter++; assert assertNoSeqNumberConflict(seqNo, data); return new Translog.Location(generation, offset, data.length()); }
/** * 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 void writeBlob(String blobName, BytesReference data) throws IOException { final Path file = path.resolve(blobName); try (OutputStream outputStream = Files.newOutputStream(file)) { data.writeTo(outputStream); } IOUtils.fsync(file, false); IOUtils.fsync(path, true); }
/** * Writes an optional bytes reference including a length header. Use this if you need to differentiate between null and empty bytes * references. Use {@link #writeBytesReference(BytesReference)} and {@link StreamInput#readBytesReference()} if you do not. */ public void writeOptionalBytesReference(@Nullable BytesReference bytes) throws IOException { if (bytes == null) { writeVInt(0); return; } writeVInt(bytes.length() + 1); bytes.writeTo(this); }
/** * Writes the bytes reference, including a length header. */ public void writeBytesReference(@Nullable BytesReference bytes) throws IOException { if (bytes == null) { writeVInt(0); return; } writeVInt(bytes.length()); bytes.writeTo(this); }
/** * Writes an optional bytes reference including a length header. Use this if you need to differentiate between null and empty bytes * references. Use {@link #writeBytesReference(BytesReference)} and {@link StreamInput#readBytesReference()} if you do not. */ public void writeOptionalBytesReference(@Nullable BytesReference bytes) throws IOException { if (bytes == null) { writeVInt(0); return; } writeVInt(bytes.length() + 1); bytes.writeTo(this); }
/** * Writes the bytes reference, including a length header. */ public void writeBytesReference(@Nullable BytesReference bytes) throws IOException { if (bytes == null) { writeVInt(0); return; } writeVInt(bytes.length()); bytes.writeTo(this); }
/** * Writes the bytes reference, including a length header. */ public void writeBytesReference(@Nullable BytesReference bytes) throws IOException { if (bytes == null) { writeVInt(0); return; } writeVInt(bytes.length()); bytes.writeTo(this); }
@Override public final void writeRawField(String name, BytesReference content, XContentType contentType) throws IOException { if (mayWriteRawData(contentType) == false) { writeFieldName(name); copyRawValue(content, contentType.xContent()); } else { writeStartRaw(name); flush(); content.writeTo(os); writeEndRaw(); } }
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 writeText(Text text) throws IOException { if (!text.hasBytes()) { final String string = text.string(); spare.copyChars(string); writeInt(spare.length()); write(spare.bytes(), 0, spare.length()); } else { BytesReference bytes = text.bytes(); writeInt(bytes.length()); bytes.writeTo(this); } }
public void writeText(Text text) throws IOException { if (!text.hasBytes()) { final String string = text.string(); spare.copyChars(string); writeInt(spare.length()); write(spare.bytes(), 0, spare.length()); } else { BytesReference bytes = text.bytes(); writeInt(bytes.length()); bytes.writeTo(this); } }
public void writeText(Text text) throws IOException { if (!text.hasBytes()) { final String string = text.string(); spare.copyChars(string); writeInt(spare.length()); write(spare.bytes(), 0, spare.length()); } else { BytesReference bytes = text.bytes(); writeInt(bytes.length()); bytes.writeTo(this); } }
public void writeText(Text text) throws IOException { if (!text.hasBytes()) { final String string = text.string(); spare.copyChars(string); writeInt(spare.length()); write(spare.bytes(), 0, spare.length()); } else { BytesReference bytes = text.bytes(); writeInt(bytes.length()); bytes.writeTo(this); } }