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

static public String downloadFile(String downloadUrl, String fileName, String dir) throws IOException {
    URL url = new URL(downloadUrl);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);/*from  w  w w .  j  ava  2s  .c  o m*/
    urlConnection.connect();
    int code = urlConnection.getResponseCode();
    if (code > 300 || code == -1) {
        throw new IOException("Cannot read url: " + downloadUrl);
    }
    String filePath = prepareFilePath(fileName, dir);
    String tmpFilePath = prepareTmpFilePath(fileName, dir);
    FileOutputStream fileOutput = new FileOutputStream(tmpFilePath);
    BufferedInputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
    byte[] buffer = new byte[1024];
    int bufferLength;
    while ((bufferLength = inputStream.read(buffer)) > 0) {
        fileOutput.write(buffer, 0, bufferLength);
    }
    fileOutput.close();
    // move tmp to destination
    copyFile(new File(tmpFilePath), new File(filePath));
    return filePath;
}

From source file:Main.java

/**
 * Method to save audio file to SD card//  w w w .  j a  v  a 2 s. c  o  m
 * @param course_title, title of course
 * @param format, the file format, for example .mp3, .txt
 */
public static boolean copyToStorage(File source_file, String course_title, String format) {

    File output_file = getEmptyFileWithStructuredPath(course_title, format);

    try {
        byte[] buffer = new byte[4096];
        int bytesToHold;

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source_file));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(output_file));

        while ((bytesToHold = bis.read(buffer)) != -1) {
            bos.write(buffer, 0, bytesToHold);
        }

        bos.flush();
        bos.close();

        return true;

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return false;
}

From source file:Main.java

/**
 * //from www  . jav  a 2 s  .  c o m
 * @param zipFile
 *            zip file
 * @param folderName
 *            folder to load
 * @return list of entry in the folder
 * @throws ZipException
 * @throws IOException
 */
public static List<ZipEntry> loadFiles(File zipFile, String folderName) throws ZipException, IOException {
    Log.d("loadFiles", folderName);
    long start = System.currentTimeMillis();
    FileInputStream fis = new FileInputStream(zipFile);
    BufferedInputStream bis = new BufferedInputStream(fis);
    ZipInputStream zis = new ZipInputStream(bis);

    ZipEntry zipEntry = null;
    List<ZipEntry> list = new LinkedList<ZipEntry>();
    String folderLowerCase = folderName.toLowerCase();
    int counter = 0;
    while ((zipEntry = zis.getNextEntry()) != null) {
        counter++;
        String zipEntryName = zipEntry.getName();
        Log.d("loadFiles.zipEntry.getName()", zipEntryName);
        if (zipEntryName.toLowerCase().startsWith(folderLowerCase) && !zipEntry.isDirectory()) {
            list.add(zipEntry);
        }
    }
    Log.d("Loaded 1", counter + " files, took: " + (System.currentTimeMillis() - start) + " milliseconds.");
    zis.close();
    bis.close();
    fis.close();
    return list;
}

From source file:Main.java

public static void copyFile(File in, File out) throws IOException {
    // avoids copying a file to itself
    if (in.equals(out)) {
        return;/*from  w  w  w . jav  a2  s  .co  m*/
    }
    // ensure the output file location exists
    out.getCanonicalFile().getParentFile().mkdirs();

    BufferedInputStream fis = new BufferedInputStream(new FileInputStream(in));
    BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(out));

    // a temporary buffer to read into
    byte[] tmpBuffer = new byte[8192];
    int len = 0;
    while ((len = fis.read(tmpBuffer)) != -1) {
        // add the temp data to the output
        fos.write(tmpBuffer, 0, len);
    }
    // close the input stream
    fis.close();
    // close the output stream
    fos.flush();
    fos.close();
}

From source file:com.l1j5.web.common.utils.ImageUtils.java

public static void createThumbnail(String load, String save, String type, int w, int h) {

    try {/* w w w  . ja v  a 2 s  .  c  om*/
        BufferedInputStream stream_file = new BufferedInputStream(new FileInputStream(load));
        createThumbnail(stream_file, save, type, w, h);
    } catch (Exception e) {
        log.error(e);
    }

}

From source file:com.digitallizard.bbcnewsreader.resource.web.HtmlParser.java

/**
 * @param args/*  w w w  . jav  a 2s. co  m*/
 * @throws IOException
 * @throws ClientProtocolException
 */
public static byte[] getPage(String stringUrl) throws Exception {
    URL url = new URL(stringUrl);
    URLConnection connection = url.openConnection();

    InputStream stream = connection.getInputStream();
    BufferedInputStream inputbuffer = new BufferedInputStream(stream);

    ByteArrayBuffer arraybuffer = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = inputbuffer.read()) != -1) {
        arraybuffer.append((byte) current);
    }
    return arraybuffer.toByteArray();
}

From source file:Main.java

public static SocketFactory getSocketFactoryWithCustomCA(InputStream stream) throws CertificateException,
        KeyStoreException, IOException, NoSuchAlgorithmException, KeyManagementException {

    // Load CAs from an InputStream
    // (could be from a resource or ByteArrayInputStream or ...)
    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    InputStream caInput = new BufferedInputStream(stream);
    Certificate ca;//www  . j  a  v  a  2 s. co m
    try {
        ca = cf.generateCertificate(caInput);
        System.out.println("ca=" + ((X509Certificate) ca).getSubjectDN());
    } finally {
        try {
            caInput.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Create a KeyStore containing our trusted CAs
    String keyStoreType = KeyStore.getDefaultType();
    KeyStore keyStore = KeyStore.getInstance(keyStoreType);
    keyStore.load(null, null);
    keyStore.setCertificateEntry("ca", ca);

    // Create a TrustManager that trusts the CAs in our KeyStore
    String tmfAlgorithm = TrustManagerFactory.getDefaultAlgorithm();
    TrustManagerFactory tmf = TrustManagerFactory.getInstance(tmfAlgorithm);
    tmf.init(keyStore);

    // Create an SSLContext that uses our TrustManager
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, tmf.getTrustManagers(), null);

    return context.getSocketFactory();
}

From source file:Main.java

static public InputStream loadImage(String url) {
    HttpURLConnection connection = null;
    InputStream is = null;/* w w w.  j  a v  a  2  s.  c o m*/

    try {
        connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setConnectTimeout(15000);

        is = new BufferedInputStream(connection.getInputStream());
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }

    return is;
}

From source file:Main.java

public static Object unmarshall(String cntxtPkg, File xmlFile) throws JAXBException, SAXException, IOException {
    return unmarshall(cntxtPkg, new BufferedInputStream(new FileInputStream(xmlFile)));
}

From source file:Main.java

public static String accessToFlashAir(String uri) throws IOException {
    URL url = new URL(uri);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

    String result = null;/* w w  w  . j av  a  2s.c o  m*/
    try {
        InputStream in = new BufferedInputStream(urlConnection.getInputStream());
        result = inputStreamToString(in);
        in.close();
    } finally {
        urlConnection.disconnect();
    }

    return result;
}