Example usage for java.io BufferedOutputStream write

List of usage examples for java.io BufferedOutputStream write

Introduction

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

Prototype

@Override
public synchronized void write(byte b[], int off, int len) throws IOException 

Source Link

Document

Writes len bytes from the specified byte array starting at offset off to this buffered output stream.

Usage

From source file:com.honnix.cheater.bundle.CheaterActivator.java

private void setTrustStore(BundleContext context) throws IOException {
    InputStream is = context.getBundle().getEntry("/etc/jssecacerts").openStream();
    BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(TEMP_TRUST_STORE));

    byte[] buffer = new byte[255];
    int length;//from   w  w  w  .  ja  v  a2s.  co m

    while ((length = is.read(buffer)) != -1) {
        os.write(buffer, 0, length);
    }

    os.flush();
    os.close();
    is.close();

    System.setProperty(CheaterConstant.TRUST_STORE_KEY, TEMP_TRUST_STORE);
}

From source file:coral.reef.web.FileUploadController.java

/**
 * Extracts a zip entry (file entry)/*from w w  w.j a  v  a  2s . c o  m*/
 * 
 * @param zipIn
 * @param filePath
 * @throws IOException
 */
private void extractFile(ZipInputStream zipIn, File filePath) throws IOException {
    filePath.getParentFile().mkdirs();
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    bos.close();
}

From source file:ftpserver.Data.java

public void sendData(String sdata) throws Exception {
    //System.out.print("Sending data on datacon ");
    if (socket == null) {
        Thread.currentThread().sleep(500);
        if (socket == null) {
            throw new IOException("No client connected");
        }//w ww  .j a  v  a2  s.  c o  m
    }
    BufferedOutputStream out = null;
    try {
        out = new BufferedOutputStream(socket.getOutputStream());
        out.write(sdata.getBytes(), 0, sdata.length());
        out.flush();
    } catch (Exception e) {
        throw e;
    }
}

From source file:au.com.addstar.SpigotDirectDownloader.java

public boolean downloadUpdate(ResourceInfo info, File file, Long timeOut)
        throws InvalidDownloadException, ConnectionFailedException {
    try {//from   w w w. j ava  2 s .c  om
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        Resource resource = api.getResourceManager().getResourceById(info.id, spigotUser);
        if (info.premium && !owned.contains(resource.getResourceId())) {
            return false;
        }
        Page page = webClient.getPage(resource.getDownloadURL());
        webClient.waitForBackgroundJavaScript(timeOut); // todo need to add check for need purchase or no auth.
        BufferedInputStream in = new java.io.BufferedInputStream(
                page.getEnclosingWindow().getEnclosedPage().getWebResponse().getContentAsStream());
        java.io.FileOutputStream fos = new java.io.FileOutputStream(file);
        java.io.BufferedOutputStream bout = new BufferedOutputStream(fos, 1024);
        byte[] data = new byte[1024];
        int x;
        while ((x = in.read(data, 0, 1024)) >= 0) {
            bout.write(data, 0, x);
        }
        bout.close();
        in.close();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    Plugin plugin = Plugin.checkDownloadedVer(file);
    if (plugin == null || plugin.getVersion() == null) {
        FileUtils.deleteQuietly(file);
        if (timeOut < 15000L && info.external) {
            downloadUpdate(info, file, timeOut + 5000L);
        }
        throw new InvalidDownloadException("File did not contain a plugin.yml");
    }
    return true;
}

From source file:eu.scape_project.archiventory.container.ZipContainer.java

/**
 * Extracts a zip entry (file entry)/*  ww w  .j  a v a  2s.  c  o  m*/
 * @param zipIn
 * @param filePath
 * @throws IOException
 */
private void extractFile(ZipInputStream zipIn, String filePath) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(filePath));
    byte[] bytesIn = new byte[BUFFER_SIZE];
    int read = 0;
    while ((read = zipIn.read(bytesIn)) != -1) {
        bos.write(bytesIn, 0, read);
    }
    bos.close();
    if ((new File(filePath).exists())) {
        String id = filePath.replaceFirst("[0-9]{10,30}", "");
        //id = id.replace(this.tmpDirName"/tmp", "");
        // key-value pair
        this.put(filePath, filePath);
    }
}

From source file:controller.LoadImageServlet.java

