当前位置: 首页>>代码示例>>Java>>正文


Java Streams.copy方法代码示例

本文整理汇总了Java中org.elasticsearch.common.io.Streams.copy方法的典型用法代码示例。如果您正苦于以下问题:Java Streams.copy方法的具体用法?Java Streams.copy怎么用?Java Streams.copy使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.elasticsearch.common.io.Streams的用法示例。


在下文中一共展示了Streams.copy方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: readBlob

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
/**
 * 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 {
    try (InputStream inputStream = blobContainer.readBlob(blobName)) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Streams.copy(inputStream, out);
        final byte[] bytes = out.toByteArray();
        final String resourceDesc = "ChecksumBlobStoreFormat.readBlob(blob=\"" + blobName + "\")";
        try (ByteArrayIndexInput indexInput = new ByteArrayIndexInput(resourceDesc, bytes)) {
            CodecUtil.checksumEntireFile(indexInput);
            CodecUtil.checkHeader(indexInput, codec, VERSION, VERSION);
            long filePointer = indexInput.getFilePointer();
            long contentSize = indexInput.length() - CodecUtil.footerLength() - filePointer;
            BytesReference bytesReference = new BytesArray(bytes, (int) filePointer, (int) contentSize);
            return read(bytesReference);
        } catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) {
            // we trick this into a dedicated exception with the original stacktrace
            throw new CorruptStateException(ex);
        }
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:26,代码来源:ChecksumBlobStoreFormat.java

示例2: handleFavicon

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
void handleFavicon(RestRequest request, RestChannel channel) {
    if (request.method() == RestRequest.Method.GET) {
        try {
            try (InputStream stream = getClass().getResourceAsStream("/config/favicon.ico")) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                Streams.copy(stream, out);
                BytesRestResponse restResponse = new BytesRestResponse(RestStatus.OK, "image/x-icon", out.toByteArray());
                channel.sendResponse(restResponse);
            }
        } catch (IOException e) {
            channel.sendResponse(new BytesRestResponse(INTERNAL_SERVER_ERROR, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
        }
    } else {
        channel.sendResponse(new BytesRestResponse(FORBIDDEN, BytesRestResponse.TEXT_CONTENT_TYPE, BytesArray.EMPTY));
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:17,代码来源:RestController.java

示例3: writeRawField

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
@Override
public void writeRawField(String name, InputStream content, XContentType contentType) throws IOException {
    if (mayWriteRawData(contentType) == false) {
        // EMPTY is safe here because we never call namedObject when writing raw data
        try (XContentParser parser = XContentFactory.xContent(contentType).createParser(NamedXContentRegistry.EMPTY, content)) {
            parser.nextToken();
            writeFieldName(name);
            copyCurrentStructure(parser);
        }
    } else {
        writeStartRaw(name);
        flush();
        Streams.copy(content, os);
        writeEndRaw();
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:17,代码来源:JsonXContentGenerator.java

示例4: testScriptsWithoutExtensions

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
public void testScriptsWithoutExtensions() throws IOException {
    buildScriptService(Settings.EMPTY);
    Path testFileNoExt = scriptsFilePath.resolve("test_no_ext");
    Path testFileWithExt = scriptsFilePath.resolve("test_script.test");
    Streams.copy("test_file_no_ext".getBytes("UTF-8"), Files.newOutputStream(testFileNoExt));
    Streams.copy("test_file".getBytes("UTF-8"), Files.newOutputStream(testFileWithExt));
    resourceWatcherService.notifyNow();

    CompiledScript compiledScript = scriptService.compile(new Script(ScriptType.FILE, "test", "test_script", Collections.emptyMap()),
            ScriptContext.Standard.SEARCH);
    assertThat(compiledScript.compiled(), equalTo((Object) "compiled_test_file"));

    Files.delete(testFileNoExt);
    Files.delete(testFileWithExt);
    resourceWatcherService.notifyNow();

    try {
        scriptService.compile(new Script(ScriptType.FILE, "test", "test_script", Collections.emptyMap()), ScriptContext.Standard.SEARCH);
        fail("the script test_script should no longer exist");
    } catch (IllegalArgumentException ex) {
        assertThat(ex.getMessage(), containsString("unable to find file script [test_script] using lang [test]"));
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:24,代码来源:ScriptServiceTests.java

示例5: testScriptCompiledOnceHiddenFileDetected

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
public void testScriptCompiledOnceHiddenFileDetected() throws IOException {
    buildScriptService(Settings.EMPTY);

    Path testHiddenFile = scriptsFilePath.resolve(".hidden_file");
    Streams.copy("test_hidden_file".getBytes("UTF-8"), Files.newOutputStream(testHiddenFile));

    Path testFileScript = scriptsFilePath.resolve("file_script.test");
    Streams.copy("test_file_script".getBytes("UTF-8"), Files.newOutputStream(testFileScript));
    resourceWatcherService.notifyNow();

    CompiledScript compiledScript = scriptService.compile(new Script(ScriptType.FILE, "test", "file_script", Collections.emptyMap()),
            ScriptContext.Standard.SEARCH);
    assertThat(compiledScript.compiled(), equalTo((Object) "compiled_test_file_script"));

    Files.delete(testHiddenFile);
    Files.delete(testFileScript);
    resourceWatcherService.notifyNow();
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:19,代码来源:ScriptServiceTests.java

示例6: simpleDeployTest

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
@Test
public void simpleDeployTest() throws IOException, InterruptedException {
    String name = "demo";
    InputStream in = getClass().getResourceAsStream("/example.zip");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Streams.copy(in, out);
    BytesReference content = new BytesArray(out.toByteArray());
    String contentType = "application/zip";
    DeployRequestBuilder deployRequestBuilder = new DeployRequestBuilder(client("1").admin().cluster())
            .setName(name)
            .setContentType(contentType)
            .setContent(content);
    final DeployRequest deployRequest = deployRequestBuilder.request();
    client("1").admin().cluster().execute(DeployAction.INSTANCE, deployRequest);
    Thread.sleep(5000L);
}
 
开发者ID:jprante,项目名称:elasticsearch-plugin-deploy,代码行数:17,代码来源:SimpleTest.java

示例7: httpPost

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
@Test
public void httpPost() throws IOException {
    startCluster();
    try {
        String httpAddress = findHttpAddress(client());
        if (httpAddress == null) {
            throw new IllegalArgumentException("no HTTP address found");
        }
        URL base = new URL(httpAddress);
        URL url = new URL(base, "_langdetect");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        Streams.copy(new StringReader("Das ist ein Text"), new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8));
        StringWriter response = new StringWriter();
        Streams.copy(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8), response);
        assertEquals("{\"languages\":[{\"language\":\"de\",\"probability\":0.9999967609942226}]}", response.toString());
    } finally {
        stopCluster();
    }
}
 
开发者ID:jprante,项目名称:elasticsearch-plugin-bundle,代码行数:23,代码来源:SimpleHttpTest.java

示例8: httpPostShortProfile

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
@Test
public void httpPostShortProfile() throws IOException {
    startCluster();
    try {
        String httpAddress = findHttpAddress(client());
        if (httpAddress == null) {
            throw new IllegalArgumentException("no HTTP address found");
        }
        URL base = new URL(httpAddress);
        URL url = new URL(base, "_langdetect?profile=short-text");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("POST");
        connection.setDoOutput(true);
        connection.setDoInput(true);
        Streams.copy(new StringReader("Das ist ein Text"), new OutputStreamWriter(connection.getOutputStream(), StandardCharsets.UTF_8));
        StringWriter response = new StringWriter();
        Streams.copy(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8), response);
        assertEquals("{\"profile\":\"short-text\",\"languages\":[{\"language\":\"de\",\"probability\":0.9999968539079941}]}", response.toString());
    } finally {
        stopCluster();
    }
}
 
开发者ID:jprante,项目名称:elasticsearch-plugin-bundle,代码行数:23,代码来源:SimpleHttpTest.java

示例9: copyToBytesFromClasspath

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
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());
        }
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:12,代码来源:StreamsUtils.java

示例10: sendFiles

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
void sendFiles(Store store, StoreFileMetaData[] files, Function<StoreFileMetaData, OutputStream> outputStreamFactory) throws Exception {
    store.incRef();
    try {
        ArrayUtil.timSort(files, (a, b) -> Long.compare(a.length(), b.length())); // send smallest first
        for (int i = 0; i < files.length; i++) {
            final StoreFileMetaData md = files[i];
            try (IndexInput indexInput = store.directory().openInput(md.name(), IOContext.READONCE)) {
                // it's fine that we are only having the indexInput in the try/with block. The copy methods handles
                // exceptions during close correctly and doesn't hide the original exception.
                Streams.copy(new InputStreamIndexInput(indexInput, md.length()), outputStreamFactory.apply(md));
            } catch (Exception e) {
                final IOException corruptIndexException;
                if ((corruptIndexException = ExceptionsHelper.unwrapCorruption(e)) != null) {
                    if (store.checkIntegrityNoException(md) == false) { // we are corrupted on the primary -- fail!
                        logger.warn("{} Corrupted file detected {} checksum mismatch", shardId, md);
                        failEngine(corruptIndexException);
                        throw corruptIndexException;
                    } else { // corruption has happened on the way to replica
                        RemoteTransportException exception = new RemoteTransportException("File corruption occurred on recovery but " +
                                "checksums are ok", null);
                        exception.addSuppressed(e);
                        logger.warn(
                            (org.apache.logging.log4j.util.Supplier<?>) () -> new ParameterizedMessage(
                                "{} Remote file corruption on node {}, recovering {}. local checksum OK",
                                shardId,
                                request.targetNode(),
                                md),
                            corruptIndexException);
                        throw exception;
                    }
                } else {
                    throw e;
                }
            }
        }
    } finally {
        store.decRef();
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:40,代码来源:RecoverySourceHandler.java

示例11: taskResultIndexMapping

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
public String taskResultIndexMapping() {
    try (InputStream is = getClass().getResourceAsStream(TASK_RESULT_INDEX_MAPPING_FILE)) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        Streams.copy(is, out);
        return out.toString(IOUtils.UTF_8);
    } catch (Exception e) {
        logger.error(
            (Supplier<?>) () -> new ParameterizedMessage(
                "failed to create tasks results index template [{}]", TASK_RESULT_INDEX_MAPPING_FILE), e);
        throw new IllegalStateException("failed to create tasks results index template [" + TASK_RESULT_INDEX_MAPPING_FILE + "]", e);
    }

}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:14,代码来源:TaskResultsService.java

示例12: readSnapshotIndexLatestBlob

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
long readSnapshotIndexLatestBlob() throws IOException {
    try (InputStream blob = snapshotsBlobContainer.readBlob(INDEX_LATEST_BLOB)) {
        BytesStreamOutput out = new BytesStreamOutput();
        Streams.copy(blob, out);
        return Numbers.bytesToLong(out.bytes().toBytesRef());
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:8,代码来源:BlobStoreRepository.java

示例13: uncompress

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
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();
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:8,代码来源:CompressorFactory.java

示例14: writeBlob

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
@Override
public void writeBlob(String blobName, InputStream inputStream, long blobSize) throws IOException {
    if (blobExists(blobName)) {
        throw new FileAlreadyExistsException("blob [" + blobName + "] already exists, cannot overwrite");
    }
    final Path file = path.resolve(blobName);
    try (OutputStream outputStream = Files.newOutputStream(file, StandardOpenOption.CREATE_NEW)) {
        Streams.copy(inputStream, outputStream, new byte[blobStore.bufferSizeInBytes()]);
    }
    IOUtils.fsync(file, false);
    IOUtils.fsync(path, true);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:13,代码来源:FsBlobContainer.java

示例15: createFileScripts

import org.elasticsearch.common.io.Streams; //导入方法依赖的package包/类
private void createFileScripts(String... langs) throws IOException {
    for (String lang : langs) {
        Path scriptPath = scriptsFilePath.resolve("file_script." + lang);
        Streams.copy("10".getBytes("UTF-8"), Files.newOutputStream(scriptPath));
    }
    resourceWatcherService.notifyNow();
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:8,代码来源:ScriptServiceTests.java


注:本文中的org.elasticsearch.common.io.Streams.copy方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。