Example usage for java.io BufferedInputStream read

List of usage examples for java.io BufferedInputStream read

Introduction

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

Prototype

public synchronized int read() throws IOException 

Source Link

Document

See the general contract of the read method of InputStream.

Usage

From source file:LabelJarSample.java

public static Image getImage(Class relativeClass, String filename) {
    Image returnValue = null;/*  w  ww .  j  av a2  s.  c  o m*/
    InputStream is = relativeClass.getResourceAsStream(filename);
    if (is != null) {
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            int ch;
            while ((ch = bis.read()) != -1) {
                baos.write(ch);
            }
            returnValue = Toolkit.getDefaultToolkit().createImage(baos.toByteArray());
        } catch (IOException exception) {
            System.err.println("Error loading: " + filename);
        }
    }
    return returnValue;
}

From source file:com.dicksoft.ocr.util.HttpUtil.java

/**
 * Downloads the text from the specified URL.
 * //from  ww  w .j a va 2s.c  o  m
 * @param url
 *            the URL of the text to read
 * @return the text
 * @throws IOException
 *             if the URL is malformed, or problem reading the stream
 */
public static String fetchText(String url) throws IOException {
    if (LOG.isDebugEnabled())
        LOG.debug("Http fetch: " + url);
    StringBuffer result = new StringBuffer();
    URL urlReal = null;
    urlReal = new URL(url);
    BufferedInputStream in = null;
    in = new BufferedInputStream(urlReal.openStream());

    int data = 0;
    while (true) {
        data = in.read();
        if (data == -1)
            break;
        else
            result.append((char) data);
    }
    return result.toString();
}

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

public static byte[] getImage(URL url) throws Exception {
    URLConnection connection = url.openConnection();

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

    ByteArrayBuffer arraybuffer = new ByteArrayBuffer(50);
    int current = 0;
    while ((current = inputbuffer.read()) != -1) {
        arraybuffer.append((byte) current);
    }/*ww  w.ja v a2  s .  com*/

    byte[] image = arraybuffer.toByteArray();

    return image;
}

From source file:it.nicola_amatucci.util.JsonAndroidLocalIO.java

public static <T> T loadData(Context context, String filename, Class<T> obj) {
    StringBuilder strContent = new StringBuilder("");
    try {/*from   w w w. j av  a 2 s  .  c  o  m*/
        BufferedInputStream in = new BufferedInputStream(context.openFileInput(filename));

        int ch;
        while ((ch = in.read()) != -1)
            strContent.append((char) ch);

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

    if (strContent.length() > 0) {
        try {
            Log.i("TAG", strContent.toString());
            JSONObject json = new JSONObject(strContent.toString());
            return Json.object_from_json(json, obj);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return null;
}

From source file:Main.java

public static Image getImage(Class relativeClass, String filename) {
    Image returnValue = null;/*  w ww.ja  v a2 s  .c o  m*/
    InputStream is = relativeClass.getResourceAsStream(filename);
    if (is != null) {
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            int ch;
            while ((ch = bis.read()) != -1) {
                baos.write(ch);
            }
            Toolkit t = Toolkit.getDefaultToolkit();

            returnValue = t.createImage(baos.toByteArray());
        } catch (IOException exception) {
            System.err.println("Error loading: " + filename);
        }
    }
    return returnValue;
}

From source file:Main.java

public static byte[] downloadImage(String imageUrl) {
    if (imageUrl.endsWith(".jpg") || imageUrl.endsWith(".bmp") || imageUrl.endsWith(".png")
            || imageUrl.endsWith(".gif")) {
        try {/* w ww. j  a v a 2s.c o  m*/
            URL url = new URL(imageUrl);
            URLConnection urlConn = url.openConnection();
            InputStream is = urlConn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayBuffer baf = new ByteArrayBuffer(50);
            int current = 0;

            while ((current = bis.read()) != -1) {
                baf.append((byte) current);
            }

            return baf.toByteArray();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    return null;
}

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

/**
 * @param args/*www  .j  a v a2s.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 boolean bufferedCopyStream(InputStream inStream, OutputStream outStream, boolean closeStream)
        throws Exception {
    BufferedInputStream bis = new BufferedInputStream(inStream);
    BufferedOutputStream bos = new BufferedOutputStream(outStream);
    while (true) {
        int data = bis.read();
        if (data == -1) {
            break;
        }/*from  w w w  .j a  v  a 2 s  .  c  o  m*/
        bos.write(data);
    }
    bos.flush();
    if (closeStream) {
        bos.close();
    }
    return true;
}

From source file:MainClass.java

public static void desEncrypt(String f1, String f2) throws Exception {
    SecretKey key = null;//  ww  w. jav  a2s.c  o m
    ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("DESKey.ser"));
    key = (SecretKey) keyFile.readObject();
    keyFile.close();

    KeyGenerator keygen = KeyGenerator.getInstance("DES");
    key = keygen.generateKey();
    ObjectOutputStream keyFileout = new ObjectOutputStream(new FileOutputStream("DESKey.ser"));
    keyFileout.writeObject(key);
    keyFileout.close();
    Cipher cipher = null;
    cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(f1));

    CipherOutputStream out = new CipherOutputStream(new BufferedOutputStream(new FileOutputStream(f2)), cipher);
    int i;
    do {
        i = in.read();
        if (i != -1)
            out.write(i);
    } while (i != -1);
    in.close();
    out.close();
}

From source file:com.mondospider.android.lib.LibHTTP.java

public static String get(String url) {
    String ReturnHTML = "";
    //      Log.d("LibHTTP->get->url",url);
    try {//from w  ww .j  a va 2s.c om
        URLConnection urlConn = new URL(url).openConnection();
        InputStream is = urlConn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is, 16000);
        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }
        ReturnHTML = new String(baf.toByteArray());
    } catch (Exception e) {
        ReturnHTML = e.getMessage();
    }
    //        Log.d("LibHTTP->get->ReturnHTML", ReturnHTML);
    return ReturnHTML;
}