/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request/*w  w w. jav a 2  s .c  om*/
 * @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 {
    String iid = request.getParameter("param1");
    byte[] sImageBytes;
    try {

        Connection con = JdbcConnection.getConnection();
        String Query = "SELECT image FROM user WHERE email ='" + iid + "';";
        System.out.println("Query is" + Query);
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(Query);
        System.out.println("..........1");
        String name = "client_2";
        if (rs.next()) {

            sImageBytes = rs.getBytes("image");

            response.setContentType("image/jpeg");
            response.setContentLength(sImageBytes.length);
            // Give the name of the image in the name variable in the below line   
            response.setHeader("Content-Disposition", "inline; filename=\"" + name + "\"");

            BufferedInputStream input = new BufferedInputStream(new ByteArrayInputStream(sImageBytes));
            BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream());

            byte[] buffer = new byte[8192];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
                System.out.println(".......3");
            }
            output.flush();
        }
    } catch (Exception ex) {
        System.out.println("error :" + ex);
    }
}

From source file:com.gsr.myschool.server.reporting.bilan.ConvocationReportController.java

@RequestMapping(method = RequestMethod.GET, value = "/convocationReport", produces = "application/pdf")
@ResponseStatus(HttpStatus.OK)//  www . java 2  s  .com
public void generateExcel(@RequestParam String fileName, HttpServletRequest request,
        HttpServletResponse response) {
    try {
        final int buffersize = 1024;
        final byte[] buffer = new byte[buffersize];

        response.addHeader("Content-Disposition", "attachment; filename=convocation_report.pdf");

        File file = new File(
                request.getSession().getServletContext().getRealPath("/") + TMP_FOLDER_PATH + fileName);
        InputStream inputStream = new FileInputStream(file);
        BufferedOutputStream outputStream = new BufferedOutputStream(response.getOutputStream());

        int available = 0;
        while ((available = inputStream.read(buffer)) >= 0) {
            outputStream.write(buffer, 0, available);
        }

        inputStream.close();

        outputStream.flush();
        outputStream.close();

        file.delete();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java

public static void unzip(String sourceFile, String destDir) throws IOException {
    BufferedOutputStream dest = null;
    FileInputStream fis = new FileInputStream(sourceFile);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry = null;/*from   ww  w.  j a  va 2 s. c om*/

    int BUFFER_SIZE = 4096;
    while ((entry = zis.getNextEntry()) != null) {
        String dst = destDir + File.separator + entry.getName();
        if (entry.isDirectory()) {
            createDir(destDir, entry);
            continue;
        }
        int count;
        byte data[] = new byte[BUFFER_SIZE];

        // write the file to the disk
        FileOutputStream fos = new FileOutputStream(dst);
        dest = new BufferedOutputStream(fos, BUFFER_SIZE);
        while ((count = zis.read(data, 0, BUFFER_SIZE)) != -1) {
            dest.write(data, 0, count);
        }
        // close the output streams
        dest.flush();
        dest.close();
    }

    zis.close();
}

From source file:org.ktunaxa.referral.server.mvc.UploadGeometryController.java

private URL unzipShape(byte[] fileContent) throws IOException {
    String tempDir = System.getProperty("java.io.tmpdir");

    URL url = null;//from w  w  w.  j  a v  a2  s.  com
    ZipInputStream zin = new ZipInputStream(new ByteArrayInputStream(fileContent));
    try {
        ZipEntry entry;
        while ((entry = zin.getNextEntry()) != null) {
            log.info("Extracting: " + entry);
            String name = tempDir + "/" + entry.getName();
            tempFiles.add(name);
            if (name.endsWith(".shp")) {
                url = new URL("file://" + name);
            }
            int count;
            byte[] data = new byte[BUFFER];
            // write the files to the disk
            deleteFileIfExists(name);
            FileOutputStream fos = new FileOutputStream(name);
            BufferedOutputStream destination = new BufferedOutputStream(fos, BUFFER);
            try {
                while ((count = zin.read(data, 0, BUFFER)) != -1) {
                    destination.write(data, 0, count);
                }
                destination.flush();
            } finally {
                destination.close();
            }
        }
    } finally {
        zin.close();
    }
    if (url == null) {
        throw new IllegalArgumentException("Missing .shp file");
    }
    return url;
}

From source file:eu.scape_project.arc2warc.identification.PayloadContent.java

private byte[] inputStreamToByteArray() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedInputStream buffis = new BufferedInputStream(inputStream);
    BufferedOutputStream buffos = new BufferedOutputStream(baos);
    byte[] tempBuffer = new byte[BUFFER_SIZE];
    int bytesRead;
    boolean firstByteArray = true;
    while ((bytesRead = buffis.read(tempBuffer)) != -1) {
        buffos.write(tempBuffer, 0, bytesRead);
        if (applyIdentification && firstByteArray && tempBuffer != null && bytesRead > 0) {
            identified = identifyPayloadType(tempBuffer);
        }//from w  ww .ja  va 2 s.c  om
        firstByteArray = false;
    }
    buffis.close();
    buffos.flush();
    buffos.close();

    return baos.toByteArray();
}