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


Java IOUtils.closeQuietly方法代码示例

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


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

示例1: drainInputStream

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
public static byte[] drainInputStream(InputStream inputStream) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    try {
        byte[] buffer = new byte[1024];
        long bytesRead = 0;
        while ((bytesRead = inputStream.read(buffer)) > -1) {
            byteArrayOutputStream.write(buffer, 0, (int) bytesRead);
        }
        return byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(byteArrayOutputStream, null);
    }
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:17,代码来源:ApacheDefaultHttpRequestFactoryTest.java

示例2: drainInputStream

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
/**
 * Reads to the end of the inputStream returning a byte array of the contents
 * 
 * @param inputStream
 *            InputStream to drain
 * @return Remaining data in stream as a byte array
 */
public static byte[] drainInputStream(InputStream inputStream) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    try {
        byte[] buffer = new byte[1024];
        long bytesRead = 0;
        while ((bytesRead = inputStream.read(buffer)) > -1) {
            byteArrayOutputStream.write(buffer, 0, (int) bytesRead);
        }
        return byteArrayOutputStream.toByteArray();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(byteArrayOutputStream, null);
    }
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:24,代码来源:InputStreamUtils.java

示例3: putLocalObject

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
/**
 * Used for performance testing purposes only.
 */
private void putLocalObject(final UploadObjectRequest reqIn,
        OutputStream os) throws IOException {
    UploadObjectRequest req = reqIn.clone();

    final File fileOrig = req.getFile();
    final InputStream isOrig = req.getInputStream();

    if (isOrig == null) {
        if (fileOrig == null)
            throw new IllegalArgumentException("Either a file lor input stream must be specified");
        req.setInputStream(new FileInputStream(fileOrig));
        req.setFile(null);
    }

    try {
        IOUtils.copy(req.getInputStream(), os);
    } finally {
        cleanupDataSource(req, fileOrig, isOrig,
                req.getInputStream(), log);
        IOUtils.closeQuietly(os, log);
    }
    return;
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:27,代码来源:AmazonS3Client.java

示例4: close

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
/**
 * {@inheritDoc}
 *
 * Delegates to {@link S3ObjectInputStream#abort()} if there is any data
 * remaining in the stream. Otherwise, it safely closes the stream.
 *
 * @see {@link S3ObjectInputStream#abort()}
 */
@Override
public void close() throws IOException {
    if (bytesRead >= contentLength || eofReached) {
        super.close();
    } else {
        LOG.warn(
                "Not all bytes were read from the S3ObjectInputStream, aborting HTTP connection. This is likely an error and " +
                "may result in sub-optimal behavior. Request only the bytes you need via a ranged GET or drain the input " +
                "stream after use.");
        if (httpRequest != null) {
            httpRequest.abort();
        }
        IOUtils.closeQuietly(in, null);
    }
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:24,代码来源:S3AbortableInputStream.java

示例5: putLocalObjectSecurely

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
@Override
public final void putLocalObjectSecurely(final UploadObjectRequest reqIn,
        String uploadId, OutputStream os) throws IOException {
    UploadObjectRequest req = reqIn.clone();

    final File fileOrig = req.getFile();
    final InputStream isOrig = req.getInputStream();

    final T uploadContext = multipartUploadContexts.get(uploadId);
    ContentCryptoMaterial cekMaterial = uploadContext.getContentCryptoMaterial();
    req = wrapWithCipher(req, cekMaterial);

    try {
        IOUtils.copy(req.getInputStream(), os);
        // so it won't crap out with a false negative at the end; (Not
        // really relevant here)
        uploadContext.setHasFinalPartBeenSeen(true);
    } finally {
        cleanupDataSource(req, fileOrig, isOrig,
                req.getInputStream(), log);
        IOUtils.closeQuietly(os, log);
    }
    return;
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:25,代码来源:S3CryptoModuleBase.java

示例6: toSessionItem

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
@Override
public DynamoSessionItem toSessionItem(Session session) {
    ObjectOutputStream oos = null;
    try {
        ByteArrayOutputStream fos = new ByteArrayOutputStream();
        oos = new ObjectOutputStream(fos);
        ((StandardSession) session).writeObjectData(oos);
        oos.close();
        DynamoSessionItem sessionItem = new DynamoSessionItem(session.getIdInternal());
        sessionItem.setSessionData(ByteBuffer.wrap(fos.toByteArray()));
        return sessionItem;
    } catch (Exception e) {
        IOUtils.closeQuietly(oos, null);
        throw new SessionConversionException("Unable to convert Tomcat Session into Dynamo storage representation",
                e);
    }
}
 
开发者ID:amazon-archives,项目名称:aws-dynamodb-session-tomcat,代码行数:18,代码来源:DefaultDynamoSessionItemConverter.java

示例7: loadPartitionFromStream

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
private Partitions loadPartitionFromStream(InputStream stream, String location) {

        try {

            return mapper.readValue(stream, Partitions.class);

        } catch (IOException e) {
            throw new SdkClientException("Error while loading partitions " +
                    "file from " + location, e);
        } finally {
            IOUtils.closeQuietly(stream, null);
        }
    }
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:14,代码来源:PartitionsLoader.java

示例8: loadOverrideMetadataIfExists

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
private RegionMetadata loadOverrideMetadataIfExists() {
    RegionMetadata metadata = loadFromSystemProperty();

    if (metadata == null) {
        InputStream override = RegionUtils.class
                .getResourceAsStream(OVERRIDE_ENDPOINTS_RESOURCE_PATH);
        if (override != null) {
            metadata = loadFromStream(override);
            IOUtils.closeQuietly(override, LOG);
        }
    }

    return metadata;
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:15,代码来源:LegacyRegionXmlMetadataBuilder.java

示例9: release

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
@Override
public final void release() {
    // Don't call IOUtils.release(in, null) or else could lead to infinite loop
    IOUtils.closeQuietly(this, null);
    if (out instanceof Releasable) {
        // This allows any underlying stream that has the close operation
        // disabled to be truly released
        Releasable r = (Releasable)out;
        r.release();
    }
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:12,代码来源:SdkFilterOutputStream.java

示例10: parseErrorCode

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
@Override
public String parseErrorCode(HttpResponse response, JsonContent jsonContents) {
    IonReader reader = ionSystem.newReader(jsonContents.getRawContent());
    try {
        IonType type = reader.next();
        if (type != IonType.STRUCT) {
            throw new SdkClientException(String.format("Can only get error codes from structs (saw %s), request id %s", type, getRequestId(response)));
        }

        boolean errorCodeSeen = false;
        String errorCode = null;
        String[] annotations = reader.getTypeAnnotations();
        for (String annotation : annotations) {
            if (annotation.startsWith(TYPE_PREFIX)) {
                if (errorCodeSeen) {
                    throw new SdkClientException(String.format("Multiple error code annotations found for request id %s", getRequestId(response)));
                } else {
                    errorCodeSeen = true;
                    errorCode = annotation.substring(TYPE_PREFIX.length());
                }
            }
        }

        return errorCode;
    } finally {
        IOUtils.closeQuietly(reader, log);
    }
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:29,代码来源:IonErrorCodeParser.java

示例11: readResource

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
/**
 * Connects to the given endpoint to read the resource
 * and returns the text contents.
 *
 * @param endpoint
 *            The service endpoint to connect to.
 *
 * @param retryPolicy
 *            The custom retry policy that determines whether a
 *            failed request should be retried or not.
 *
 * @return The text payload returned from the Amazon EC2 endpoint
 *         service for the specified resource path.
 *
 * @throws IOException
 *             If any problems were encountered while connecting to the
 *             service for the requested resource path.
 * @throws SdkClientException
 *             If the requested service is not found.
 */
public String readResource(URI endpoint, CredentialsEndpointRetryPolicy retryPolicy) throws IOException {
    int retriesAttempted = 0;
    InputStream inputStream = null;

    while (true) {
        try {
            HttpURLConnection connection = connectionUtils.connectToEndpoint(endpoint);

            int statusCode = connection.getResponseCode();

            if (statusCode == HttpURLConnection.HTTP_OK) {
                inputStream = connection.getInputStream();
                return IOUtils.toString(inputStream);
            } else if (statusCode == HttpURLConnection.HTTP_NOT_FOUND) {
                // This is to preserve existing behavior of EC2 Instance metadata service.
                throw new SdkClientException("The requested metadata is not found at " + connection.getURL());
            } else {
                if (!retryPolicy.shouldRetry(retriesAttempted++, CredentialsEndpointRetryParameters.builder().withStatusCode(statusCode).build())) {
                    inputStream = connection.getErrorStream();
                    handleErrorResponse(inputStream, statusCode, connection.getResponseMessage());
                }
            }
        } catch (IOException ioException) {
            if (!retryPolicy.shouldRetry(retriesAttempted++, CredentialsEndpointRetryParameters.builder().withException(ioException).build())) {
                throw ioException;
            }
            LOG.debug("An IOException occured when connecting to service endpoint: " + endpoint  + "\n Retrying to connect again.");
        } finally {
            IOUtils.closeQuietly(inputStream, LOG);
        }
    }

}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:54,代码来源:EC2CredentialsUtils.java

示例12: release

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
/**
 * WARNING: Subclass that overrides this method must NOT call
 * super.release() or else it would lead to infinite loop.
 * <p>
 * {@inheritDoc}
 */
@Override
public void release() {
    // Don't call IOUtils.release(in, null) or else could lead to infinite loop
    IOUtils.closeQuietly(this, null);
    InputStream in = getWrappedInputStream();
    if (in instanceof Releasable) {
        // This allows any underlying stream that has the close operation
        // disabled to be truly released
        Releasable r = (Releasable)in;
        r.release();
    }
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:19,代码来源:SdkInputStream.java

示例13: doesStreamEqualStream

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
/**
 * Returns true if, and only if, the contents read from the specified input streams are exactly
 * equal. Both input streams will be closed at the end of this method.
 *
 * @param expected
 *            The input stream containing the expected contents.
 * @param inputStream
 *            The stream that will be read, compared to the expected file contents, and finally
 *            closed.
 * @return True if the two input streams contain the same data.
 * @throws IOException
 *             If any problems are encountered comparing the file and stream.
 */
public static boolean doesStreamEqualStream(InputStream expected, InputStream actual) throws IOException {

    try {
        final byte[] expectedDigest = InputStreamUtils.calculateMD5Digest(expected);
        final byte[] actualDigest = InputStreamUtils.calculateMD5Digest(actual);

        return Arrays.equals(expectedDigest, actualDigest);
    } catch (NoSuchAlgorithmException nse) {
        throw new AmazonClientException(nse.getMessage(), nse);
    } finally {
        IOUtils.closeQuietly(expected, null);
        IOUtils.closeQuietly(actual, null);
    }
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:28,代码来源:SdkAsserts.java

示例14: getObjectAsString

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
@Override
public String getObjectAsString(String bucketName, String key)
        throws AmazonServiceException, SdkClientException {
    rejectNull(bucketName, "Bucket name must be provided");
    rejectNull(key, "Object key must be provided");

    S3Object object = getObject(bucketName, key);
    try {
        return IOUtils.toString(object.getObjectContent());
    } catch (IOException e) {
        throw new SdkClientException("Error streaming content from S3 during download");
    } finally {
        IOUtils.closeQuietly(object, log);
    }
}
 
开发者ID:IBM,项目名称:ibm-cos-sdk-java,代码行数:16,代码来源:AmazonS3Client.java

示例15: testTransferManager

import com.amazonaws.util.IOUtils; //导入方法依赖的package包/类
@Test
public void testTransferManager() throws Exception {
    AmazonS3Client client = new AmazonS3Client(awsCreds,
            new ClientConfiguration().withSignerOverride("S3SignerType"));
    client.setEndpoint(s3Endpoint.toString());
    client.setS3ClientOptions(new S3ClientOptions().withPathStyleAccess(true));

    int numStreams = 2;
    int numUploadThreads = 2;
    int queueCapacity = 2;
    int partSize = 5;
    final StreamTransferManager manager = new StreamTransferManager(containerName, key, client, numStreams,
            numUploadThreads, queueCapacity, partSize) {

        @Override
        public void customiseUploadPartRequest(UploadPartRequest request) {
            /*
            Workaround from https://github.com/andrewgaul/s3proxy/commit/50a302436271ec46ce81a415b4208b9e14fcaca4
            to deal with https://github.com/andrewgaul/s3proxy/issues/80
             */
            ObjectMetadata metadata = new ObjectMetadata();
            metadata.setContentType("application/unknown");
            request.setObjectMetadata(metadata);
        }
    };
    final List<MultiPartOutputStream> streams = manager.getMultiPartOutputStreams();
    List<StringBuilder> builders = new ArrayList<StringBuilder>(numStreams);
    ExecutorService pool = Executors.newFixedThreadPool(numStreams);
    for (int i = 0; i < numStreams; i++) {
        final int streamIndex = i;
        final StringBuilder builder = new StringBuilder();
        builders.add(builder);
        Runnable task = new Runnable() {
            @Override
            public void run() {
                MultiPartOutputStream outputStream = streams.get(streamIndex);
                for (int lineNum = 0; lineNum < 1000000; lineNum++) {
                    String line = String.format("Stream %d, line %d\n", streamIndex, lineNum);
                    outputStream.write(line.getBytes());
                    try {
                        outputStream.checkSize();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    builder.append(line);
                }
                outputStream.close();
            }
        };
        pool.submit(task);
    }
    pool.shutdown();
    pool.awaitTermination(5, TimeUnit.SECONDS);
    manager.complete();

    for (int i = 1; i < numStreams; i++) {
        builders.get(0).append(builders.get(i));
    }

    String expectedResult = builders.get(0).toString();

    S3ObjectInputStream objectContent = client.getObject(containerName, key).getObjectContent();
    String result = IOUtils.toString(objectContent);
    IOUtils.closeQuietly(objectContent, null);

    Assert.assertEquals(expectedResult, result);
}
 
开发者ID:alexmojaki,项目名称:s3-stream-upload,代码行数:68,代码来源:StreamTransferManagerTest.java


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