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:org.apache.hadoop.mapreduce.task.reduce.TestFetcher.java

@Test
public void testCorruptedIFile() throws Exception {
    final int fetcher = 7;
    Path onDiskMapOutputPath = new Path(name.getMethodName() + "/foo");
    Path shuffledToDisk = OnDiskMapOutput.getTempPath(onDiskMapOutputPath, fetcher);
    fs = FileSystem.getLocal(job).getRaw();
    IFileWrappedMapOutput<Text, Text> odmo = new OnDiskMapOutput<Text, Text>(map1ID, mm, 100L, job, fetcher,
            true, fs, onDiskMapOutputPath);

    String mapData = "MAPDATA12345678901234567890";

    ShuffleHeader header = new ShuffleHeader(map1ID.toString(), 14, 10, 1);
    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(bout);
    IFileOutputStream ios = new IFileOutputStream(dos);
    header.write(dos);/*from   ww  w .  j  av a 2  s  .  c  o m*/

    int headerSize = dos.size();
    try {
        ios.write(mapData.getBytes());
    } finally {
        ios.close();
    }

    int dataSize = bout.size() - headerSize;

    // Ensure that the OnDiskMapOutput shuffler can successfully read the data.
    MapHost host = new MapHost("TestHost", "http://test/url");
    ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
    try {
        // Read past the shuffle header.
        bin.read(new byte[headerSize], 0, headerSize);
        odmo.shuffle(host, bin, dataSize, dataSize, metrics, Reporter.NULL);
    } finally {
        bin.close();
    }

    // Now corrupt the IFile data.
    byte[] corrupted = bout.toByteArray();
    corrupted[headerSize + (dataSize / 2)] = 0x0;

    try {
        bin = new ByteArrayInputStream(corrupted);
        // Read past the shuffle header.
        bin.read(new byte[headerSize], 0, headerSize);
        odmo.shuffle(host, bin, dataSize, dataSize, metrics, Reporter.NULL);
        fail("OnDiskMapOutput.shuffle didn't detect the corrupted map partition file");
    } catch (ChecksumException e) {
        LOG.info("The expected checksum exception was thrown.", e);
    } finally {
        bin.close();
    }

    // Ensure that the shuffled file can be read.
    IFileInputStream iFin = new IFileInputStream(fs.open(shuffledToDisk), dataSize, job);
    try {
        iFin.read(new byte[dataSize], 0, dataSize);
    } finally {
        iFin.close();
    }
}

From source file:org.commoncrawl.util.CompressedURLFPListV2.java

