Example usage for org.apache.commons.io.output NullOutputStream nullOutputStream

List of usage examples for org.apache.commons.io.output NullOutputStream nullOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.io.output NullOutputStream nullOutputStream.

Prototype

public static OutputStream nullOutputStream() 

Source Link

Document

Returns a new OutputStream which discards all bytes.

Usage

From source file:org.dataconservancy.packaging.tool.model.ipm.ChecksumCalculatingInputStreamTest.java

@Test
public void testCorrectChecksum() throws Exception {
    final String expectedSha1 = "bae5ed658ab3546aee12f23f36392f35dba1ebdd";
    final String expectedMd5 = "ce90a5f32052ebbcd3b20b315556e154";
    final InputStream data = this.getClass().getResourceAsStream("/fox.txt");

    MessageDigest sha1 = MessageDigest.getInstance("SHA-1");
    MessageDigest md5 = MessageDigest.getInstance("MD5");
    FileInfo.ChecksumCalculatingInputStream underTest = new FileInfo.ChecksumCalculatingInputStream(data,
            Arrays.asList(md5, sha1));

    IOUtils.copy(underTest, new NullOutputStream());

    underTest.close();/*from   w w w. j ava  2  s . c  o m*/

    Map<MessageDigest, byte[]> digests = underTest.digests();
    assertEquals(expectedSha1, Hex.encodeHexString(digests.get(sha1)));
    assertEquals(expectedMd5, Hex.encodeHexString(digests.get(md5)));
}

From source file:org.dataconservancy.packaging.tool.model.ipm.FileInfo.java

/**
 * Default constructor that should be used in most cases. Will read the file at the path location and load the necessary file attributes.
 * @param path The path to the file.// w  ww .  ja  va  2s.  co m
 */
