Example usage for java.io BufferedOutputStream BufferedOutputStream

List of usage examples for java.io BufferedOutputStream BufferedOutputStream

Introduction

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

Prototype

public BufferedOutputStream(OutputStream out) 

Source Link

Document

Creates a new buffered output stream to write data to the specified underlying output stream.

Usage

From source file:azkaban.jobtype.connectors.teradata.TeraDataWalletInitializer.java

/**
 * As of TDCH 1.4.1, integration with Teradata wallet only works with hadoop jar command line command.
 * This is mainly because TDCH depends on the behavior of hadoop jar command line which extracts jar file
 * into hadoop tmp folder.//from   w w w  . j a v  a  2  s  .c om
 *
 * This method will extract tdchJarfile and place it into temporary folder, and also add jvm shutdown hook
 * to delete the directory when JVM shuts down.
 * @param tdchJarFile TDCH jar file.
 */
public static void initialize(File tmpDir, File tdchJarFile) {
    synchronized (TeraDataWalletInitializer.class) {
        if (tdchJarExtractedDir != null) {
            return;
        }

        if (tdchJarFile == null) {
            throw new IllegalArgumentException("TDCH jar file cannot be null.");
        }
        if (!tdchJarFile.exists()) {
            throw new IllegalArgumentException(
                    "TDCH jar file does not exist. " + tdchJarFile.getAbsolutePath());
        }
        try {
            //Extract TDCH jar.
            File unJarDir = createUnjarDir(
                    new File(tmpDir.getAbsolutePath() + File.separator + UNJAR_DIR_NAME));
            JarFile jar = new JarFile(tdchJarFile);
            Enumeration<JarEntry> enumEntries = jar.entries();

            while (enumEntries.hasMoreElements()) {
                JarEntry srcFile = enumEntries.nextElement();
                File destFile = new File(unJarDir + File.separator + srcFile.getName());
                if (srcFile.isDirectory()) { // if its a directory, create it
                    destFile.mkdir();
                    continue;
                }

                InputStream is = jar.getInputStream(srcFile);
                BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(destFile));
                IOUtils.copy(is, os);
                close(os);
                close(is);
            }
            jar.close();
            tdchJarExtractedDir = unJarDir;
        } catch (IOException e) {
            throw new RuntimeException("Failed while extracting TDCH jar file.", e);
        }
    }
    logger.info("TDCH jar has been extracted into directory: " + tdchJarExtractedDir.getAbsolutePath());
}

From source file:com.rometools.propono.atom.server.impl.FileStore.java

public OutputStream getFileOutputStream(final String path) {
    LOG.debug("getFileOutputStream path: " + path);
    try {/*from w  ww  .  ja  va2s.  co m*/
        final File f = new File(path);
        f.getParentFile().mkdirs();
        return new BufferedOutputStream(new FileOutputStream(f));
    } catch (final FileNotFoundException e) {
        LOG.debug("   File not found: " + path);
        return null;
    }
}

From source file:com.sun.syndication.propono.atom.server.impl.FileStore.java

public OutputStream getFileOutputStream(String path) {
    log.debug("getFileOutputStream path: " + path);
    try {/*from   w  ww.  j  a v a 2s  .c om*/
        File f = new File(path);
        f.getParentFile().mkdirs();
        return new BufferedOutputStream(new FileOutputStream(f));
    } catch (FileNotFoundException e) {
        log.debug("   File not found: " + path);
        return null;
    }
}

From source file:julie.com.mikaelson.file.controller.FileInfoController.java

public @ResponseBody String handleFileUpload(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file) {
    if (!file.isEmpty()) {
        try {//from   w w  w  .  j a v a  2s  . c om
            byte[] bytes = file.getBytes();
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(new File(name + "-uploaded")));
            stream.write(bytes);
            stream.close();
            return "You successfully uploaded " + name + " into " + name + "-uploaded !";
        } catch (Exception e) {
            return "You failed to upload " + name + " => " + e.getMessage();
        }
    } else {
        return "You failed to upload " + name + " because the file was empty.";
    }
}

