Refine search
/** * Computes the checksum of a file using the specified checksum object. * Multiple files may be checked using one <code>Checksum</code> instance * if desired simply by reusing the same checksum object. * For example: * <pre> * long csum = FileUtils.checksum(file, new CRC32()).getValue(); * </pre> * * @param file the file to checksum, must not be <code>null</code> * @param checksum the checksum object to be used, must not be <code>null</code> * @return the checksum specified, updated with the content of the file * @throws NullPointerException if the file or checksum is <code>null</code> * @throws IllegalArgumentException if the file is a directory * @throws IOException if an IO error occurs reading the file * @since Commons IO 1.3 */ public static Checksum checksum(File file, Checksum checksum) throws IOException { if (file.isDirectory()) { throw new IllegalArgumentException("Checksums can't be computed on directories"); } InputStream in = null; try { in = new CheckedInputStream(new FileInputStream(file), checksum); IOUtils.copy(in, new NullOutputStream()); } finally { IOUtils.closeQuietly(in); } return checksum; }
protected static boolean matchChecksum(File localFile, String expectedSignature) { try (FileInputStream input = new FileInputStream(localFile)) { MessageDigest digester = MessageDigest.getInstance("MD5"); try (DigestInputStream digest = new DigestInputStream(input, digester)) { IOUtils.copy(digest, new NullOutputStream()); } return expectedSignature.equalsIgnoreCase(encodeHexString(digester.digest())); } catch (Exception e) { throw new RuntimeException(e); } }
void importDietMatrices(String archiveURL, DietMatrixListener matrix) throws StudyImporterException { try { InputStream inputStream = getDataset().getResource(archiveURL); ZipInputStream zipInputStream = new ZipInputStream(inputStream); ZipEntry entry; while ((entry = zipInputStream.getNextEntry()) != null) { if (entry.getName().matches("WEB.*\\.csv$")) { matrix.onMatrix(IOUtils.toString(zipInputStream, StandardCharsets.UTF_8)); } else { IOUtils.copy(zipInputStream, new NullOutputStream()); } } IOUtils.closeQuietly(zipInputStream); } catch (IOException e) { throw new StudyImporterException(e); } } }
public void hash() throws IOException { DigestOutputStream os = null; try { os = new DigestOutputStream(new NullOutputStream(), MessageDigest.getInstance("SHA-256")); copyFile(os); os.close(); revision = Hex.encodeHexString(os.getMessageDigest().digest()); os = null; } catch (NoSuchAlgorithmException e) { throw new IOException(e); } finally { IOUtils.closeQuietly(os); } }
IOUtils.copy(inputStream, new NullOutputStream()); continue; IOUtils.copy(inputStream, new NullOutputStream()); continue; final byte[] oldClassBytes = IOUtils.toByteArray(newInputStream);
private static String getIdForInputStream(final InputStream in) throws NoSuchAlgorithmException, IOException { MessageDigest digest = MessageDigest.getInstance("SHA-1"); OutputStream output = new DigestOutputStream(new NullOutputStream(), digest); try { IOUtils.copyLarge(in, output); } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(in); } return encodeHexString(digest.digest()); }
protected void drain(HttpURLConnection con) throws IOException { if (con.getResponseCode() >= 500) { String error = IOUtils.toString(con.getErrorStream()); con.getErrorStream().close(); throw new IOException("Error received:" + con.getResponseCode() + "\n" + error); } else { IOUtils.copy(con.getInputStream(),new NullOutputStream()); con.getInputStream().close(); } }
@Test public void testCopy_inputStreamToOutputStream_IO84() throws Exception { final long size = (long)Integer.MAX_VALUE + (long)1; final InputStream in = new NullInputStream(size); final OutputStream out = new NullOutputStream(); // Test copy() method assertEquals(-1, IOUtils.copy(in, out)); // reset the input in.close(); // Test copyLarge() method assertEquals("copyLarge()", size, IOUtils.copyLarge(in, out)); }
protected String digest(String permissionsSegment) { try { MessageDigest digest = DigestUtils.getSha256Digest(); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new DigestOutputStream(new NullOutputStream(), digest)); outputStreamWriter.write(getClass().getSimpleName()); outputStreamWriter.write("$"); outputStreamWriter.write(name()); outputStreamWriter.write("/"); outputStreamWriter.write(permissionsSegment); outputStreamWriter.write("["); for (GoDashboardPipeline pipeline : allPipelines()) { outputStreamWriter.write(pipeline.cacheSegment()); outputStreamWriter.write(","); } outputStreamWriter.write("]"); outputStreamWriter.flush(); return Hex.encodeHexString(digest.digest()); } catch (IOException e) { throw new UncheckedIOException(e); } } }
public static String calculate(String id) { if (id.isEmpty()) { return null; } NullOutputStream nullOut = new NullOutputStream(); DigestOutputStream digestOut = null; try { digestOut = new DigestOutputStream(nullOut, MessageDigest.getInstance("MD5")); IOUtils.write(id, digestOut); digestOut.close(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage(), e); } return digestToHexString(digestOut.getMessageDigest().digest()); }
@Test public void testLargeFiles_IO84() throws Exception { final long size = (long)Integer.MAX_VALUE + (long)1; final NullInputStream mock = new NullInputStream(size); final OutputStream nos = new NullOutputStream(); final CountingOutputStream cos = new CountingOutputStream(nos); // Test integer methods IOUtils.copyLarge(mock, cos); try { cos.getCount(); fail("Expected getCount() to throw an ArithmeticException"); } catch (final ArithmeticException ae) { // expected result } try { cos.resetCount(); fail("Expected resetCount() to throw an ArithmeticException"); } catch (final ArithmeticException ae) { // expected result } mock.close(); // Test long methods IOUtils.copyLarge(mock, cos); assertEquals("getByteCount()", size, cos.getByteCount()); assertEquals("resetByteCount()", size, cos.resetByteCount()); }
private void copy(final ByteArrayOutputStream baos, final InputStream is) throws IOException { final NullOutputStream looseIt = new NullOutputStream(); long byteSize = 0; final byte[] buffer = new byte[2048]; int length; while ((length = is.read(buffer)) != -1) { byteSize += length; if (isTooLarge(byteSize)) { baos.close(); // we need to consume the rest of the payload in order to reply. looseIt.write(buffer, 0, length); } else { baos.write(buffer, 0, length); } } if (isTooLarge(byteSize)) { LOGGER.info(ProxyCodes.PAYLOAD_TOO_LARGE, "Aborting payload read: {0} - Cached request cannot exceed: {1}MB - Was: {2}MB", getIn().getRequestUri(), SizeUnit.BYTES.toMegabytes(MAX_FILE_UPLOAD_SIZE), SizeUnit.BYTES.toMegabytes(byteSize)); throw new PayloadTooLargeException("Aborting payload read. Cached request cannot exceed: " + SizeUnit.BYTES.toMegabytes(MAX_FILE_UPLOAD_SIZE) + "MB"); } }
@Override public void close() throws IOException { super.close(); if (!closed) { executorLogger.finishProcess(processInfo); closed = true; } }
public String getMd5() { try (InputStream input = source.getInputStream()) { MessageDigest digester = MessageDigest.getInstance("MD5"); try (DigestInputStream digest = new DigestInputStream(input, digester)) { IOUtils.copy(digest, new NullOutputStream()); } return Hex.encodeHexString(digester.digest()).toLowerCase(); } catch (Exception e) { throw new RuntimeException(e); } }
/** * Retrieves the {@link Fingerprint} for a specific credential. * * @param c the credential. * @return the {@link Fingerprint} or {@code null} if the credential has no fingerprint associated with it. * @throws IOException if the credential's fingerprint hash could not be computed. * @since 2.1.1 */ @CheckForNull public static Fingerprint getFingerprintOf(@NonNull Credentials c) throws IOException { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); DigestOutputStream out = new DigestOutputStream(new NullOutputStream(), md5); try { FINGERPRINT_XML.toXML(c, new OutputStreamWriter(out, Charset.forName("UTF-8"))); } finally { IOUtils.closeQuietly(out); } return Jenkins.getActiveInstance().getFingerprintMap().get(Util.toHexString(md5.digest())); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException("JLS mandates MD5 as a supported digest algorithm"); } }
private String getIdForInputStream(final InputStream in) throws Exception { MessageDigest digest = MessageDigest.getInstance("SHA-256"); OutputStream output = new DigestOutputStream(new NullOutputStream(), digest); try { IOUtils.copyLarge(in, output); } finally { IOUtils.closeQuietly(output); IOUtils.closeQuietly(in); } return encodeHexString(digest.digest()); }
protected void execSql(String sqlScriptFile) throws IOException { CommandBuilder builder = new CommandBuilder(conf, url, driver, userName, passWord, sqlScriptFile); // run the script using SqlLine SqlLine sqlLine = new SqlLine(); ByteArrayOutputStream outputForLog = null; if (!verbose) { OutputStream out; if (LOG.isDebugEnabled()) { out = outputForLog = new ByteArrayOutputStream(); } else { out = new NullOutputStream(); } sqlLine.setOutputStream(new PrintStream(out)); System.setProperty("sqlline.silent", "true"); } LOG.info("Going to run command <" + builder.buildToLog() + ">"); SqlLine.Status status = sqlLine.begin(builder.buildToRun(), null, false); if (LOG.isDebugEnabled() && outputForLog != null) { LOG.debug("Received following output from Sqlline:"); LOG.debug(outputForLog.toString("UTF-8")); } if (status != SqlLine.Status.OK) { throw new IOException("Schema script failed, errorcode " + status); } }
/** * Calculates a MD5 digest over the list of entity ids, suitable for use as an * ETag. If the list is empty, {@code null} is returned. * * @param entityIds the entities * @return the MD5 digest, or {@code null} if {@code entityIds} is empty. */ public static String calculateDigest(List<String> entityIds) { if (entityIds.isEmpty()) { return null; } NullOutputStream nullOut = new NullOutputStream(); DigestOutputStream digestOut = null; try { digestOut = new DigestOutputStream(nullOut, MessageDigest.getInstance("MD5")); for (String id : entityIds) { IOUtils.write(id, digestOut); } digestOut.close(); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage(), e); } return digestToHexString(digestOut.getMessageDigest().digest()); }
@Test public void testLargeFiles_IO84() throws Exception { final long size = (long)Integer.MAX_VALUE + (long)1; final NullInputStream mock = new NullInputStream(size); final CountingInputStream cis = new CountingInputStream(mock); final OutputStream out = new NullOutputStream(); // Test integer methods IOUtils.copyLarge(cis, out); try { cis.getCount(); fail("Expected getCount() to throw an ArithmeticException"); } catch (final ArithmeticException ae) { // expected result } try { cis.resetCount(); fail("Expected resetCount() to throw an ArithmeticException"); } catch (final ArithmeticException ae) { // expected result } mock.close(); // Test long methods IOUtils.copyLarge(cis, out); assertEquals("getByteCount()", size, cis.getByteCount()); assertEquals("resetByteCount()", size, cis.resetByteCount()); }