org.elasticsearch.common.io.Streams Java Examples

The following examples show how to use org.elasticsearch.common.io.Streams. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MockAmazonS3.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public PutObjectResult putObject(final PutObjectRequest request) throws AmazonClientException {
    assertThat(request.getBucketName(), equalTo(bucket));
    assertThat(request.getMetadata().getSSEAlgorithm(), serverSideEncryption ? equalTo("AES256") : nullValue());
    assertThat(request.getCannedAcl(), notNullValue());
    assertThat(request.getCannedAcl().toString(), cannedACL != null ? equalTo(cannedACL) : equalTo("private"));
    assertThat(request.getStorageClass(), storageClass != null ? equalTo(storageClass) : equalTo("STANDARD"));


    final String blobName = request.getKey();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {
        Streams.copy(request.getInputStream(), out);
        blobs.put(blobName, out.toByteArray());
    } catch (IOException e) {
        throw new AmazonClientException(e);
    }
    return new PutObjectResult();
}
 
Example #2
Source File: MetaDataCreateIndexService.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void addMappings(Map<String, Map<String, Object>> mappings, Path mappingsDir) throws IOException {
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(mappingsDir)) {
        for (Path mappingFile : stream) {
            final String fileName = mappingFile.getFileName().toString();
            if (FileSystemUtils.isHidden(mappingFile)) {
                continue;
            }
            int lastDotIndex = fileName.lastIndexOf('.');
            String mappingType = lastDotIndex != -1 ? mappingFile.getFileName().toString().substring(0, lastDotIndex) : mappingFile.getFileName().toString();
            try (BufferedReader reader = Files.newBufferedReader(mappingFile, Charsets.UTF_8)) {
                String mappingSource = Streams.copyToString(reader);
                if (mappings.containsKey(mappingType)) {
                    XContentHelper.mergeDefaults(mappings.get(mappingType), parseMapping(mappingSource));
                } else {
                    mappings.put(mappingType, parseMapping(mappingSource));
                }
            } catch (Exception e) {
                logger.warn("failed to read / parse mapping [" + mappingType + "] from location [" + mappingFile + "], ignoring...", e);
            }
        }
    }
}
 
Example #3
Source File: ChecksumBlobStoreFormat.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Reads blob with specified name without resolving the blobName using using {@link #blobName} method.
 *
 * @param blobContainer blob container
 * @param blobName blob name
 */
public T readBlob(BlobContainer blobContainer, String blobName) throws IOException {
    final BytesReference bytes = Streams.readFully(blobContainer.readBlob(blobName));
    final String resourceDesc = "ChecksumBlobStoreFormat.readBlob(blob=\"" + blobName + "\")";
    try (ByteArrayIndexInput indexInput =
             new ByteArrayIndexInput(resourceDesc, BytesReference.toBytes(bytes))) {
        CodecUtil.checksumEntireFile(indexInput);
        CodecUtil.checkHeader(indexInput, codec, VERSION, VERSION);
        long filePointer = indexInput.getFilePointer();
        long contentSize = indexInput.length() - CodecUtil.footerLength() - filePointer;
        try (XContentParser parser = XContentHelper.createParser(namedXContentRegistry, LoggingDeprecationHandler.INSTANCE,
                                                                 bytes.slice((int) filePointer, (int) contentSize), XContentType.SMILE)) {
            return reader.apply(parser);
        }
    } catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) {
        // we trick this into a dedicated exception with the original stacktrace
        throw new CorruptStateException(ex);
    }
}
 
Example #4
Source File: TransportBulkCreateIndicesAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void addMappings(Map<String, Map<String, Object>> mappings, File mappingsDir) {
    File[] mappingsFiles = mappingsDir.listFiles();
    for (File mappingFile : mappingsFiles) {
        if (mappingFile.isHidden()) {
            continue;
        }
        int lastDotIndex = mappingFile.getName().lastIndexOf('.');
        String mappingType = lastDotIndex != -1 ? mappingFile.getName().substring(0, lastDotIndex) : mappingFile.getName();
        try {
            String mappingSource = Streams.copyToString(new InputStreamReader(new FileInputStream(mappingFile), Charsets.UTF_8));
            if (mappings.containsKey(mappingType)) {
                XContentHelper.mergeDefaults(mappings.get(mappingType), parseMapping(mappingSource));
            } else {
                mappings.put(mappingType, parseMapping(mappingSource));
            }
        } catch (Exception e) {
            logger.warn("failed to read / parse mapping [" + mappingType + "] from location [" + mappingFile + "], ignoring...", e);
        }
    }
}
 
