Example usage for java.io ByteArrayOutputStream size

List of usage examples for java.io ByteArrayOutputStream size

Introduction

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

Prototype

public synchronized int size() 

Source Link

Document

Returns the current size of the buffer.

Usage

From source file:net.big_oh.common.web.WebUtil.java

private static int approximateObjectSize(Object obj) throws IOException {
    java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
    java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(baos);

    try {/*  w ww . ja v a  2s.  com*/

        int sizeBefore = baos.size();
        oos.writeObject(obj);
        logger.debug("Approximated size of a " + obj.getClass().getName() + " object as "
                + (baos.size() - sizeBefore) + " bytes.");

        return baos.size();

    } catch (NotSerializableException e) {
        logger.warn(
                "Failed to approximate size because of a non-serializable, non-transient object in the object graph for "
                        + obj.getClass().getName(),
                e);
        return 0;
    } finally {
        IOUtils.closeQuietly(baos);
        IOUtils.closeQuietly(oos);
    }

}

From source file:Main.java

public static List<ByteArrayOutputStream> cutby(int value, byte[] content) {
    List<ByteArrayOutputStream> bytes = new ArrayList<ByteArrayOutputStream>();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    boolean preisSame = false; //
    if (content.length > 0) {
        for (int i = 0; i < content.length; i++) {
            if (content[i] != value) {
                out.write(content[i]);//from ww  w  .  ja  va  2 s . c  om
                preisSame = false;
            } else if (!preisSame) {
                if (out.size() > 0) {
                    bytes.add(out);
                    out = new ByteArrayOutputStream();
                }
                preisSame = true;
            }
        }
    }
    if (out.size() > 0) {
        bytes.add(out);
    }
    return bytes;
}

From source file:ch.descabato.browser.BackupBrowser.java

private static byte[] readBytes(InputStream inputStream, long max) throws IOException {
    ByteArrayOutputStream bout = new ByteArrayOutputStream((int) max);
    byte[] buff = new byte[1024];
    BufferedInputStream bin = new BufferedInputStream(inputStream);
    int read = 0;
    while ((read = bin.read(buff)) > 0 && bout.size() < max) {
        bout.write(buff, 0, read);//from w  w w .  j a  v  a 2 s  .co  m
    }

    return bout.toByteArray(); //To change body of created methods use File | Settings | File Templates.
}

From source file:com.github.jinahya.codec.BossVsEngineerTestNanoTimeDecode.java

@BeforeClass
private static void warmUp() throws IOException {
    LOGGER.info("warmUp()");

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    final long freeMemory = Runtime.getRuntime().freeMemory();
    LOGGER.log(Level.INFO, "Runtime.freeMemory: {0}", freeMemory);

    for (int i = 0; i < 16; i++) {
        baos.reset();/*from   w w w  .j a  v a  2s  .c  om*/
        while (baos.size() < 1048576L) {
            baos.write(Tests.decodedBytes());
        }
        final byte[] decoded = baos.toByteArray();
        new HexEncoder().encodeLikeABoss(decoded);
        new HexEncoder().encodeLikeAnEngineer(decoded);
    }

    for (int i = 0; i < 16; i++) {
        baos.reset();
        while (baos.size() < 1048576L) {
            baos.write(Tests.encodedBytes());
        }
        final byte[] encoded = baos.toByteArray();
        new HexDecoder().decodeLikeABoss(encoded);
        new HexDecoder().decodeLikeAnEngineer(encoded);
    }

    for (int i = 0; i < 128; i++) {
        decodeLikeABoss(Tests.encodedBytes());
        decodeLikeAnEngineer(Tests.encodedBytes());
    }
}

From source file:com.github.jinahya.codec.BossVsEngineerTestNanoTimeEncode.java

@BeforeClass
private static void warmUp() throws IOException {
    LOGGER.info("warmUp()");

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    final long freeMemory = Runtime.getRuntime().freeMemory();
    LOGGER.log(Level.INFO, "Runtime.freeMemory: {0}", freeMemory);

    for (int i = 0; i < 16; i++) {
        baos.reset();//from  w w  w .ja v  a2s  .  c o  m
        while (baos.size() < 1048576L) {
            baos.write(Tests.decodedBytes());
        }
        final byte[] decoded = baos.toByteArray();
        new HexEncoder().encodeLikeABoss(decoded);
        new HexEncoder().encodeLikeAnEngineer(decoded);
    }

    for (int i = 0; i < 16; i++) {
        baos.reset();
        while (baos.size() < 1048576L) {
            baos.write(Tests.encodedBytes());
        }
        final byte[] encoded = baos.toByteArray();
        new HexDecoder().decodeLikeABoss(encoded);
        new HexDecoder().decodeLikeAnEngineer(encoded);
    }

    for (int i = 0; i < 128; i++) {
        encodeLikeABoss(Tests.decodedBytes());
        encodeLikeAnEngineer(Tests.decodedBytes());
    }
}

From source file:com.linkedin.pinot.perf.ForwardIndexWriterBenchmark.java