public static void validateURLFPSerializationRootDomain() {

    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    Builder firstBuilder = new Builder(FLAG_ARCHIVE_SEGMENT_ID);

    TreeMultimap<Long, URLFPV2> sourceMap = TreeMultimap.create();
    TreeMultimap<Long, URLFPV2> destMap = TreeMultimap.create();
    ;//from   w  w  w.ja va2s .  com

    // top level domain with matching sub domain an two urls
    insertURLFPItem(sourceMap, DOMAIN_2_SUBDOMAIN_1_URL_1 + "0", 1);
    insertURLFPItem(sourceMap, DOMAIN_2_SUBDOMAIN_1_URL_2 + "1", 1);
    // top level domain with matching sub domain an two urls
    insertURLFPItem(sourceMap, DOMAIN_2_SUBDOMAIN_1_URL_1 + "2", 1);
    insertURLFPItem(sourceMap, DOMAIN_2_SUBDOMAIN_1_URL_2 + "3", 1);
    // top level domain with matching sub domain an two urls
    insertURLFPItem(sourceMap, DOMAIN_2_SUBDOMAIN_1_URL_1 + "4", 1);
    insertURLFPItem(sourceMap, DOMAIN_2_SUBDOMAIN_1_URL_2 + "5", 1);

    addMapToBuilder(firstBuilder, sourceMap);

    try {
        // flush to byte stream ...
        firstBuilder.flush(byteStream);
        // now set up to read the stream
        ByteArrayInputStream inputStream = new ByteArrayInputStream(byteStream.toByteArray(), 0,
                byteStream.size());
        Reader reader = new Reader(inputStream);

        while (reader.hasNext()) {
            URLFPV2 fp = reader.next();
            destMap.put(fp.getRootDomainHash(), fp);
        }
        reader.close();

        // dump both lists
        for (long rootDomain : sourceMap.keySet()) {
            for (URLFPV2 URLFPV2 : sourceMap.get(rootDomain)) {
                System.out.println("SourceFP Root:" + URLFPV2.getRootDomainHash() + " Domain:"
                        + URLFPV2.getDomainHash() + " URL:" + URLFPV2.getUrlHash());
            }
        }
        for (long rootDomain : destMap.keySet()) {
            for (URLFPV2 URLFPV2 : destMap.get(rootDomain)) {
                System.out.println("DestFP Root:" + URLFPV2.getRootDomainHash() + " Domain:"
                        + URLFPV2.getDomainHash() + " URL:" + URLFPV2.getUrlHash());
            }
        }

        System.out
                .println("Buffer Size:" + byteStream.size() + " URLCount:" + firstBuilder.getLinkMap().size());

        Assert.assertTrue(sourceMap.equals(destMap));

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:biz.webgate.dominoext.poi.component.kernel.simpleviewexport.CSVExportProcessor.java

public void process2HTTP(ExportModel expModel, UISimpleViewExport uis, HttpServletResponse hsr,
        DateTimeHelper dth) {/*from  w  w w .j a  va  2s  . co  m*/
    try {
        ByteArrayOutputStream csvBAOS = new ByteArrayOutputStream();
        OutputStreamWriter csvWriter = new OutputStreamWriter(csvBAOS);
        CSVPrinter csvPrinter = new CSVPrinter(csvWriter, CSVFormat.DEFAULT);

        // BUILDING HEADER
        if (uis.isIncludeHeader()) {
            for (ExportColumn expColumn : expModel.getColumns()) {
                csvPrinter.print(expColumn.getColumnName());
            }
            csvPrinter.println();
        }
        // Processing Values
        for (ExportDataRow expRow : expModel.getRows()) {
            for (ExportColumn expColumn : expModel.getColumns()) {
                csvPrinter.print(convertValue(expRow.getValue(expColumn.getPosition()), expColumn, dth));
            }
            csvPrinter.println();
        }
        csvPrinter.flush();

        hsr.setContentType("text/csv");
        hsr.setHeader("Cache-Control", "no-cache");
        hsr.setDateHeader("Expires", -1);
        hsr.setContentLength(csvBAOS.size());
        hsr.addHeader("Content-disposition", "inline; filename=\"" + uis.getDownloadFileName() + "\"");
        OutputStream os = hsr.getOutputStream();
        csvBAOS.writeTo(os);
        os.close();
    } catch (Exception e) {
        ErrorPageBuilder.getInstance().processError(hsr, "Error during SVE-Generation (CSV Export)", e);
    }
}

From source file:Highcharts.ExportController.java

@RequestMapping(method = RequestMethod.POST)
    public void exporter(@RequestParam(value = "svg", required = false) String svg,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "filename", required = false) String filename,
            @RequestParam(value = "width", required = false) String width,
            @RequestParam(value = "scale", required = false) String scale,
            @RequestParam(value = "options", required = false) String options,
            @RequestParam(value = "constr", required = false) String constructor,
            @RequestParam(value = "callback", required = false) String callback, HttpServletResponse response,
            HttpServletRequest request) throws ServletException, IOException, InterruptedException,
            SVGConverterException, NoSuchElementException, PoolException, TimeoutException {

        long start1 = System.currentTimeMillis();

        MimeType mime = getMime(type);
        filename = getFilename(filename);
        Float parsedWidth = widthToFloat(width);
        Float parsedScale = scaleToFloat(scale);
        options = sanitize(options);//from   www . j ava2 s  . c om
        String input;

        boolean convertSvg = false;

        if (options != null) {
            // create a svg file out of the options
            input = options;
            callback = sanitize(callback);
        } else {
            // assume SVG conversion
            if (svg == null) {
                throw new ServletException("The manadatory svg POST parameter is undefined.");
            } else {
                svg = sanitize(svg);
                if (svg == null) {
                    throw new ServletException("The manadatory svg POST parameter is undefined.");
                }
                convertSvg = true;
                input = svg;
            }
        }

        ByteArrayOutputStream stream = null;
        if (convertSvg && mime.equals(MimeType.SVG)) {
            // send this to the client, without converting.
            stream = new ByteArrayOutputStream();
            stream.write(input.getBytes());
        } else {
            //stream = SVGCreator.getInstance().convert(input, mime, constructor, callback, parsedWidth, parsedScale);
            stream = converter.convert(input, mime, constructor, callback, parsedWidth, parsedScale);
        }

        if (stream == null) {
            throw new ServletException("Error while converting");
        }

        logger.debug(request.getHeader("referer") + " Total time: " + (System.currentTimeMillis() - start1));

        response.reset();
        response.setCharacterEncoding("utf-8");
        response.setContentLength(stream.size());
        response.setStatus(HttpStatus.OK.value());
        response.setHeader("Content-disposition",
                "attachment; filename=\"" + filename + "." + mime.name().toLowerCase() + "\"");

        IOUtils.write(stream.toByteArray(), response.getOutputStream());
        response.flushBuffer();
    }

From source file:com.pdfhow.diff.UploadServlet.java

/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request//  w w w. ja va 2  s  . c o  m
 *            servlet request
 * @param response
 *            servlet response
 * @throws ServletException
 *             if a servlet-specific error occurs
 * @throws IOException
 *             if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (request.getParameter("getfile") != null && !request.getParameter("getfile").isEmpty()) {
        File file = new File(
                request.getServletContext().getRealPath("/") + "imgs/" + request.getParameter("getfile"));
        if (file.exists()) {
            int bytes = 0;
            ServletOutputStream op = response.getOutputStream();

            response.setContentType(getMimeType(file));
            response.setContentLength((int) file.length());
            response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

            byte[] bbuf = new byte[1024];
            DataInputStream in = new DataInputStream(new FileInputStream(file));

            while ((in != null) && ((bytes = in.read(bbuf)) != -1)) {
                op.write(bbuf, 0, bytes);
            }

            in.close();
            op.flush();
            op.close();
        }
    } else if (request.getParameter("delfile") != null && !request.getParameter("delfile").isEmpty()) {
        File file = new File(
                request.getServletContext().getRealPath("/") + "imgs/" + request.getParameter("delfile"));
        if (file.exists()) {
            file.delete(); // TODO:check and report success
        }
    } else if (request.getParameter("getthumb") != null && !request.getParameter("getthumb").isEmpty()) {
        File file = new File(
                request.getServletContext().getRealPath("/") + "imgs/" + request.getParameter("getthumb"));
        if (file.exists()) {
            System.out.println(file.getAbsolutePath());
            String mimetype = getMimeType(file);
            if (mimetype.endsWith("png") || mimetype.endsWith("jpeg") || mimetype.endsWith("jpg")
                    || mimetype.endsWith("gif")) {
                BufferedImage im = ImageIO.read(file);
                if (im != null) {
                    BufferedImage thumb = Scalr.resize(im, 75);
                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    if (mimetype.endsWith("png")) {
                        ImageIO.write(thumb, "PNG", os);
                        response.setContentType("image/png");
                    } else if (mimetype.endsWith("jpeg")) {
                        ImageIO.write(thumb, "jpg", os);
                        response.setContentType("image/jpeg");
                    } else if (mimetype.endsWith("jpg")) {
                        ImageIO.write(thumb, "jpg", os);
                        response.setContentType("image/jpeg");
                    } else {
                        ImageIO.write(thumb, "GIF", os);
                        response.setContentType("image/gif");
                    }
                    ServletOutputStream srvos = response.getOutputStream();
                    response.setContentLength(os.size());
                    response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
                    os.writeTo(srvos);
                    srvos.flush();
                    srvos.close();
                }
            }
        } // TODO: check and report success
    } else {
        PrintWriter writer = response.getWriter();
        writer.write("call POST with multipart form data");
    }
}