Example #5
Source File: XmlBuilderTest.java    From elasticsearch-xml with Apache License 2.0 5 votes vote down vote up
@Test
public void testDynamicNamespaces() throws Exception {
    XmlNamespaceContext context = XmlNamespaceContext.getDefaultInstance();
    XmlXParams params = new XmlXParams(context);
    InputStream in = getClass().getResourceAsStream("/dynamic-namespace.json");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Streams.copy(in, out);
    byte[] buf = out.toByteArray();
    String s = convertToXml(params, buf, 0, buf.length, false);
    assertEquals(
        "<root xmlns=\"http://elasticsearch.org/ns/1.0/\" xmlns:atom=\"http://www.w3.org/2005/Atom\" xmlns:dc=\"http://purl.org/dc/elements/1.1/\" xmlns:dcterms=\"http://purl.org/dc/terms/\" xmlns:es=\"http://elasticsearch.org/ns/1.0/\" xmlns:foaf=\"http://xmlns.com/foaf/0.1/\" xmlns:owl=\"http://www.w3.org/2002/07/owl#\" xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:rdfs=\"http://www.w3.org/2000/01/rdf-schema#\" xmlns:xalan=\"http://xml.apache.org/xslt\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\"><context es:ns=\"http://example.org/\" xmlns:ns=\"http://example.org/\"/><wstxns1:foo xmlns:wstxns1=\"http://example.org/\">bar</wstxns1:foo></root>",
        s
    );
}
 
Example #6
Source File: LicenseServiceTest.java    From crate with Apache License 2.0 5 votes vote down vote up
private static byte[] getPrivateKey() {
    try (InputStream is = LicenseServiceTest.class.getResourceAsStream("/private.key")) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Streams.copy(is, out);
        return out.toByteArray();
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    }
}
 
Example #7
Source File: EnterpriseLicense.java    From crate with Apache License 2.0 5 votes vote down vote up
private static byte[] publicKey() {
    try (InputStream is = EnterpriseLicenseService.class.getResourceAsStream("/public.key")) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Streams.copy(is, out);
        return out.toByteArray();
    } catch (IOException ex) {
        throw new IllegalStateException(ex);
    } catch (NullPointerException e) {
        throw new IllegalStateException("The CrateDB distribution is missing its public key", e);
    }
}
 
Example #8
Source File: InputStreamStreamInput.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void readBytes(byte[] b, int offset, int len) throws IOException {
    if (len < 0)
        throw new IndexOutOfBoundsException();
    final int read = Streams.readFully(is, b, offset, len);
    if (read != len) {
        throw new EOFException();
    }
}
 
Example #9
Source File: CompressibleBytesOutputStream.java    From crate with Apache License 2.0 5 votes vote down vote up
CompressibleBytesOutputStream(BytesStream bytesStreamOutput, boolean shouldCompress) throws IOException {
    this.bytesStreamOutput = bytesStreamOutput;
    this.shouldCompress = shouldCompress;
    if (shouldCompress) {
        this.stream = CompressorFactory.COMPRESSOR.streamOutput(Streams.flushOnCloseStream(bytesStreamOutput));
    } else {
        this.stream = bytesStreamOutput;
    }
}
 
Example #10
Source File: MessagesProvider.java    From elasticshell with Apache License 2.0 5 votes vote down vote up
Map<String, String> loadFromClasspath(String resourceName) throws IOException {
    InputStream is = this.getClass().getResourceAsStream(resourceName);
    if (is == null) {
        throw new FileNotFoundException("Unable to find file " + MESSAGES_FILE);
    }
    SettingsLoader settingsLoader = SettingsLoaderFactory.loaderFromResource(resourceName);
    return settingsLoader.load(Streams.copyToString(new InputStreamReader(is, "UTF-8")));
}
 
Example #11
Source File: BaseClient.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
public void mapping(String type, InputStream in) throws IOException {
    if (type == null) {
        return;
    }
    StringWriter sw = new StringWriter();
    Streams.copy(new InputStreamReader(in), sw);
    mappings.put(type, sw.toString());
}
 
Example #12
Source File: Neo4jDriverTest.java    From elasticsearch-river-neo4j with Apache License 2.0 5 votes vote down vote up
@Test
public void settingsAreTakenFromNeo4jObjectIfSet() throws IOException {
    InputStream in = this.classLoader.getResourceAsStream("dummy_river_settings.json");
    RiverSettings riverSettings = new RiverSettings(ImmutableSettings.settingsBuilder().build(), XContentHelper.convertToMap(
                Streams.copyToByteArray(in), false).v2());
    Neo4jDriver driver = new Neo4jDriver(name, riverSettings, "myindex", client);

    assertEquals("time", driver.getTimestampField());
    assertEquals("neoindex", driver.getIndex());
    assertEquals(500, driver.getInterval());
    assertEquals("http://192.56.57.89:7888/db/data", driver.getUri());
    assertEquals("turtle", driver.getType());
}
 
