Example usage for java.io BufferedInputStream BufferedInputStream

List of usage examples for java.io BufferedInputStream BufferedInputStream

Introduction

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

Prototype

public BufferedInputStream(InputStream in) 

Source Link

Document

Creates a BufferedInputStream and saves its argument, the input stream in, for later use.

Usage

From source file:Main.java

/**
 * Method read./*from  www .j  a  v a  2s .co  m*/
 * 
 * @param filepath
 *            String
 * @return Properties
 * @throws FileNotFoundException
 */
public static Properties read(String filepath) throws FileNotFoundException {
    Properties rval;
    BufferedInputStream bis = null;

    try {
        File file = new File(filepath);

        bis = new BufferedInputStream(new FileInputStream(file));
        rval = new Properties();

        rval.load(bis);

        return rval;
    } catch (IOException e) {
    } finally {
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException e) {
            }

            bis = null;
        }
    }

    // try relative to java.home
    try {
        File file = new File(System.getProperty("java.home") + File.separator + filepath);

        bis = new BufferedInputStream(new FileInputStream(file));
        rval = new Properties();

        rval.load(bis);

        return rval;
    } catch (IOException e) {
    } finally {
        if (bis != null) {
            try {
                bis.close();
            } catch (IOException e) {
            }

            bis = null;
        }
    }

    throw new FileNotFoundException("Property file " + filepath + " not found");
}

From source file:Thumbnail.java