From source file:fuse.okuyamafs.OkuyamaFilesystem.java

public int write(String path, Object fh, boolean isWritepage, ByteBuffer buf, long offset)
        throws FuseException {
    log.info("write  path:" + path + " offset:" + offset + " isWritepage:" + isWritepage + " buf.limit:"
            + buf.limit());//w w  w. j  ava2s.com
    //long startAA = System.nanoTime();
    try {

        if (startTimeAAA == 0L)
            startTimeAAA = System.nanoTime();
        // ??????????????
        if (OkuyamaFilesystem.storageType == 1)
            return realWrite(path, fh, isWritepage, buf, offset);

        if (fh == null)
            return Errno.EBADE;

        synchronized (this.parallelDataAccessSync[((path.hashCode() << 1) >>> 1) % 100]) {

            if (appendWriteDataBuf.containsKey(fh)) {

                Map appendData = (Map) appendWriteDataBuf.get(fh);
                ByteArrayOutputStream bBuf = (ByteArrayOutputStream) appendData.get("buf");
                long bOffset = ((Long) appendData.get("offset")).longValue();

                if ((bOffset + bBuf.size()) == offset) {

                    byte[] tmpBuf = new byte[buf.limit()];
                    buf.get(tmpBuf);
                    bBuf.write(tmpBuf);

                    // ??????????????????
                    if (bBuf.size() >= writeBufferSize) {

                        // ???????
                        appendWriteDataBuf.remove(fh);
                        String bPath = (String) appendData.get("path");
                        Object bFh = (Object) appendData.get("fh");
                        boolean bIsWritepage = ((Boolean) appendData.get("isWritepage")).booleanValue();

                        int ret = this.realWrite(bPath, bFh, bIsWritepage, bBuf, bOffset);

                        return ret;
                    } else {

                        return 0;
                    }
                } else {

                    // offset?????????????
                    appendWriteDataBuf.remove(fh);
                    String bPath = (String) appendData.get("path");
                    Object bFh = (Object) appendData.get("fh");
                    boolean bIsWritepage = ((Boolean) appendData.get("isWritepage")).booleanValue();

                    int realWriteRet = this.realWrite(bPath, bFh, bIsWritepage, bBuf, bOffset);

                    if (realWriteRet == 0) {

                        int retI = this.realWrite(path, fh, isWritepage, buf, offset);

                        return retI;
                    } else {
                        return realWriteRet;
                    }
                }
            } else {

                Map appendData = new HashMap();
                appendData.put("path", path);
                appendData.put("fh", fh);
                appendData.put("isWritepage", isWritepage);
                ByteArrayOutputStream baos = new ByteArrayOutputStream(1024 * 1024 * 10 + 8192);
                byte[] tmpByte = new byte[buf.limit()];
                buf.get(tmpByte);
                baos.write(tmpByte);
                appendData.put("buf", baos);
                appendData.put("offset", offset);
                this.appendWriteDataBuf.put(fh, appendData);
                this.writeBufFpMap.addGroupingData(path, fh);

                return 0;
            }
        }
    } catch (Exception e) {

        throw new FuseException(e);
    } finally {
    }
}

