Example usage for java.io ByteArrayOutputStream write

List of usage examples for java.io ByteArrayOutputStream write

Introduction

In this page you can find the example usage for java.io ByteArrayOutputStream write.

Prototype

public synchronized void write(int b) 

Source Link

Document

Writes the specified byte to this ByteArrayOutputStream .

Usage

From source file:Main.java

public static byte[] readBytes(InputStream in, long skip, long size) throws IOException {
    ByteArrayOutputStream out = null;
    try {//from  ww w . j  a v a2 s  .c  om
        if (skip > 0) {
            long skipSize = 0;
            while (skip > 0 && (skipSize = in.skip(skip)) > 0) {
                skip -= skipSize;
            }
        }
        out = new ByteArrayOutputStream();
        for (int i = 0; i < size; i++) {
            out.write(in.read());
        }
    } finally {
        closeQuietly(out);
    }
    return out.toByteArray();
}

From source file:de.mat.utils.pdftools.PdfExtractEmptyPages.java

/**
 * <h4>FeatureDomain:</h4>//  w w w. j  a v a2 s .  c o m
 *     PublishingTools
 * <h4>FeatureDescription:</h4>
 *     reads readerOrig and adds pages to writerRemoved if empty, or to 
 *     writerTrimmed if not empty
 * <h4>FeatureResult:</h4>
 *   <ul>
 *     <li>updates writerTrimmed - add all pages which are not empty
 *     <li>updates writerRemoved - add all empty pages
 *   </ul> 
 * <h4>FeatureKeywords:</h4>
 *     PDF Publishing
 * @param origFileName - orig filename of the sourcepdf
 * @param readerOrig - reader of source
 * @param writerTrimmed - writer for trimmed pages
 * @param writerRemoved - writer for empty pages
 * @param flgTrim - ??
 * @return - count of trimmed pages
 * @throws Exception
 */
public static int addTrimmedPages(String origFileName, PdfReader readerOrig, PdfCopy writerTrimmed,
        PdfCopy writerRemoved, boolean flgTrim) throws Exception {
    PdfImportedPage page = null;
    int countTrimmedPages = 0;

    //loop each page
    for (int i = 1; i <= readerOrig.getNumberOfPages(); i++) {
        boolean flgIsEmpty = true;

        // get dictionary
        PdfDictionary pageDict = readerOrig.getPageN(i);

        // every pdf-version has its own way :-(
        char version = readerOrig.getPdfVersion();

        if (version == '3') {
            // PDF-Version: 3

            // examine the resource dictionary for /Font or
            // /XObject keys.  If either are present, they're almost
            // certainly actually used on the page -> not blank.
            PdfObject myObj = pageDict.get(PdfName.RESOURCES);
            PdfDictionary resDict = null;
            if (myObj instanceof PdfDictionary) {
                resDict = (PdfDictionary) myObj;
            } else {
                resDict = (PdfDictionary) PdfReader.getPdfObject(myObj);
            }
            if (resDict != null) {
                flgIsEmpty = resDict.get(PdfName.FONT) == null && resDict.get(PdfName.XOBJECT) == null;
                if (LOGGER.isInfoEnabled()) {
                    if (flgIsEmpty) {
                        LOGGER.info("probably empty page " + i + " Version: 1." + version
                                + " FONT/XOBJECT found in File:" + origFileName);
                    } else {
                        LOGGER.info("normal page " + i + " Version: 1." + version
                                + " no FONT/XOBJECT found in File:" + origFileName);
                    }
                }
            }
        } else if (version == '4') {
            // PDF-Version: 4
            // check the contentsize.

            // get the page content
            byte bContent[] = readerOrig.getPageContent(i);
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            // write the content to an output stream
            bs.write(bContent);

            flgIsEmpty = true;
            if (bs.size() > blankPdfsize) {
                if (LOGGER.isInfoEnabled())
                    LOGGER.info("normal page " + i + " Version: 1." + version + " BS:" + bs.size() + " File:"
                            + origFileName);
                flgIsEmpty = false;
            } else {
                if (LOGGER.isInfoEnabled())
                    LOGGER.info("probably empty page " + i + " Version: 1." + version + " BS:" + bs.size()
                            + " File:" + origFileName);
            }
        } else if (version == '5') {
            // PDF-Version: 5
            // check the contentsize.

            // get the page content
            byte bContent[] = readerOrig.getPageContent(i);
            ByteArrayOutputStream bs = new ByteArrayOutputStream();
            // write the content to an output stream
            bs.write(bContent);

            flgIsEmpty = true;
            if (bs.size() > blankPdfsize_v5) {
                if (LOGGER.isInfoEnabled())
                    LOGGER.info("normal page " + i + " Version: 1." + version + " BS:" + bs.size() + " File:"
                            + origFileName);
                flgIsEmpty = false;
            } else {
                if (LOGGER.isInfoEnabled())
                    LOGGER.info("probably empty page " + i + " Version: 1." + version + " BS:" + bs.size()
                            + " File:" + origFileName);
            }
        }

        // add page to removed or trimmed document
        if (!flgIsEmpty || !flgTrim) {
            if (LOGGER.isInfoEnabled())
                LOGGER.info("add page " + i);
            page = writerTrimmed.getImportedPage(readerOrig, i);
            writerTrimmed.addPage(page);
            countTrimmedPages++;
        } else {
            if (LOGGER.isInfoEnabled())
                LOGGER.info("skip page " + i + " Version: 1." + version + " File:" + origFileName);
            if (writerRemoved != null) {
                page = writerRemoved.getImportedPage(readerOrig, i);
                writerRemoved.addPage(page);
            }
        }
    }

    return countTrimmedPages;
}

From source file:net.seleucus.wsp.crypto.FwknopSymmetricCrypto.java

protected static byte[][] deriveKeyAndIV(byte[] salt, byte[] master_key)
        throws NoSuchAlgorithmException, IOException {

    byte[] key = new byte[KEY_LEN];
    byte[] IV = new byte[IV_LEN];

    MessageDigest md5 = MessageDigest.getInstance("MD5");

    ByteArrayOutputStream data = new ByteArrayOutputStream();
    ByteArrayOutputStream toHash = new ByteArrayOutputStream();
    byte[] d = null;

    while (data.size() < IV_LEN + KEY_LEN) {
        md5.reset();//from   w w w .  jav  a2  s  .c o  m
        toHash.reset();
        if (d != null)
            toHash.write(d);
        toHash.write(master_key);
        toHash.write(salt);
        d = md5.digest(toHash.toByteArray());
        data.write(d);
    }

    byte[] output = data.toByteArray();

    System.arraycopy(output, 0, key, 0, KEY_LEN);
    System.arraycopy(output, KEY_LEN, IV, 0, IV_LEN);

    return new byte[][] { key, IV };
}