Example #13
Source File: DetectLanguageTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
private void testLanguage(String path, String lang) throws Exception {
    Reader reader = new InputStreamReader(getClass().getResourceAsStream(path), StandardCharsets.UTF_8);
    Writer writer = new StringWriter();
    Streams.copy(reader, writer);
    reader.close();
    writer.close();
    LangdetectService detect = new LangdetectService();
    assertEquals(lang, detect.detectAll(writer.toString()).get(0).getLanguage());
}
 
Example #14
Source File: FSABuilder.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
public FSA load(DataInputStream inputStream) throws IOException {
    this.size = inputStream.readInt();
    this.epsilon = inputStream.readInt();
    this.serialized = new byte[this.size];
    try {
        Streams.readFully(inputStream, serialized);
    } finally {
        inputStream.close();
    }
    final FSA fsa = new ConstantArcSizeFSA(Arrays.copyOf(this.serialized, this.size), this.epsilon);
    this.serialized = null;
    return fsa;
}
 
Example #15
Source File: CsvXContentGenerator.java    From elasticsearch-rest-command with The Unlicense 5 votes vote down vote up
@Override
public void writeRawField(String fieldName, InputStream content, OutputStream bos) throws IOException {
    generator.writeFieldName(fieldName);
    generator.writeRaw(':');
    flush();
    Streams.copy(content, bos);
    finishWriteRaw();
}
 
Example #16
Source File: XmlBuilderTest.java    From elasticsearch-xml with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseJson() throws Exception {
    XmlNamespaceContext context = XmlNamespaceContext.getDefaultInstance();
    context.addNamespace("bib","info:srw/cql-context-set/1/bib-v1/");
    context.addNamespace("abc", "http://localhost/");
    context.addNamespace("xbib", "http://xbib.org/");
    context.addNamespace("lia", "http://xbib.org/namespaces/lia/");
    XmlXParams params = new XmlXParams(context);
    InputStream in = getClass().getResourceAsStream("/test.json");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Streams.copy(in, out);
    byte[] buf = out.toByteArray();
    String s = convertToXml(params, buf, 0, buf.length, false);
    assertEquals(53194, s.length());
}
 
Example #17
Source File: XGBoostJsonParserTests.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
private String readModel(String model) throws IOException {
    try (InputStream is = this.getClass().getResourceAsStream(model)) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Streams.copy(is,  bos);
        return bos.toString(StandardCharsets.UTF_8.name());
    }
}
 
Example #18
Source File: IndexFeatureStore.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
private static String readResourceFile(String indexName, String resource) {
    try (InputStream is = IndexFeatureStore.class.getResourceAsStream(resource)) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Streams.copy(is, out);
        return out.toString(StandardCharsets.UTF_8.name());
    } catch (Exception e) {
        LOGGER.error(
                (org.apache.logging.log4j.util.Supplier<?>) () -> new ParameterizedMessage(
                        "failed to create ltr feature store index [{}] with resource [{}]",
                        indexName, resource), e);
        throw new IllegalStateException("failed to create ltr feature store index with resource [" + resource + "]", e);
    }
}
 
Example #19
Source File: InputStreamStreamInput.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readBytes(byte[] b, int offset, int len) throws IOException {
    if (len < 0)
        throw new IndexOutOfBoundsException();
    final int read = Streams.readFully(is, b, offset, len);
    if (read != len) {
        throw new EOFException();
    }
}
 
Example #20
Source File: XContentFactory.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Guesses the content type based on the provided input stream without consuming it.
 */
public static XContentType xContentType(InputStream si) throws IOException {
    if (si.markSupported() == false) {
        throw new IllegalArgumentException("Cannot guess the xcontent type without mark/reset support on " + si.getClass());
    }
    si.mark(GUESS_HEADER_LENGTH);
    try {
        final byte[] firstBytes = new byte[GUESS_HEADER_LENGTH];
        final int read = Streams.readFully(si, firstBytes);
        return xContentType(new BytesArray(firstBytes, 0, read));
    } finally {
        si.reset();
    }
}
 
Example #21
Source File: HelpPrinter.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static void print(Class clazz, String name, final Terminal terminal) {
    terminal.println(Terminal.Verbosity.SILENT);
    try (InputStream input = clazz.getResourceAsStream(name + HELP_FILE_EXT)) {
        Streams.readAllLines(input, new Callback<String>() {
            @Override
            public void handle(String line) {
                terminal.println(Terminal.Verbosity.SILENT, line);
            }
        });
    } catch (IOException ioe) {
        ioe.printStackTrace(terminal.writer());
    }
    terminal.println();
}
 
