Example usage for java.io BufferedOutputStream flush

List of usage examples for java.io BufferedOutputStream flush

Introduction

In this page you can find the example usage for java.io BufferedOutputStream flush.

Prototype

@Override
public synchronized void flush() throws IOException 

Source Link

Document

Flushes this buffered output stream.

Usage

From source file:com.panet.imeta.core.xml.XMLHandler.java

/**
 * Build an XML string (including a carriage return) for a certain tag
 * binary (byte[]) value//from  w ww .  j ava 2  s .c  om
 * 
 * @param tag
 *            The XML tag
 * @param val
 *            The binary value of the tag
 * @return The XML String for the tag.
 * @throws IOException
 *             in case there is an Base64 or GZip encoding problem
 */
public static final String addTagValue(String tag, byte[] val, boolean cr) throws IOException {
    String string;
    if (val == null) {
        string = null;
    } else {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        GZIPOutputStream gzos = new GZIPOutputStream(baos);
        BufferedOutputStream bos = new BufferedOutputStream(gzos);
        bos.write(val);
        bos.flush();
        bos.close();

        string = new String(Base64.encodeBase64(baos.toByteArray()));
    }

    return addTagValue(tag, string, true);
}

From source file:hu.sztaki.lpds.pgportal.services.dspace.DSpaceUtil.java

/**
 * Extracts any file from <code>inputFile</code> that begins with
 * 'bitstream' to <code>outputFile</code>.
 * Adapted from examples on/*from w w  w . j  a  v a2s.co m*/
 * http://java.sun.com/developer/technicalArticles/Programming/compression/
 *
 * @param inputFile  File to extract 'bitstream...' from
 * @param outputFile  File to extract 'bitstream...' to
 */
private static void unzipBitstream(InputStream is, File outputFile) throws IOException {
    final int BUFFER = 2048;
    BufferedOutputStream dest = null;
    ZipInputStream zis = new ZipInputStream(is);
    ZipEntry entry;
    try {

        while ((entry = zis.getNextEntry()) != null) {
            if (entry.getName().matches("bitstream.*")) {
                int count;
                byte data[] = new byte[BUFFER];

                // write the files to the disk
                FileOutputStream fos = new FileOutputStream(outputFile.getAbsoluteFile());
                dest = new BufferedOutputStream(fos, BUFFER);
                while ((count = zis.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, count);
                    //System.out.println("Writing: " + outputFile.getAbsoluteFile());
                }
                dest.flush();
                dest.close();
            }
        }
    } finally {
        zis.close();
        try {
            dest.close();
        } catch (Exception e) {
        }
    }
}

From source file:com.gsr.myschool.server.reporting.convocation.ConvocationController.java