From source file:FileMonitor.java

/**
 * Copy the source file system structure into the supplied target location. If
 * the source is a file, the destiniation will be created as a file; if the
 * source is a directory, the destination will be created as a directory.
 * /*  w w  w.ja  va2s. c om*/
 * @param sourceFileOrDirectory
 *          the file or directory whose contents are to be copied into the
 *          target location
 * @param destinationFileOrDirectory
 *          the location where the copy is to be placed; does not need to
 *          exist, but if it does its type must match that of <code>src</code>
 * @return the number of files (not directories) that were copied
 * @throws IllegalArgumentException
 *           if the <code>src</code> or <code>dest</code> references are
 *           null
 * @throws IOException
 */
public static int copy(File sourceFileOrDirectory, File destinationFileOrDirectory) throws IOException {
    int numberOfFilesCopied = 0;
    if (sourceFileOrDirectory.isDirectory()) {
        destinationFileOrDirectory.mkdirs();
        String list[] = sourceFileOrDirectory.list();

        for (int i = 0; i < list.length; i++) {
            String dest1 = destinationFileOrDirectory.getPath() + File.separator + list[i];
            String src1 = sourceFileOrDirectory.getPath() + File.separator + list[i];
            numberOfFilesCopied += copy(new File(src1), new File(dest1));
        }
    } else {
        InputStream fin = new FileInputStream(sourceFileOrDirectory);
        fin = new BufferedInputStream(fin);
        try {
            OutputStream fout = new FileOutputStream(destinationFileOrDirectory);
            fout = new BufferedOutputStream(fout);
            try {
                int c;
                while ((c = fin.read()) >= 0) {
                    fout.write(c);
                }
            } finally {
                fout.close();
            }
        } finally {
            fin.close();
        }
        numberOfFilesCopied++;
    }
    return numberOfFilesCopied;
}

From source file:com.foreignreader.util.WordNetExtractor.java