From source file:org.openqa.selenium.server.SeleniumDriverResourceHandler.java

private void respond(HttpResponse res, RemoteCommand sc, String uniqueId) throws IOException {
    ByteArrayOutputStream buf = new ByteArrayOutputStream(1000);
    Writer writer = new OutputStreamWriter(buf, StringUtil.__UTF_8);
    if (sc != null) {
        writer.write(sc.toString());//from  ww w  .jav a2 s. c om
        log.fine("res to " + uniqueId + ": " + sc.toString());
    } else {
        log.fine("res empty");
    }
    for (int pad = 998 - buf.size(); pad-- > 0;) {
        writer.write(" ");
    }
    writer.write("\015\012");
    writer.close();
    OutputStream out = res.getOutputStream();
    buf.writeTo(out);

}

From source file:com.liferay.mobile.android.http.file.FileUploadTest.java

@Test
public void addFileEntry() throws Exception {
    DLAppService service = new DLAppService(session);

    long repositoryId = props.getGroupId();
    long folderId = DLAppServiceTest.PARENT_FOLDER_ID;
    String fileName = DLAppServiceTest.SOURCE_FILE_NAME;
    String mimeType = DLAppServiceTest.MIME_TYPE;

    InputStream is = new ByteArrayInputStream("Hello".getBytes(StandardCharsets.UTF_8));

    final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    FileProgressCallback callback = new FileProgressCallback() {

        @Override//from ww  w.  j  a  va2 s . c  om
        public void onBytes(byte[] bytes) {
            try {
                baos.write(bytes);
            } catch (IOException ioe) {
                fail(ioe.getMessage());
            }
        }

        @Override
        public void onProgress(int totalBytes) {
            if (totalBytes == 5) {
                try {
                    baos.flush();
                } catch (IOException ioe) {
                    fail(ioe.getMessage());
                }
            }
        }

    };

    UploadData data = new UploadData(is, mimeType, fileName, callback);

    _file = service.addFileEntry(repositoryId, folderId, fileName, mimeType, fileName, "", "", data, null);

    assertEquals(fileName, _file.get(DLAppServiceTest.TITLE));
    assertEquals(5, callback.getTotal());
    assertEquals(5, baos.size());
}

