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

public static String readTextFile(URL url) throws IOException {
    if (url == null)
        return "";
    BufferedInputStream bis = null;
    StringBuffer sb = new StringBuffer();
    try {/*w ww . java  2  s .co  m*/
        bis = new BufferedInputStream(url.openStream());
        byte buff[] = new byte[2048];
        int bytesRead;
        while (-1 != (bytesRead = bis.read(buff, 0, buff.length)))
            sb.append(new String(buff, 0, bytesRead));
    } catch (IOException e) {
        throw e;
    }
    return sb.toString();
}

From source file:Main.java

public static boolean copyFile2(File src, File dst) {
    FileInputStream i;/*  w w  w .j a  v  a 2  s .com*/
    try {
        i = new FileInputStream(src);
        BufferedInputStream in = new BufferedInputStream(i);
        FileOutputStream o = new FileOutputStream(dst);
        BufferedOutputStream out = new BufferedOutputStream(o);
        byte[] b = new byte[1024 * 5];
        int len;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
        out.flush();
        in.close();
        out.close();
        o.close();
        i.close();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

private static void downloadResource(String from, String to) {
    OutputStream outputStream = null;
    BufferedInputStream inputStream = null;
    HttpURLConnection connection = null;
    URL url;/*from   w w  w .  ja  v  a  2  s.co m*/
    byte[] buffer = new byte[1024];

    try {
        url = new URL(from);

        connection = (HttpURLConnection) url.openConnection();

        connection.setRequestMethod("GET");
        connection.setDoOutput(true);
        connection.connect();
        outputStream = new FileOutputStream(to);

        inputStream = new BufferedInputStream(url.openStream());
        int read;

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

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (outputStream != null)
            try {
                outputStream.close();
            } catch (IOException e) {
            }
        if (inputStream != null)
            try {
                inputStream.close();
            } catch (IOException e) {
            }
        if (connection != null)
            connection.disconnect();
    }
}

From source file:MainClass.java

public static void desEncrypt(String f1, String f2) throws Exception {
    SecretKey key = null;//ww w  . java 2  s .  com

    ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("DESKey.ser"));
    key = (SecretKey) keyFile.readObject();
    keyFile.close();
    Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key);
    CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(f1)), cipher);
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f2));
    int i;
    do {
        i = in.read();
        if (i != -1)
            out.write(i);
    } while (i > 0);
    in.close();
    out.close();
}

From source file:Main.java

public static String readData(Context mContext, String key, int resId) {
    Properties props = new Properties();
    try {//  w  w w  .  ja v  a2 s . c  o  m
        InputStream in = new BufferedInputStream(mContext.getResources().openRawResource(resId));
        props.load(in);
        in.close();
        String value = props.getProperty(key);
        return value;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:MainClass.java

public static void blowfishEncrypt(String f1, String f2) throws Exception {
    SecretKey key = null;// w  ww . j a  v a  2  s .  co m

    ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("BlowfishKey.ser"));
    key = (SecretKey) keyFile.readObject();
    keyFile.close();
    Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, key);
    CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(f1)), cipher);
    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f2));
    int i;
    do {
        i = in.read();
        if (i != -1)
            out.write(i);
    } while (i > 0);
    in.close();
    out.close();
}

From source file:TestPipes.java

public static void readData(InputStream is) {
    DataInputStream in = new DataInputStream(new BufferedInputStream(is));
    boolean eof = false;
    try {/*from   w  ww .  j  a  v  a  2s . c o m*/
        while (!eof) {
            int iValue = in.readInt();
            System.out.println("read value = " + iValue);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    System.out.println("End of Data");
}

From source file:Main.java

/**
 * Creates an SSLSocketFactory which contains {@code certChainFile} as its only root certificate.
 *///from ww  w  .j a v a  2s.c  o  m
public static SSLSocketFactory newSslSocketFactoryForCa(InputStream certChain) throws Exception {
    KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(null, null);
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate cert = (X509Certificate) cf.generateCertificate(new BufferedInputStream(certChain));
    X500Principal principal = cert.getSubjectX500Principal();
    ks.setCertificateEntry(principal.getName("RFC2253"), cert);
    //    ks.setCertificateEntry("ca", cert);

    // Set up trust manager factory to use our key store.
    TrustManagerFactory trustManagerFactory = TrustManagerFactory
            .getInstance(TrustManagerFactory.getDefaultAlgorithm());
    trustManagerFactory.init(ks);
    SSLContext context = SSLContext.getInstance("TLS");
    context.init(null, trustManagerFactory.getTrustManagers(), null);
    return context.getSocketFactory();
}

From source file:Main.java

public static String getCharset1(String fileName) throws IOException {
    BufferedInputStream bin = new BufferedInputStream(new FileInputStream(fileName));
    int p = (bin.read() << 8) + bin.read();

    String code;// w  ww .jav  a 2  s.  c o  m
    switch (p) {
    case 0xefbb:
        code = "UTF-8";
        break;
    case 0xfffe:
        code = "Unicode";
        break;
    case 0xfeff:
        code = "UTF-16BE";
        break;
    default:
        code = "GBK";
    }
    return code;
}

From source file:es.logongas.iothome.agent.ConfigLoader.java

public static Config getConfig(String fileName) {
    Config config;/*from   w ww .  j av  a  2s.c  om*/
    InputStream inputStream = null;
    try {
        ObjectMapper objectMapper = new ObjectMapper();

        inputStream = new BufferedInputStream(new FileInputStream(fileName));
        config = (Config) objectMapper.readValue(inputStream, Config.class);

        return config;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    } finally {
        try {
            if (inputStream != null) {
                inputStream.close();
            }
        } catch (IOException ex) {
            Logger.getLogger(Storage.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}