Example #22
Source File: Settings.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Loads settings from a stream that represents them using the
 * {@link SettingsLoaderFactory#loaderFromSource(String)}.
 */
public Builder loadFromStream(String resourceName, InputStream is) throws SettingsException {
    SettingsLoader settingsLoader = SettingsLoaderFactory.loaderFromResource(resourceName);
    try {
        Map<String, String> loadedSettings = settingsLoader.load(Streams.copyToString(new InputStreamReader(is, Charsets.UTF_8)));
        put(loadedSettings);
    } catch (Exception e) {
        throw new SettingsException("Failed to load settings from [" + resourceName + "]", e);
    }
    return this;
}
 
Example #23
Source File: FsBlobContainer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void writeBlob(String blobName, InputStream inputStream, long blobSize) throws IOException {
    final Path file = path.resolve(blobName);
    try (OutputStream outputStream = Files.newOutputStream(file)) {
        Streams.copy(inputStream, outputStream, new byte[blobStore.bufferSizeInBytes()]);
    }
    IOUtils.fsync(file, false);
    IOUtils.fsync(path, true);
}
 
Example #24
Source File: CompressorFactory.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private static BytesReference uncompress(BytesReference bytes, Compressor compressor) throws IOException {
    StreamInput compressed = compressor.streamInput(bytes.streamInput());
    BytesStreamOutput bStream = new BytesStreamOutput();
    Streams.copy(compressed, bStream);
    compressed.close();
    return bStream.bytes();
}
 
Example #25
Source File: NumericTokenizer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean incrementToken() throws IOException {
    if (!started) {
        // reset() must be idempotent, this is why we read data in incrementToken
        final int len = Streams.readFully(input, buffer);
        if (len == buffer.length && input.read() != -1) {
            throw new IOException("Cannot read numeric data larger than " + buffer.length + " chars");
        }
        setValue(numericTokenStream, new String(buffer, 0, len));
        numericTokenStream.reset();
        started = true;
    }
    return numericTokenStream.incrementToken();
}
 
Example #26
Source File: ScriptService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void onFileInit(Path file) {
    if (logger.isTraceEnabled()) {
        logger.trace("Loading script file : [{}]", file);
    }
    Tuple<String, String> scriptNameExt = scriptNameExt(file);
    if (scriptNameExt != null) {
        ScriptEngineService engineService = getScriptEngineServiceForFileExt(scriptNameExt.v2());
        if (engineService == null) {
            logger.warn("no script engine found for [{}]", scriptNameExt.v2());
        } else {
            try {
                //we don't know yet what the script will be used for, but if all of the operations for this lang
                // with file scripts are disabled, it makes no sense to even compile it and cache it.
                if (isAnyScriptContextEnabled(engineService.types()[0], engineService, ScriptType.FILE)) {
                    logger.info("compiling script file [{}]", file.toAbsolutePath());
                    try(InputStreamReader reader = new InputStreamReader(Files.newInputStream(file), Charsets.UTF_8)) {
                        String script = Streams.copyToString(reader);
                        CacheKey cacheKey = new CacheKey(engineService, scriptNameExt.v1(), null, Collections.<String, String>emptyMap());
                        staticCache.put(cacheKey, new CompiledScript(ScriptType.FILE, scriptNameExt.v1(), engineService.types()[0], engineService.compile(script, Collections.<String, String>emptyMap())));
                        scriptMetrics.onCompilation();
                    }
                } else {
                    logger.warn("skipping compile of script file [{}] as all scripted operations are disabled for file scripts", file.toAbsolutePath());
                }
            } catch (Throwable e) {
                logger.warn("failed to load/compile script [{}]", e, scriptNameExt.v1());
            }
        }
    }
}
 
Example #27
Source File: StandardnumberMappingTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressForbidden(reason = "accessing local resources from classpath")
private String copyToStringFromClasspath(String path) throws Exception {
    return Streams.copyToString(new InputStreamReader(getClass().getResource(path).openStream(), "UTF-8"));
}
 
Example #28
Source File: LangdetectMappingTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 4 votes vote down vote up
@SuppressForbidden(reason = "accessing local resources from classpath")
private String copyToStringFromClasspath(String path) throws Exception {
    return Streams.copyToString(new InputStreamReader(getClass().getResourceAsStream(path), "UTF-8"));
}
 
Example #29
Source File: AbstractLegacyBlobContainer.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void writeBlob(String blobName, InputStream inputStream, long blobSize) throws IOException {
    try (OutputStream stream = createOutput(blobName)) {
        Streams.copy(inputStream, stream);
    }
}