From source file:org.commoncrawl.util.CompressedURLFPList.java

public static void validateURLFPSerializationMultiDomain() {

    ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    Builder firstBuilder = new Builder(FLAG_ARCHIVE_SEGMENT_ID);

    TreeMultimap<Integer, URLFP> sourceMap = TreeMultimap.create();
    TreeMultimap<Integer, URLFP> destMap = TreeMultimap.create();
    ;//from  w  w w .  j av  a 2  s  .c o  m

    // single top level domain with one sub domain an two urls
    insertURLFPItem(sourceMap, DOMAIN_1_SUBDOMAIN_1_URL_1, 1);
    insertURLFPItem(sourceMap, DOMAIN_1_SUBDOMAIN_1_URL_2, 1);

    // top level domain with matching sub domain an two urls
    insertURLFPItem(sourceMap, DOMAIN_2_SUBDOMAIN_1_URL_1, 1);
    insertURLFPItem(sourceMap, DOMAIN_2_SUBDOMAIN_1_URL_2, 1);

    // two sub domains with two urls each
    insertURLFPItem(sourceMap, DOMAIN_3_SUBDOMAIN_1_URL_1, 1);
    insertURLFPItem(sourceMap, DOMAIN_3_SUBDOMAIN_1_URL_2, 1);
    insertURLFPItem(sourceMap, DOMAIN_3_SUBDOMAIN_2_URL_1, 1);
    insertURLFPItem(sourceMap, DOMAIN_3_SUBDOMAIN_2_URL_2, 1);

    addMapToBuilder(firstBuilder, sourceMap);

    try {
        // flush to byte stream ...
        firstBuilder.flush(byteStream);
        // now set up to read the stream
        ByteArrayInputStream inputStream = new ByteArrayInputStream(byteStream.toByteArray(), 0,
                byteStream.size());
        Reader reader = new Reader(inputStream);

        while (reader.hasNext()) {
            URLFP fp = reader.next();
            destMap.put(fp.getRootDomainHash(), fp);
        }
        reader.close();

        // dump both lists
        for (Integer rootDomain : sourceMap.keySet()) {
            for (URLFP urlfp : sourceMap.get(rootDomain)) {
                System.out.println("SourceFP Root:" + urlfp.getRootDomainHash() + " Domain:"
                        + urlfp.getDomainHash() + " URL:" + urlfp.getUrlHash());
            }
        }
        for (Integer rootDomain : destMap.keySet()) {
            for (URLFP urlfp : destMap.get(rootDomain)) {
                System.out.println("DestFP Root:" + urlfp.getRootDomainHash() + " Domain:"
                        + urlfp.getDomainHash() + " URL:" + urlfp.getUrlHash());
            }
        }

        Assert.assertTrue(sourceMap.equals(destMap));

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.nemesis.admin.UploadServlet.java

/**
 * @param request/*w  ww.  j a v  a2 s  . com*/
 * @param response
 * @throws javax.servlet.ServletException
 * @throws java.io.IOException
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 * response)
 *
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    if (request.getParameter("getfile") != null && !request.getParameter("getfile").isEmpty()) {
        File file = getFile(request, request.getParameter("getfile"));
        if (file.exists()) {
            int bytes = 0;
            try (ServletOutputStream op = response.getOutputStream()) {
                response.setContentType(getMimeType(file));
                response.setContentLength((int) file.length());
                response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

                byte[] bbuf = new byte[1024];
                DataInputStream in = new DataInputStream(new FileInputStream(file));

                while ((in != null) && ((bytes = in.read(bbuf)) != -1)) {
                    op.write(bbuf, 0, bytes);
                }

                in.close();
                op.flush();
            }
        }
    } else if (request.getParameter("delfile") != null && !request.getParameter("delfile").isEmpty()) {
        File file = getFile(request, request.getParameter("delfile"));
        if (file.exists()) {
            file.delete(); // TODO:check and report success
        }
    } else if (request.getParameter("getthumb") != null && !request.getParameter("getthumb").isEmpty()) {
        File file = getFile(request, request.getParameter("getthumb"));
        if (file.exists()) {
            System.out.println(file.getAbsolutePath());
            String mimetype = getMimeType(file);
            if (mimetype.endsWith("png") || mimetype.endsWith("jpeg") || mimetype.endsWith("jpg")
                    || mimetype.endsWith("gif")) {
                BufferedImage im = ImageIO.read(file);
                if (im != null) {
                    int newWidth = 75;
                    if (request.getParameter("w") != null) {
                        try {
                            newWidth = Integer.parseInt(request.getParameter("w"));
                        } catch (Exception e) {
                            //Se mantiene el valor por defecto de 75
                        }
                    }

                    BufferedImage thumb = Scalr.resize(im, newWidth);
                    if (request.getParameter("h") != null) {
                        try {
                            thumb = Scalr.crop(thumb, newWidth, Integer.parseInt(request.getParameter("h")));
                        } catch (IllegalArgumentException | ImagingOpException e) {
                            //Se mantienen las proporciones.
                        }
                    }

                    ByteArrayOutputStream os = new ByteArrayOutputStream();
                    if (mimetype.endsWith("png")) {
                        ImageIO.write(thumb, "PNG", os);
                        response.setContentType("image/png");
                    } else if (mimetype.endsWith("jpeg")) {
                        ImageIO.write(thumb, "jpg", os);
                        response.setContentType("image/jpeg");
                    } else if (mimetype.endsWith("jpg")) {
                        ImageIO.write(thumb, "jpg", os);
                        response.setContentType("image/jpeg");
                    } else {
                        ImageIO.write(thumb, "GIF", os);
                        response.setContentType("image/gif");
                    }
                    try (ServletOutputStream srvos = response.getOutputStream()) {
                        response.setContentLength(os.size());
                        response.setHeader("Content-Disposition",
                                "inline; filename=\"" + file.getName() + "\"");
                        os.writeTo(srvos);
                        srvos.flush();
                    }
                }
            }
        } // TODO: check and report success
    } else {
        PrintWriter writer = response.getWriter();
        writer.write("call POST with multipart form data");
    }
}