public static void convertRawToForwardIndex(File rawFile) throws Exception {
    List<String> lines = IOUtils.readLines(new FileReader(rawFile));
    int totalDocs = lines.size();
    int max = Integer.MIN_VALUE;
    int maxNumberOfMultiValues = Integer.MIN_VALUE;
    int totalNumValues = 0;
    int data[][] = new int[totalDocs][];
    for (int i = 0; i < lines.size(); i++) {
        String line = lines.get(i);
        String[] split = line.split(",");
        totalNumValues = totalNumValues + split.length;
        if (split.length > maxNumberOfMultiValues) {
            maxNumberOfMultiValues = split.length;
        }/*from   w  w w  .j  a v  a  2s. c om*/
        data[i] = new int[split.length];
        for (int j = 0; j < split.length; j++) {
            String token = split[j];
            int val = Integer.parseInt(token);
            data[i][j] = val;
            if (val > max) {
                max = val;
            }
        }
    }
    int maxBitsNeeded = (int) Math.ceil(Math.log(max) / Math.log(2));
    int size = 2048;
    int[] offsets = new int[size];
    int bitMapSize = 0;
    File outputFile = new File("output.mv.fwd");

    FixedBitMultiValueWriter fixedBitSkipListSCMVWriter = new FixedBitMultiValueWriter(outputFile, totalDocs,
            totalNumValues, maxBitsNeeded);

    for (int i = 0; i < totalDocs; i++) {
        fixedBitSkipListSCMVWriter.setIntArray(i, data[i]);
        if (i % size == size - 1) {
            MutableRoaringBitmap rr1 = MutableRoaringBitmap.bitmapOf(offsets);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(bos);
            rr1.serialize(dos);
            dos.close();
            // System.out.println("Chunk " + i / size + " bitmap size:" + bos.size());
            bitMapSize += bos.size();
        } else if (i == totalDocs - 1) {
            MutableRoaringBitmap rr1 = MutableRoaringBitmap.bitmapOf(Arrays.copyOf(offsets, i % size));
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutputStream dos = new DataOutputStream(bos);
            rr1.serialize(dos);
            dos.close();
            // System.out.println("Chunk " + i / size + " bitmap size:" + bos.size());
            bitMapSize += bos.size();
        }
    }
    fixedBitSkipListSCMVWriter.close();
    System.out.println("Output file size:" + outputFile.length());
    System.out.println("totalNumberOfDoc\t\t\t:" + totalDocs);
    System.out.println("totalNumberOfValues\t\t\t:" + totalNumValues);
    System.out.println("chunk size\t\t\t\t:" + size);
    System.out.println("Num chunks\t\t\t\t:" + totalDocs / size);
    int numChunks = totalDocs / size + 1;
    int totalBits = (totalNumValues * maxBitsNeeded);
    int dataSizeinBytes = (totalBits + 7) / 8;

    System.out.println("Raw data size with fixed bit encoding\t:" + dataSizeinBytes);
    System.out.println("\nPer encoding size");
    System.out.println();
    System.out.println("size (offset + length)\t\t\t:" + ((totalDocs * (4 + 4)) + dataSizeinBytes));
    System.out.println();
    System.out.println("size (offset only)\t\t\t:" + ((totalDocs * (4)) + dataSizeinBytes));
    System.out.println();
    System.out.println("bitMapSize\t\t\t\t:" + bitMapSize);
    System.out.println("size (with bitmap)\t\t\t:" + (bitMapSize + (numChunks * 4) + dataSizeinBytes));

    System.out.println();
    System.out.println("Custom Bitset\t\t\t\t:" + (totalNumValues + 7) / 8);
    System.out.println("size (with custom bitset)\t\t\t:"
            + (((totalNumValues + 7) / 8) + (numChunks * 4) + dataSizeinBytes));
}

From source file:org.apache.axis.attachments.MimeUtils.java

/**
 * Gets the header length for any part./*from   ww w  . j a va 2 s. c om*/
 * @param bp the part to determine the header length for.
 * @return the length in bytes.
 *
 * @throws javax.mail.MessagingException
 * @throws java.io.IOException
 */
private static long getHeaderLength(javax.mail.internet.MimeBodyPart bp)
        throws javax.mail.MessagingException, java.io.IOException {

    javax.mail.internet.MimeBodyPart headersOnly = new javax.mail.internet.MimeBodyPart(
            new javax.mail.internet.InternetHeaders(), new byte[0]);

    for (java.util.Enumeration en = bp.getAllHeaders(); en.hasMoreElements();) {
        javax.mail.Header header = (javax.mail.Header) en.nextElement();

        headersOnly.addHeader(header.getName(), header.getValue());
    }

    java.io.ByteArrayOutputStream bas = new java.io.ByteArrayOutputStream(1024 * 16);

    headersOnly.writeTo(bas);
    bas.close();

    return (long) bas.size(); // This has header length plus the crlf part that seperates the data
}

From source file:org.eclipse.winery.accountability.blockchain.util.CompressionUtils.java

public static byte[] compress(byte[] content) {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    try {//from  www.java2 s . c om
        GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
        gzipOutputStream.write(content);
        gzipOutputStream.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    System.out.printf("Compressiono %f\n", (1.0f * content.length / byteArrayOutputStream.size()));
    return byteArrayOutputStream.toByteArray();
}

From source file:org.openamf.test.RemotingTester.java

public static AMFMessage sendMessage(String gateway, AMFMessage requestMessage)
        throws IOException, HttpException {

    ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
    DataOutputStream outputStream = new DataOutputStream(baOutputStream);
    AMFSerializer serializer = new AMFSerializer(outputStream);
    serializer.serializeMessage(requestMessage);

    PostMethod post = new PostMethod(gateway);
    post.setRequestBody(new ByteArrayInputStream(baOutputStream.toByteArray()));
    post.setRequestContentLength(baOutputStream.size());
    post.setRequestHeader("Content-type", "application/x-amf");

    HttpClient httpclient = new HttpClient();
    int result = httpclient.executeMethod(post);

    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(post.getResponseBody()));

    AMFDeserializer deserializer = new AMFDeserializer(inputStream);
    AMFMessage resposeMessage = deserializer.getAMFMessage();

    // Release current connection to the connection pool once you are done
    post.releaseConnection();/*from   w  w w .  j a va  2 s .com*/

    return resposeMessage;
}

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

/**
 * <h4>FeatureDomain:</h4>/*from w ww  . j a  v a2  s.com*/
 *     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;
}