public void doWork(String[] args) {
    try {/*from  w  w  w. ja  v  a2 s . co m*/
        byte[] bytes = new byte[50000];

        FileInputStream fs = new FileInputStream(args[2]);
        BufferedInputStream bis = new BufferedInputStream(fs);
        bis.read(bytes);

        ID id = new ID();
        id.nail_id = Integer.parseInt(args[0]);
        id.acc_id = Integer.parseInt(args[1]);

        statement = connection.prepareStatement("INSERT INTO thumbnail VALUES(?,?,?,?, 0, now())");

        statement.setInt(1, id.nail_id);
        statement.setInt(2, id.acc_id);
        statement.setBytes(3, bytes);
        statement.setObject(4, id);

        int i = statement.executeUpdate();
        System.out.println("Rows updated = " + i);

        bis.close();
        fs.close();
        statement.close();
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Do an HTTP POST and return the data as a byte array.
 *///from   w  w w.  ja  va2s . c  om
public static byte[] executePost(String url, byte[] data, Map<String, String> requestProperties)
        throws MalformedURLException, IOException {
    HttpURLConnection urlConnection = null;
    try {
        urlConnection = (HttpURLConnection) new URL(url).openConnection();
        urlConnection.setRequestMethod("POST");
        urlConnection.setDoOutput(data != null);
        urlConnection.setDoInput(true);
        if (requestProperties != null) {
            for (Map.Entry<String, String> requestProperty : requestProperties.entrySet()) {
                urlConnection.setRequestProperty(requestProperty.getKey(), requestProperty.getValue());
            }
        }
        if (data != null) {
            OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
            out.write(data);
            out.close();
        }
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        return convertInputStreamToByteArray(in);
    } finally {
        if (urlConnection != null) {
            urlConnection.disconnect();
        }
    }
}

From source file:Main.java

/**
 * Read and return the entire contents of the supplied {@link File file}.
 * /*from w w w  .j  ava  2 s . c  o m*/
 * @param file the file containing the contents; may be null
 * @return the contents, or an empty byte array if the supplied file is null
 * @throws IOException if there is an error reading the content
 */
public static byte[] readBytes(File file) throws IOException {
    if (file == null)
        return new byte[] {};
    InputStream stream = new BufferedInputStream(new FileInputStream(file));
    boolean error = false;
    try {
        return readBytes(stream);
    } catch (IOException e) {
        error = true; // this error should be thrown, even if there is an error closing stream
        throw e;
    } catch (RuntimeException e) {
        error = true; // this error should be thrown, even if there is an error closing stream
        throw e;
    } finally {
        try {
            stream.close();
        } catch (IOException e) {
            if (!error)
                throw e;
        }
    }
}

From source file:net.sf.zekr.engine.template.ZekrFileResourceLoader.java

public synchronized InputStream getResourceStream(String templateName) throws ResourceNotFoundException {
    InputStream is = null;//from  w  ww .  j av  a  2  s .co m
    try {
        is = new BufferedInputStream(new FileInputStream(new File(templateName).getAbsolutePath()));
    } catch (FileNotFoundException e) {
        // do nothing!
    }
    if (is != null) // if no exception occurred
        return is;

    throw new ResourceNotFoundException("Resource not found: " + templateName);
}

From source file:com.hs.mail.sieve.Sieve.java

public static boolean runSieve(MailetContext context, Recipient recipient, SmtpMessage msg) {
    File script = getScript(context, recipient);
    if (script != null) {
        InputStream is = null;/*from  ww  w  .j av  a  2 s . c  o m*/
        try {
            SieveMailAdapter adapter = new SieveMailAdapter(context, recipient.getMailbox(), recipient.getID());
            adapter.setMessage(msg);
            is = new BufferedInputStream(new FileInputStream(script));
            factory.interpret(adapter, is);
            return true;
        } catch (Exception e) {
            logger.error(e.getMessage());
        } finally {
            IOUtils.closeQuietly(is);
        }
    }
    return false;
}

From source file:de.innovationgate.utils.MD5HashingInputStream.java

/**
 * Reads the given input stream and returns the data hash base64 encoded. The input data is not conserved.
 * Use this method if you really just want to know what hash the given data produces, but do not want to use the data actually.
 * The input stream is closed implicitly.
 * @param in The input stream/*  www.j  a v  a 2  s.co  m*/
 * @return The md5 hash of the input stream
 * @throws NoSuchAlgorithmException
 * @throws IOException
 */
public static String getStreamHash(InputStream in) throws NoSuchAlgorithmException, IOException {

    MD5HashingInputStream hashIn = new MD5HashingInputStream(new BufferedInputStream(in));
    byte[] buf = new byte[2048];
    int len;
    do {
        len = hashIn.read(buf);
    } while (len != -1);

    hashIn.close();
    return hashIn.getHash();

}

From source file:org.crazydog.util.spring.FileCopyUtils.java

/**
 * Copy the contents of the given input File to the given output File.
 * @param in the file to copy from/*from ww w . java2 s  .com*/
 * @param out the file to copy to
 * @return the number of bytes copied
 * @throws IOException in case of I/O errors
 */
public static int copy(File in, File out) throws IOException {
    org.springframework.util.Assert.notNull(in, "No input File specified");
    org.springframework.util.Assert.notNull(out, "No output File specified");
    return copy(new BufferedInputStream(new FileInputStream(in)),
            new BufferedOutputStream(new FileOutputStream(out)));
}

From source file:com.googlecode.xmlzen.utils.FileUtils.java

/**
 * Reads a {@link File} and returns the contents as {@link String}
 * //www.j  ava2  s . co  m
 * @param file File to read
 * @param charset Charset of this File
 * @return File contents as String
 */
public static String readFile(final File file, final String charset) {
    InputStream in = null;
    try {
        if (!file.isFile()) {
            return null;
        }
        in = new FileInputStream(file);
        final BufferedInputStream buffIn = new BufferedInputStream(in);
        final byte[] buffer = new byte[(int) Math.min(file.length(), BUFFER)];
        int read;
        final StringBuilder result = new StringBuilder();
        while ((read = buffIn.read(buffer)) != -1) {
            result.append(new String(buffer, 0, read, charset));
        }
        return result.toString();
    } catch (final Exception e) {
        throw new XmlZenException("Failed reading file: " + file, e);
    } finally {
        close(in);
    }
}

From source file:io.rhiot.component.webcam.WebcamHelper.java

/**
 * Creates an OutOnly exchange with the BufferedImage.
 *///from  ww w  .j  a va2s.  c  o  m
static Exchange createOutOnlyExchangeWithBodyAndHeaders(WebcamEndpoint endpoint, BufferedImage image)
        throws IOException {
    Exchange exchange = endpoint.createExchange(ExchangePattern.OutOnly);
    Message message = exchange.getIn();
    try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
        ImageIO.write(image, endpoint.getFormat(), output);
        message.setBody(new BufferedInputStream(new ByteArrayInputStream(output.toByteArray())));
        message.setHeader(Exchange.FILE_NAME, message.getMessageId() + "" + endpoint.getFormat());
    }

    return exchange;
}