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); } } }
private void closeQuietlyForRuntimeExceptions(Closeable c, Log log) { try { closeQuietly(c, log); } catch (RuntimeException e) { if (log.isDebugEnabled()) { log.debug("Unable to close closeable", e); } } }
@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(); } } }
/** * 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); } } }
private static String kotlinVersionByJar() { StringBuilder kotlinVersion = new StringBuilder(""); JarInputStream kotlinJar = null; try { Class<?> kotlinUnit = Class.forName("kotlin.Unit"); kotlinVersion.append("kotlin"); kotlinJar = new JarInputStream(kotlinUnit.getProtectionDomain().getCodeSource().getLocation().openStream()); String version = kotlinJar.getManifest().getMainAttributes().getValue("Implementation-Version"); concat(kotlinVersion, version, "/"); } catch (ClassNotFoundException e) { //Ignore } catch (Exception e) { if (log.isTraceEnabled()) { log.trace("Exception attempting to get Kotlin version.", e); } } finally { closeQuietly(kotlinJar, log); } return kotlinVersion.toString(); }
/** * Releases the given {@link Closeable} especially if it was an instance of * {@link Releasable}. * <p> * For example, the creation of a <code>ResettableInputStream</code> would entail * physically opening a file. If the opened file is meant to be closed only * (in a finally block) by the very same code block that created it, then it * is necessary that the release method must not be called while the * execution is made in other stack frames. * * In such case, as other stack frames may inadvertently or indirectly call * the close method of the stream, the creator of the stream would need to * explicitly disable the accidental closing via * <code>ResettableInputStream#disableClose()</code>, so that the release method * becomes the only way to truly close the opened file. */ public static void release(Closeable is, Log log) { closeQuietly(is, log); if (is instanceof Releasable) { Releasable r = (Releasable) is; r.release(); } }
/** * Loads the versionInfo.properties file from the AWS Java SDK and * stores the information so that the file doesn't have to be read the * next time the data is needed. */ private static void initializeVersion() { InputStream inputStream = ClassLoaderHelper.getResourceAsStream( VERSION_INFO_FILE, true, VersionInfoUtils.class); Properties versionInfoProperties = new Properties(); try { if (inputStream == null) throw new Exception(VERSION_INFO_FILE + " not found on classpath"); versionInfoProperties.load(inputStream); version = versionInfoProperties.getProperty("version"); platform = versionInfoProperties.getProperty("platform"); } catch (Exception e) { log.info("Unable to load version information for the running SDK: " + e.getMessage()); version = "unknown-version"; platform = "java"; } finally { closeQuietly(inputStream, log); } }
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; }
private JmesPathExpression getAstFromArgument(String argument, Map<String, JmesPathExpression> argumentToAstMap) throws IOException { if (argument != null && !argumentToAstMap.containsKey(argument)) { final Process p = executeToAstProcess(argument); if(p.exitValue()!= 0) { throw new RuntimeException(IOUtils.toString(p.getErrorStream())); } JsonNode jsonNode = mapper.readTree(IOUtils.toString(p.getInputStream())); JmesPathExpression ast = fromAstJsonToAstJava(jsonNode); argumentToAstMap.put(argument, ast); IOUtils.closeQuietly(p.getInputStream(), null); return ast; } else if (argument != null) { return argumentToAstMap.get(argument); } return null; }
/** * 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); } }
/** * 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(); } } }
@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); } }
public GetIntrospectionSchemaResult unmarshall(JsonUnmarshallerContext context) throws Exception { GetIntrospectionSchemaResult getIntrospectionSchemaResult = new GetIntrospectionSchemaResult(); java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { getIntrospectionSchemaResult.setSchema(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return getIntrospectionSchemaResult; }
public UpdateThingShadowResult unmarshall(JsonUnmarshallerContext context) throws Exception { UpdateThingShadowResult updateThingShadowResult = new UpdateThingShadowResult(); java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { updateThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return updateThingShadowResult; }
public DeleteThingShadowResult unmarshall(JsonUnmarshallerContext context) throws Exception { DeleteThingShadowResult deleteThingShadowResult = new DeleteThingShadowResult(); java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { deleteThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return deleteThingShadowResult; }
public GetThingShadowResult unmarshall(JsonUnmarshallerContext context) throws Exception { GetThingShadowResult getThingShadowResult = new GetThingShadowResult(); java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { getThingShadowResult.setPayload(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return getThingShadowResult; }
/** * 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; }
public GetSdkResult unmarshall(JsonUnmarshallerContext context) throws Exception { GetSdkResult getSdkResult = new GetSdkResult(); if (context.isStartOfDocument()) { if (context.getHeader("Content-Type") != null) { context.setCurrentHeader("Content-Type"); getSdkResult.setContentType(context.getUnmarshaller(String.class).unmarshall(context)); } if (context.getHeader("Content-Disposition") != null) { context.setCurrentHeader("Content-Disposition"); getSdkResult.setContentDisposition(context.getUnmarshaller(String.class).unmarshall(context)); } } java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { getSdkResult.setBody(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return getSdkResult; }
public GetExportResult unmarshall(JsonUnmarshallerContext context) throws Exception { GetExportResult getExportResult = new GetExportResult(); if (context.isStartOfDocument()) { if (context.getHeader("Content-Type") != null) { context.setCurrentHeader("Content-Type"); getExportResult.setContentType(context.getUnmarshaller(String.class).unmarshall(context)); } if (context.getHeader("Content-Disposition") != null) { context.setCurrentHeader("Content-Disposition"); getExportResult.setContentDisposition(context.getUnmarshaller(String.class).unmarshall(context)); } } java.io.InputStream is = context.getHttpResponse().getContent(); if (is != null) { try { getExportResult.setBody(java.nio.ByteBuffer.wrap(com.amazonaws.util.IOUtils.toByteArray(is))); } finally { com.amazonaws.util.IOUtils.closeQuietly(is, null); } } return getExportResult; }
@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; }