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(int b) throws IOException 

Source Link

Document

Writes the specified byte to this buffered output stream.

Usage

From source file:backend.translator.GTranslator.java

private File downloadFile(String url) throws IOException {
    HttpClient client = HttpClientBuilder.create().build();
    HttpGet request = new HttpGet(url);
    request.addHeader("User-Agent", "Mozilla/5.0");
    HttpResponse response = client.execute(request);
    HttpEntity entity = response.getEntity();
    BufferedInputStream bis = new BufferedInputStream(entity.getContent());
    File f = new File(R.TRANS_RESULT_PATH);
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f));
    int inByte;/*from  w w  w  . j  a va2 s. c o m*/
    while ((inByte = bis.read()) != -1)
        bos.write(inByte);

    bis.close();
    bos.close();
    return f;
}

From source file:com.clarionmedia.infinitum.internal.caching.RestResponseCache.java

@Override
protected void writeValueToDisk(File file, RestResponse data) throws IOException {
    BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(file));
    outputStream.write(data.getStatusCode());
    byte[] headerData = getHeaderData(data.getHeaders());
    outputStream.write(headerData.length);
    outputStream.write(headerData);/*  w ww .jav a 2 s . co m*/
    outputStream.write(data.getResponseData());
    outputStream.close();
}

From source file:com.facerecog.rest.controller.RecognitionController.java

@RequestMapping(value = ApiUrls.URL_RECOG_UPLOAD_IMAGE, method = RequestMethod.POST)
public String handleFileUpload(@RequestParam("file") MultipartFile file) {
    logger.info("File upload");

    if (!file.isEmpty()) {
        try {/*from w  ww  .  j a  va 2  s .c o  m*/
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(new File(file.getOriginalFilename())));
            stream.write(bytes);
            stream.close();
            return "File uploaded: " + file.getOriginalFilename();
        } catch (Exception e) {
            return "Failed to upload image!";
        }
    } else {
        return "Failed to upload file because the file was empty.";
    }
}

From source file:com.lugia.timetable.SubjectList.java

public boolean saveToFile(Context context) {
    try {/*  w ww .  ja  v a  2  s  . c om*/
        FileOutputStream out = context.openFileOutput(SAVEFILE, Context.MODE_PRIVATE);
        BufferedOutputStream stream = new BufferedOutputStream(out);

        stream.write(generateJSON().toString().getBytes());

        stream.flush();
        stream.close();

        out.close();
    } catch (Exception e) {
        // something went wrong
        Log.e(TAG, "Error on save!", e);

        return false;
    }

    return true;
}

From source file:com.orchestra.portale.controller.UserEditController.java

@RequestMapping(value = "userEditProfile", method = RequestMethod.POST)
@Secured("ROLE_USER")
public ModelAndView updateUser(HttpServletRequest request, @ModelAttribute("SpringWeb") User user,
        MultipartFile avatar) {/*  ww  w.j av  a  2 s .  co m*/
    ModelAndView model = new ModelAndView("userInfo");
    User olduser = pm.findUserByUsername(user.getUsername());
    user.setId(olduser.getId());

    if (user.getPassword() == null || user.getPassword().equals("")) {
        user.setPassword(olduser.getPassword());
    } else {
        user.setPassword(crypt(user.getPassword()));
    }

    pm.saveUser(user);

    if (avatar.getSize() > 0) {

        User user2 = pm.findUserByUsername(user.getUsername());
        MultipartFile file = avatar;

        try {
            byte[] bytes = file.getBytes();

            // Creating the directory to store file
            HttpSession session = request.getSession();
            ServletContext sc = session.getServletContext();

            File dir = new File(sc.getRealPath("/") + "dist" + File.separator + "user" + File.separator + "img"
                    + File.separator + user2.getId());
            if (!dir.exists()) {
                dir.mkdirs();
            }

            // Create the file on server
            File serverFile = new File(dir.getAbsolutePath() + File.separator + "avatar.jpg");
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(serverFile));
            stream.write(bytes);
            stream.close();
        } catch (Exception e) {

        }

    }
    model.addObject("user", user);
    HttpSession session = request.getSession();
    ServletContext sc = session.getServletContext();
    File dir = new File(sc.getRealPath("/") + "dist" + File.separator + "user" + File.separator + "img"
            + File.separator + user.getId() + File.separator + "avatar.jpg");
    if (dir.exists()) {
        model.addObject("avatar", "./dist/user/img/" + user.getId() + "/avatar.jpg");
    } else {
        model.addObject("avatar", "./dist/img/default_avatar.png");
    }
    return model;
}

