public static boolean isCompressed(BytesReference bytes) { return compressor(bytes) != null; }
/** * Uncompress the provided data, data can be detected as compressed using {@link #isCompressed(BytesReference)}. * @throws NullPointerException a NullPointerException will be thrown when bytes is null */ public static BytesReference uncompressIfNeeded(BytesReference bytes) throws IOException { Compressor compressor = compressor(Objects.requireNonNull(bytes, "the BytesReference must not be null")); BytesReference uncompressed; if (compressor != null) { uncompressed = uncompress(bytes, compressor); } else { uncompressed = bytes; } return uncompressed; }
/** * Returns bytes reference, also uncompress the source if needed. */ public BytesReference getSourceRef() { if (this.source == null) { return null; } try { this.source = CompressorFactory.uncompressIfNeeded(this.source); return this.source; } catch (IOException e) { throw new ElasticsearchParseException("failed to decompress source", e); } }
/** * 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 = data.toBytes(); this.crc32 = crc32(new BytesArray(uncompressed())); } else { BytesStreamOutput out = new BytesStreamOutput(); try (OutputStream compressedOutput = CompressorFactory.defaultCompressor().streamOutput(out)) { data.writeTo(compressedOutput); } this.bytes = out.bytes().toBytes(); this.crc32 = crc32(data); } assertConsistent(); }
StreamOutput streamOutput = bStream; if (compress != null && compress && (compressThreshold == -1 || source.length() > compressThreshold)) { streamOutput = CompressorFactory.defaultCompressor().streamOutput(bStream); } else if (compress != null && compress && !CompressorFactory.isCompressed(source)) { if (compressThreshold == -1 || source.length() > compressThreshold) { BytesStreamOutput bStream = new BytesStreamOutput(); XContentType contentType = XContentFactory.xContentType(source); if (formatContentType != null && formatContentType != contentType) { XContentBuilder builder = XContentFactory.contentBuilder(formatContentType, CompressorFactory.defaultCompressor().streamOutput(bStream)); builder.copyCurrentStructure(XContentFactory.xContent(contentType).createParser(source)); builder.close(); } else { StreamOutput streamOutput = CompressorFactory.defaultCompressor().streamOutput(bStream); source.writeTo(streamOutput); streamOutput.close(); Compressor compressor = CompressorFactory.compressor(source); if (compressor != null) { InputStream compressedStreamInput = compressor.streamInput(source.streamInput()); StreamOutput streamOutput = CompressorFactory.defaultCompressor().streamOutput(bStream); XContentBuilder builder = XContentFactory.contentBuilder(formatContentType, streamOutput); builder.copyCurrentStructure(XContentFactory.xContent(contentType).createParser(compressedStreamInput));
/** 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); } }
@Nullable public static Compressor compressor(BytesReference bytes) { if (COMPRESSOR.isCompressed(bytes)) { // bytes should be either detected as compressed or as xcontent, // if we have bytes that can be either detected as compressed or // as a xcontent, we have a problem assert XContentHelper.xContentType(bytes) == null; return COMPRESSOR; } XContentType contentType = XContentHelper.xContentType(bytes); if (contentType == null) { if (isAncient(bytes)) { throw new IllegalStateException("unsupported compression: index was created before v2.0.0.beta1 and wasn't upgraded?"); } throw new NotXContentException("Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"); } return null; }
/** * 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.defaultCompressor().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 = bStream.bytes().toBytes(); this.crc32 = (int) crc32.getValue(); assertConsistent(); }
protected AbstractClient(Settings pSettings, boolean loadConfigSettings) { Tuple<Settings, Environment> tuple = InternalSettingsPreparer.prepareSettings(pSettings, loadConfigSettings); Settings settings = settingsBuilder().put(tuple.v1()) .put("network.server", false) .put("node.client", true) .build(); this.environment = tuple.v2(); this.pluginsService = new PluginsService(settings, tuple.v2()); this.settings = pluginsService.updatedSettings(); Version version = Version.CURRENT; CompressorFactory.configure(this.settings); ModulesBuilder modules = new ModulesBuilder(); modules.add(new SettingsModule(this.settings)); modules.add(new ThreadPoolModule(this.settings)); this.injector = modules.createInjector(); }
/** 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); } }
@Nullable public static Compressor compressor(BytesReference bytes) { if (COMPRESSOR.isCompressed(bytes)) { // bytes should be either detected as compressed or as xcontent, // if we have bytes that can be either detected as compressed or // as a xcontent, we have a problem assert XContentFactory.xContentType(bytes) == null; return COMPRESSOR; } XContentType contentType = XContentFactory.xContentType(bytes); if (contentType == null) { if (isAncient(bytes)) { throw new IllegalStateException("unsupported compression: index was created before v2.0.0.beta1 and wasn't upgraded?"); } throw new NotXContentException("Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"); } return null; }
protected BytesReference write(T obj) throws IOException { try (BytesStreamOutput bytesStreamOutput = new BytesStreamOutput()) { if (compress) { try (StreamOutput compressedStreamOutput = CompressorFactory.defaultCompressor().streamOutput(bytesStreamOutput)) { write(obj, compressedStreamOutput); } } else { write(obj, bytesStreamOutput); } return bytesStreamOutput.bytes(); } }
CompressorFactory.configure(settings);
/** * Creates a parser for the bytes using the supplied content-type */ public static XContentParser createParser(NamedXContentRegistry xContentRegistry, DeprecationHandler deprecationHandler, BytesReference bytes, XContentType xContentType) throws IOException { Objects.requireNonNull(xContentType); Compressor compressor = CompressorFactory.compressor(bytes); if (compressor != null) { InputStream compressedInput = compressor.streamInput(bytes.streamInput()); if (compressedInput.markSupported() == false) { compressedInput = new BufferedInputStream(compressedInput); } return XContentFactory.xContent(xContentType).createParser(xContentRegistry, deprecationHandler, compressedInput); } else { return xContentType.xContent().createParser(xContentRegistry, deprecationHandler, bytes.streamInput()); } }
/** Decompress the provided {@link BytesReference}. */ public static BytesReference uncompress(BytesReference bytes) throws IOException { Compressor compressor = compressor(bytes); if (compressor == null) { throw new NotCompressedException(); } return uncompress(bytes, compressor); }
/** * Returns bytes reference, also un compress the source if needed. */ public BytesReference sourceRef() { if (source == null) { return null; } try { this.source = CompressorFactory.uncompressIfNeeded(this.source); return this.source; } catch (IOException e) { throw new ElasticsearchParseException("failed to decompress source", e); } }
/** 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); } }
@Nullable public static Compressor compressor(BytesReference bytes) { if (COMPRESSOR.isCompressed(bytes)) { // bytes should be either detected as compressed or as xcontent, // if we have bytes that can be either detected as compressed or // as a xcontent, we have a problem assert XContentHelper.xContentType(bytes) == null; return COMPRESSOR; } XContentType contentType = XContentHelper.xContentType(bytes); if (contentType == null) { if (isAncient(bytes)) { throw new IllegalStateException("unsupported compression: index was created before v2.0.0.beta1 and wasn't upgraded?"); } throw new NotXContentException("Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"); } return null; }
public static BytesReference serializeFullClusterState(ClusterState clusterState, Version nodeVersion) throws IOException { BytesStreamOutput bStream = new BytesStreamOutput(); try (StreamOutput stream = CompressorFactory.defaultCompressor().streamOutput(bStream)) { stream.setVersion(nodeVersion); stream.writeBoolean(true); clusterState.writeTo(stream); } return bStream.bytes(); }
private void assertConsistent() { assert CompressorFactory.compressor(new BytesArray(bytes)) != null; assert this.crc32 == crc32(new BytesArray(uncompressed())); }