@Override
protected Void doInBackground(InputStream... streams) {
    assert streams.length == 1;

    try {/*from  ww w.  j  a  v  a 2 s. co m*/

        BufferedInputStream in = new BufferedInputStream(streams[0]);
        GzipCompressorInputStream gzIn = new GzipCompressorInputStream(in);

        TarArchiveInputStream tar = new TarArchiveInputStream(gzIn);

        TarArchiveEntry tarEntry;

        File dest = LongTranslationHelper.getWordNetDict().getParentFile();

        while ((tarEntry = tar.getNextTarEntry()) != null) {
            File destPath = new File(dest, tarEntry.getName());
            if (tarEntry.isDirectory()) {
                destPath.mkdirs();
            } else {
                destPath.createNewFile();
                byte[] btoRead = new byte[1024 * 10];

                BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(destPath));
                int len = 0;

                while ((len = tar.read(btoRead)) != -1) {
                    bout.write(btoRead, 0, len);
                }

                bout.close();

            }
        }

        tar.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:net.duckling.ddl.util.FileUtil.java

/**
 * Brief Intro Here/*from w w  w  .  j  a  v a  2 s . c  o  m*/
 * ?
 * @param
 */
public static void copyFile(File sourceFile, File targetFile) throws IOException {
    // ?
    FileInputStream input = new FileInputStream(sourceFile);
    BufferedInputStream inBuff = new BufferedInputStream(input);

    // ?
    FileOutputStream output = new FileOutputStream(targetFile);
    BufferedOutputStream outBuff = new BufferedOutputStream(output);

    // 
    byte[] b = new byte[1024 * 5];
    int len;
    while ((len = inBuff.read(b)) != -1) {
        outBuff.write(b, 0, len);
    }
    // ?
    outBuff.flush();

    // ?
    inBuff.close();
    outBuff.close();
    output.close();
    input.close();
}

From source file:edu.vt.middleware.crypt.io.Base64FilterOutputStreamTest.java

/**
 * @param  charsPerLine  Number of characters per line in encoded data file.
 *
 * @throws  Exception  On test failure./*from ww w .j a va  2 s  . c o  m*/
 */
@Test(groups = { "functest", "io", "encodeBase64" }, dataProvider = "testdata")
public void testEncodeBase64(final Integer charsPerLine) throws Exception {
    logger.info("Writing encoded base64 file with " + charsPerLine + " characters per line.");

    final String outPath = "target/test-output/encoded-base64-" + charsPerLine + ".txt";
    new File(outPath).getParentFile().mkdir();

    final InputStream in = getClass().getResourceAsStream(TEXT_FILE_PATH);
    final OutputStream out = new Base64FilterOutputStream(
            new BufferedOutputStream(new FileOutputStream(new File(outPath))), charsPerLine.intValue());
    try {
        int count = 0;
        final int bufsize = 2048;
        final byte[] buffer = new byte[bufsize];
        while ((count = in.read(buffer)) > 0) {
            out.write(buffer, 0, count);
        }
    } finally {
        if (in != null) {
            in.close();
        }
        if (out != null) {
            out.close();
        }
    }

    final InputStream inRef = getClass()
            .getResourceAsStream("/edu/vt/middleware/crypt/io/base64-" + charsPerLine + ".txt");
    final InputStream inTest = new FileInputStream(new File(outPath));
    try {
        AssertJUnit.assertTrue(FileHelper.equal(inRef, inTest));
    } finally {
        if (inRef != null) {
            inRef.close();
        }
        if (inTest != null) {
            inTest.close();
        }
    }
}

From source file:com.sastix.cms.common.services.htmltopdf.PdfImpl.java

private File saveAs(String path, byte[] document) throws IOException {
    File file = new File(path);

    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
    bufferedOutputStream.write(document);
    bufferedOutputStream.flush();/*from   w ww  . ja v  a2 s  . c o  m*/
    bufferedOutputStream.close();

    return file;
}

From source file:it.inserpio.mapillary.gopro.importer.exif.EXIFPropertyWriter.java

public static void setExifGPSTag(File jpegImageFile, File jpegImageOutputFile, Coordinates coordinates)
        throws IOException, ImageReadException, ImageWriteException {
    OutputStream os = null;/*from   www .ja v  a2s.  c  om*/

    boolean canThrow = false;

    try {
        TiffOutputSet outputSet = null;

        final IImageMetadata metadata = Imaging.getMetadata(jpegImageFile);
        final JpegImageMetadata jpegMetadata = (JpegImageMetadata) metadata;

        if (jpegMetadata != null) {
            final TiffImageMetadata exif = jpegMetadata.getExif();

            if (exif != null) {
                outputSet = exif.getOutputSet();
            }
        }

        if (outputSet == null) {
            outputSet = new TiffOutputSet();
        }

        outputSet.setGPSInDegrees(coordinates.getLongitude(), coordinates.getLatitude());

        outputSet.getGPSDirectory().removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF);
        outputSet.getGPSDirectory().add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION_REF,
                GpsTagConstants.GPS_TAG_GPS_DEST_BEARING_REF_VALUE_TRUE_NORTH);

        outputSet.getGPSDirectory().removeField(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION);
        outputSet.getGPSDirectory().add(GpsTagConstants.GPS_TAG_GPS_IMG_DIRECTION,
                new RationalNumber[] { RationalNumber.valueOf(coordinates.getDirection()) });

        //outputSet.getRootDirectory().removeField(305);

        os = new FileOutputStream(jpegImageOutputFile);
        os = new BufferedOutputStream(os);

        new ExifRewriter().updateExifMetadataLossless(jpegImageFile, os, outputSet);

        canThrow = true;
    } finally {
        IoUtils.closeQuietly(canThrow, os);
    }
}