public FileInfo(Path path) {
    location = path.toUri();
    name = path.getFileName().toString();

    try {
        fileAttributes = new FileInfoAttributes(Files.readAttributes(path, BasicFileAttributes.class));
        if (fileAttributes.isRegularFile()) {
            checksums = new HashMap<>();
            formats = new ArrayList<>();

            MessageDigest md5 = null;
            MessageDigest sha1 = null;

            try {
                md5 = MessageDigest.getInstance("MD5");
                sha1 = MessageDigest.getInstance("SHA-1");
            } catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e.getMessage(), e);
            }

            try (ChecksumCalculatingInputStream in = new ChecksumCalculatingInputStream(
                    Files.newInputStream(path), Arrays.asList(md5, sha1))) {

                IOUtils.copy(in, new NullOutputStream());

                in.digests().forEach((md, digest) -> {
                    String encoded = Hex.encodeHexString(digest);
                    if (md.getAlgorithm().equals("MD5")) {
                        checksums.put(Algorithm.MD5, encoded);
                    }

                    if (md.getAlgorithm().equals("SHA-1")) {
                        checksums.put(Algorithm.SHA1, encoded);
                    }
                });
            }

            List<DetectedFormat> fileFormats = ContentDetectionService.getInstance()
                    .detectFormats(path.toFile());
            for (DetectedFormat format : fileFormats) {
                if (format.getId() != null && !format.getId().isEmpty()) {
                    formats.add(createFormatURIString(format));
                }

                if (format.getMimeType() != null && !format.getMimeType().isEmpty()) {
                    formats.add(format.getMimeType());
                }
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:org.dataconservancy.ui.dcpmap.DataSetMapper.java

/**
 * Calculates fixity over an InputStream according to the supplied algorithm.
 *
 * @param in         the InputStream//from   ww  w . j a va 2  s. c  o m
 * @param digestAlgo the algorithm used to calculate fixity
 * @return a DcsFixity object encapsulating the calculated fixity and algorithm
 * @throws IOException if there is an error calculating the fixity
 */
private DcsFixity calculateFixity(InputStream in, MessageDigest digestAlgo) throws IOException {
    final HexEncodingDigestListener digestListener = new HexEncodingDigestListener();
    final NullOutputStream devNull = new NullOutputStream();
    final DigestNotificationStream digestIn = new DigestNotificationStream(in, digestAlgo, digestListener);

    IOUtils.copy(digestIn, devNull);

    final String digest = digestListener.getDigest();

    if (digest == null || digest.isEmpty()) {
        throw new IOException("Error calculating fixity for stream: the digest was empty or null.");
    }

    DcsFixity fixity = new DcsFixity();
    fixity.setAlgorithm(digestAlgo.getAlgorithm());
    fixity.setValue(digest);

    return fixity;
}

From source file:org.dhatim.fastexcel.Benchmarks.java

private void poiPopulate(org.apache.poi.ss.usermodel.Workbook wb) throws Exception {
    Sheet ws = wb.createSheet("Sheet 1");
    CellStyle dateStyle = wb.createCellStyle();
    dateStyle.setDataFormat(wb.getCreationHelper().createDataFormat().getFormat("yyyy-mm-dd hh:mm:ss"));
    for (int r = 0; r < NB_ROWS; ++r) {
        Row row = ws.createRow(r);//from  ww w .j a v  a2s  .co m
        row.createCell(0).setCellValue(r);
        row.createCell(1).setCellValue(Integer.toString(r % 1000));
        row.createCell(2).setCellValue(r / 87.0);
        Cell c = row.createCell(3);
        c.setCellStyle(dateStyle);
        c.setCellValue(new Date());
    }
    wb.write(new NullOutputStream());
}

From source file:org.dhatim.fastexcel.Benchmarks.java

@Benchmark
public void fastExcel() throws Exception {
    Workbook wb = new Workbook(new NullOutputStream(), "Perf", "1.0");
    Worksheet ws = wb.newWorksheet("Sheet 1");
    for (int r = 0; r < NB_ROWS; ++r) {
        ws.value(r, 0, r);// w  ww .j  ava  2  s  . co m
        ws.value(r, 1, Integer.toString(r % 1000));
        ws.value(r, 2, r / 87.0);
        ws.value(r, 3, new Date());
    }
    ws.range(0, 3, NB_ROWS - 1, 3).style().format("yyyy-mm-dd hh:mm:ss").set();
    wb.finish();
}

From source file:org.dhatim.fastexcel.Correctness.java

@Test(expected = IllegalArgumentException.class)
public void badVersion() throws Exception {
    Workbook dummy = new Workbook(new NullOutputStream(), "Test", "1.0.1");
}

From source file:org.dita.dost.ant.PluginInstallTask.java

private String getFileHash(final File file) {
    try (DigestInputStream digestInputStream = new DigestInputStream(
            new BufferedInputStream(new FileInputStream(file)), MessageDigest.getInstance("SHA-256"))) {
        IOUtils.copy(digestInputStream, new NullOutputStream());
        final MessageDigest digest = digestInputStream.getMessageDigest();
        final byte[] sha256 = digest.digest();
        return printHexBinary(sha256);
    } catch (NoSuchAlgorithmException e) {
        throw new IllegalArgumentException(e);
    } catch (IOException e) {
        throw new BuildException("Failed to calculate file checksum: " + e.getMessage(), e);
    }//  ww  w.  ja v a  2 s.co  m
}

From source file:org.docx4j.convert.out.fo.ApacheFORenderer.java

/**
 * For first pass of two pass process, invoke org.apache.fop.apps.FormattingResults which can tell us the number of pages 
 * in each page sequence, and in the document as a whole.
 * //  w w w . j  a v a  2s  .co  m
 * @param fopFactory
 * @param outputFormat
 * @param foDocumentSrc
 * @param placeholderLookup
 * @return
 * @throws Docx4JException
 */
protected FormattingResults calcResults(FopFactory fopFactory, String outputFormat, Source foDocumentSrc,
        PlaceholderReplacementHandler.PlaceholderLookup placeholderLookup) throws Docx4JException {
    Fop fop = null;
    Result result = null;
    try {
        fop = fopFactory.newFop(outputFormat, new NullOutputStream());
        result = new SAXResult(new PlaceholderReplacementHandler(fop.getDefaultHandler(), placeholderLookup));
    } catch (FOPException e) {
        throw new Docx4JException("Exception setting up result for fo transformation: " + e.getMessage(), e);
    }

    Transformer transformer;
    try {
        transformer = XmlUtils.getTransformerFactory().newTransformer();
        transformer.transform(foDocumentSrc, result);
    } catch (TransformerConfigurationException e) {
        throw new Docx4JException("Exception setting up transformer: " + e.getMessage(), e);
    } catch (TransformerException e) {
        throw new Docx4JException("Exception executing transformer: " + e.getMessage(), e);
    }
    return fop.getResults();
}

From source file:org.docx4j.convert.out.pdf.viaXSLFO.FopResultUtils.java

public static FormattingResults calcResults(String userConfiguration, String outputFormat, Document document,
        Templates xslt, Map parameters) throws Docx4JException {

    Fop fop = null;//from   w  ww .  j a  v  a2  s .c o  m
    Result result = null;
    try {
        fop = FopFactoryUtil.createFop(userConfiguration, outputFormat, new NullOutputStream());
        result = new SAXResult(fop.getDefaultHandler());
    } catch (FOPException e) {
        throw new Docx4JException("Exception setting up result for fo transformation: " + e.getMessage(), e);
    }
    XmlUtils.transform(document, xslt, parameters, result);
    return fop.getResults();
}

From source file:org.docx4j.convert.out.pdf.viaXSLFO.FopResultUtils.java

public static FormattingResults calcResults(String userConfiguration, String outputFormat, Source foDocumentSrc,
        Map<String, Object> parameters) throws Docx4JException {

    Fop fop = null;/*from   www  . j  a  va2  s .  c  o  m*/
    Result result = null;
    try {
        fop = FopFactoryUtil.createFop(userConfiguration, outputFormat, new NullOutputStream());
        result = new SAXResult(fop.getDefaultHandler());
    } catch (FOPException e) {
        throw new Docx4JException("Exception setting up result for fo transformation: " + e.getMessage(), e);
    }

    Transformer transformer;
    try {
        transformer = XmlUtils.getTransformerFactory().newTransformer();
        setupParameters(transformer, parameters);
        transformer.transform(foDocumentSrc, result);
    } catch (TransformerConfigurationException e) {
        throw new Docx4JException("Exception setting up transformer: " + e.getMessage(), e);
    } catch (TransformerException e) {
        throw new Docx4JException("Exception executing transformer: " + e.getMessage(), e);
    }
    return fop.getResults();
}