@RequestMapping(method = RequestMethod.GET, produces = "application/pdf")
@ResponseStatus(HttpStatus.OK)/*from   w  ww  .  j a  va2  s .  c o  m*/
public void generateConvocation(@RequestParam String number, HttpServletResponse response) {
    ReportDTO dto = convocationService.generateConvocation(number);
    try {
        response.addHeader("Content-Disposition", "attachment; filename=" + dto.getFileName());

        BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        byte[] result = reportService.generatePdf(dto);

        outputStream.write(result, 0, result.length);
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.gsr.myschool.server.reporting.convocation.ConvocationController.java

@RequestMapping(value = "test", method = RequestMethod.GET, produces = "application/pdf")
@ResponseStatus(HttpStatus.OK)/*  w  w w .ja  v a2s  .  c  o  m*/
public void generateReportTest(@RequestParam String niveauId, @RequestParam String sessionId,
        HttpServletResponse response) {
    ReportDTO dto = convocationService.generateConvocationTest(Long.valueOf(niveauId), Long.valueOf(sessionId));
    try {
        response.addHeader("Content-Disposition", "attachment; filename=convocation.pdf");

        BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        byte[] result = reportService.generatePdf(dto);

        outputStream.write(result, 0, result.length);
        outputStream.flush();
        outputStream.close();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:net.zcarioca.jmx.servlet.handlers.RestRequestHandler.java

public void respond(HttpServletRequest request, HttpServletResponse response) throws IOException {
    try {/*from  ww w.jav  a2 s  . c om*/
        Object value = getResponse(request);

        if (value != null) {
            response.setContentType(MediaType.APPLICATION_JSON);
            response.setCharacterEncoding("UTF-8");

            BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
            ObjectMapper mapper = new ObjectMapper();
            mapper.writeValue(out, value);

            out.flush();
            out.close();
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    } catch (InvalidObjectException exc) {
        response.sendError(HttpServletResponse.SC_BAD_REQUEST);
    } catch (IOException exc) {
        throw exc;
    } catch (Exception exc) {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
}

From source file:com.galois.qrstream.lib.DecodeThread.java

private @NotNull File storeData(Job message) throws IOException {
    File cacheDir = context.getCacheDir();
    File tmpFile = File.createTempFile(Constants.APP_TAG, "", cacheDir);

    // make tmpFile world-readable:
    tmpFile.setReadable(true, false);//  www .ja v  a 2s. c om
    tmpFile.deleteOnExit();

    BufferedOutputStream bos = null;
    try {
        bos = new BufferedOutputStream(new FileOutputStream(tmpFile));
        bos.write(message.getData());
        bos.flush();
    } finally {
        if (null != bos) {
            bos.close();
        }
    }
    return tmpFile;
}

From source file:com.glaf.report.jxls.JxlsReportContainer.java

public byte[] toXls(String reportId, String actorId, Map<String, Object> context) {
    Workbook wb = JxlsReportContainer.getContainer().execute(reportId, actorId, context);
    if (wb != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedOutputStream bos = new BufferedOutputStream(baos);
        try {/*  ww  w.j a v  a  2s  . c  o  m*/
            wb.write(bos);
            bos.flush();
            return baos.toByteArray();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        } finally {
            wb = null;
            try {
                baos.close();
            } catch (IOException e) {
            }
            try {
                bos.close();
            } catch (IOException e) {
            }
            baos = null;
            bos = null;
        }
    }
    return null;
}

From source file:de.betterform.agent.web.servlet.XFormsRequestURIServlet.java

/**
 * This servlet uses the requestURI to locate and parse a XForms document for processing. The actual processing
 * is done by XFormsFilter. The parsed DOM of the document is passed as a request param to the filter.
 *
 * @param request  servlet request/*from ww  w.j a v  a2s . c  om*/
 * @param response servlet response
 * @throws javax.servlet.ServletException
 * @throws java.io.IOException
 */
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    LOGGER.debug("hit XFormsRequestURIServlet");

    request.setCharacterEncoding("UTF-8");
    WebUtil.nonCachingResponse(response);

    Document doc;

    //locate it
    String formRequestURI = request.getRequestURI().substring(request.getContextPath().length() + 1);
    String realPath = null;
    try {
        realPath = WebFactory.getRealPath(formRequestURI, getServletContext());
    } catch (XFormsConfigException e) {
        throw new ServletException(e);
    }
    File xfDoc = new File(realPath);

    if (request.getHeader("betterform-internal") != null) {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(xfDoc));
        BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());

        int read;
        while ((read = in.read()) > -1) {
            out.write(read);
        }
        out.flush();
    } else {
        try {
            //parse it
            doc = DOMUtil.parseXmlFile(xfDoc, true, false);
        } catch (ParserConfigurationException e) {
            throw new ServletException(e);
        } catch (SAXException e) {
            throw new ServletException(e);
        }

        request.setAttribute(WebFactory.XFORMS_NODE, doc);
        //do the Filter twist
        response.getOutputStream().close();
    }
}

From source file:com.glaf.core.util.ZipUtils.java

public static Map<String, byte[]> getZipBytesMap(ZipInputStream zipInputStream, List<String> includes) {
    Map<String, byte[]> zipMap = new java.util.HashMap<String, byte[]>();
    java.util.zip.ZipEntry zipEntry = null;
    ByteArrayOutputStream baos = null;
    BufferedOutputStream bos = null;
    byte tmpByte[] = null;
    try {/*from   w  w  w .j a  v  a2 s  . c om*/
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            String name = zipEntry.getName();
            String ext = FileUtils.getFileExt(name);
            if (includes.contains(ext) || includes.contains(ext.toLowerCase())) {
                tmpByte = new byte[BUFFER];
                baos = new ByteArrayOutputStream();
                bos = new BufferedOutputStream(baos, BUFFER);
                int i = 0;
                while ((i = zipInputStream.read(tmpByte, 0, BUFFER)) != -1) {
                    bos.write(tmpByte, 0, i);
                }
                bos.flush();
                byte[] bytes = baos.toByteArray();
                IOUtils.closeStream(baos);
                IOUtils.closeStream(baos);
                zipMap.put(zipEntry.getName(), bytes);
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(baos);
        IOUtils.closeStream(baos);
    }
    return zipMap;
}

From source file:com.glaf.core.util.ZipUtils.java

public static void unzip(File zipFile) {
    FileInputStream fileInputStream = null;
    ZipInputStream zipInputStream = null;
    FileOutputStream fileoutputstream = null;
    BufferedOutputStream bufferedoutputstream = null;
    try {//from  ww w  .  j a va  2 s .  c o  m
        fileInputStream = new FileInputStream(zipFile);
        zipInputStream = new ZipInputStream(fileInputStream);
        java.util.zip.ZipEntry zipEntry;
        while ((zipEntry = zipInputStream.getNextEntry()) != null) {
            byte abyte0[] = new byte[BUFFER];
            fileoutputstream = new FileOutputStream(zipEntry.getName());
            bufferedoutputstream = new BufferedOutputStream(fileoutputstream, BUFFER);
            int i;
            while ((i = zipInputStream.read(abyte0, 0, BUFFER)) != -1) {
                bufferedoutputstream.write(abyte0, 0, i);
            }
            bufferedoutputstream.flush();
            IOUtils.closeStream(fileoutputstream);
            IOUtils.closeStream(bufferedoutputstream);
        }
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        IOUtils.closeStream(fileInputStream);
        IOUtils.closeStream(zipInputStream);
        IOUtils.closeStream(fileoutputstream);
        IOUtils.closeStream(bufferedoutputstream);
    }
}