From source file:com.metamolecular.pubcouch.test.SnapshotTest.java

private byte[] zipStringToBytes(String input) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    BufferedOutputStream bufos = new BufferedOutputStream(new GZIPOutputStream(bos));
    bufos.write(input.getBytes("UTF-8"));
    bufos.close();//from   w w w  . ja v  a 2  s  .com
    byte[] retval = bos.toByteArray();
    bos.close();
    return retval;
}

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 ww w  .ja  v a2s  .c o m
 * 
 * @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:com.kalai.controller.FileUploadController.java

@RequestMapping(value = "/fileupload", method = RequestMethod.POST)
public @ResponseBody String fileuploadstore(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file, ModelMap map) {
    if (!file.isEmpty()) {
        try {//  ww w.j av a2 s.  co  m
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(new File(name)));
            stream.write(bytes);
            stream.close();
            map.addAttribute("uploadoption", "uploaded");
            map.addAttribute("Status", "uploaded Successfully" + name);

        } catch (Exception e) {
            map.addAttribute("uploadoption", "uploaded");
            map.addAttribute("Status", "uploaded UnSuccessfully" + name);
        }
    } else {
        map.addAttribute("Status", "uploaded  is Empty" + name);
    }
    return "fileupload";
}

From source file:edu.vt.middleware.crypt.signature.SignatureCliTest.java

/**
 * @param  partialLine  Partial command line.
 * @param  pubKey  Public key file.// w  w  w . j  a  v  a2 s  . c o m
 * @param  privKey  Private key file.
 *
 * @throws  Exception  On test failure.
 */
@Test(groups = { "cli", "signature" }, dataProvider = "testdata")
public void testSignatureCli(final String partialLine, final String pubKey, final String privKey)
        throws Exception {
    final String pubKeyPath = KEY_DIR_PATH + pubKey;
    final String privKeyPath = KEY_DIR_PATH + privKey;
    final PrintStream oldStdOut = System.out;
    try {
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outStream));

        // Compute signature and verify it
        String fullLine = partialLine + " -sign " + " -key " + privKeyPath + " -in " + TEST_PLAINTEXT;
        logger.info("Testing signature CLI sign operation with command line:\n\t" + fullLine);
        SignatureCli.main(CliHelper.splitArgs(fullLine));

        final String sig = outStream.toString();
        Assert.assertTrue(sig.length() > 0);

        // Write signature out to file for use in verify step
        new File(TEST_OUTPUT_DIR).mkdir();

        final File sigFile = new File(TEST_OUTPUT_DIR + "sig.out");
        final BufferedOutputStream sigOs = new BufferedOutputStream(new FileOutputStream(sigFile));
        try {
            sigOs.write(outStream.toByteArray());
        } finally {
            sigOs.close();
        }
        outStream.reset();

        // Verify signature
        fullLine = partialLine + " -verify " + sigFile + " -key " + pubKeyPath + " -in " + TEST_PLAINTEXT;
        logger.info("Testing signature CLI verify operation with command " + "line:\n\t" + fullLine);
        SignatureCli.main(CliHelper.splitArgs(fullLine));

        final String result = outStream.toString();
        AssertJUnit.assertTrue(result.indexOf("SUCCESS") != -1);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        // Restore STDOUT
        System.setOut(oldStdOut);
    }
}

From source file:com.athena.peacock.controller.web.as.AutoScalingController.java

@RequestMapping("/xml")
public void xml(HttpServletRequest request, HttpServletResponse response) throws Exception {
    //response.setContentType("application/xml");
    response.setContentType("text/xml");

    try {//from   w  ww. ja v a  2s.c o m
        File file = new File(getClass().getResource("/spring/context-common.xml").getFile());
        BufferedInputStream fin = new BufferedInputStream(new FileInputStream(file));
        BufferedOutputStream outs = new BufferedOutputStream(response.getOutputStream());

        int read = 0;
        while ((read = fin.read()) != -1) {
            outs.write(read);
        }

        IOUtils.closeQuietly(fin);
        IOUtils.closeQuietly(outs);
    } catch (Exception e) {
        e.printStackTrace();
    }
}