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:com.bristle.javalib.io.FileUtil.java

/**************************************************************************
* Copy the binary contents of the specified InputStream to the specified 
* OutputStream, up to the specified number of bytes, returning the count
* of bytes written./*from   w w w.j  ava2s .c o  m*/
*@param  streamIn       InputStream to read from
*@param  streamOut      OutputStream to write to
*@param  lngMaxBytes    Max number of bytes to copy, or lngNO_MAX_BYTES 
*                       for unlimited
*@return                Count of bytes written
*@throws TooManyBytesException
*                       When streamIn contains more than intMaxBytes,
*                       resulting in a partially copied binary stream.
*@throws IOException    When an I/O error occurs reading or writing a
*                       stream.
**************************************************************************/
public static long copyBinaryStreamToStream(InputStream streamIn, OutputStream streamOut, long lngMaxBytes)
        throws TooManyBytesException, IOException {
    // Buffer the input and output for efficiency, to avoid lots of 
    // small I/O operations.
    // Note: Don't call the read() and write() methods that could do it 
    //       all in a single byte-array because we don't want to consume 
    //       that caller-specified amount of memory.  Better to do it
    //       one byte at a time, but with buffering for efficiency.
    BufferedInputStream buffIn = new BufferedInputStream(streamIn);
    BufferedOutputStream buffOut = new BufferedOutputStream(streamOut);

    long lngBytesWritten = 0;
    for (int intByte = buffIn.read(); intByte > -1; intByte = buffIn.read()) {
        if (lngMaxBytes != lngNO_MAX_BYTES && lngBytesWritten >= lngMaxBytes) {
            buffOut.flush();
            throw new TooManyBytesException("The input stream contains more than " + lngMaxBytes
                    + " bytes.  Only " + lngBytesWritten + " bytes were written to the " + "output stream",
                    lngBytesWritten);
        }
        buffOut.write(intByte);
        lngBytesWritten++;
    }
    buffOut.flush();
    return lngBytesWritten;
}

From source file:com.example.heya.couchdb.ConnectionHandler.java

/**
 * Get a File object from an URL/*from   www .  j a va  2 s .  co m*/
 * 
 * @param url
 * @param path
 * @return
 * @throws IOException 
 * @throws ClientProtocolException 
 * @throws SpikaException 
 * @throws JSONException 
 * @throws IllegalStateException 
 * @throws SpikaForbiddenException 
 */
public static void getFile(String url, File file, String userId, String token) throws ClientProtocolException,
        IOException, SpikaException, IllegalStateException, JSONException, SpikaForbiddenException {

    File mFile = file;

    //URL mUrl = new URL(url); // you can write here any link

    InputStream is = httpGetRequest(url, userId);
    BufferedInputStream bis = new BufferedInputStream(is);

    ByteArrayBuffer baf = new ByteArrayBuffer(20000);
    int current = 0;
    while ((current = bis.read()) != -1) {
        baf.append((byte) current);
    }

    /* Convert the Bytes read to a String. */
    FileOutputStream fos = new FileOutputStream(mFile);
    fos.write(baf.toByteArray());
    fos.flush();
    fos.close();
    is.close();

}

From source file:com.seajas.search.contender.scripting.XmlHtmlReader.java

private static String getXMLGuessEncoding(final BufferedInputStream is) throws IOException {
    String encoding = null;//w w  w  .  jav  a2 s .c  om
    int[] bytes = new int[4];
    is.mark(4);
    bytes[0] = is.read();
    bytes[1] = is.read();
    bytes[2] = is.read();
    bytes[3] = is.read();
    is.reset();

    if (bytes[0] == 0x00 && bytes[1] == 0x3C && bytes[2] == 0x00 && bytes[3] == 0x3F) {
        encoding = UTF_16BE;
    } else if (bytes[0] == 0x3C && bytes[1] == 0x00 && bytes[2] == 0x3F && bytes[3] == 0x00) {
        encoding = UTF_16LE;
    } else if (bytes[0] == 0x3C && bytes[1] == 0x3F && bytes[2] == 0x78 && bytes[3] == 0x6D) {
        encoding = UTF_8;
    }
    return encoding;
}

From source file:gate.util.Files.java

/** Get a resource from the GATE ClassLoader as a byte array.
  *///from ww w.j  a v  a 2s . com
public static byte[] getResourceAsByteArray(String resourceName)
        throws IOException, IndexOutOfBoundsException, ArrayStoreException {

    InputStream resourceInputStream = getResourceAsStream(resourceName);
    BufferedInputStream resourceStream = new BufferedInputStream(resourceInputStream);
    byte b;
    final int bufSize = 1024;
    byte[] buf = new byte[bufSize];
    int i = 0;

    // get the whole resource into buf (expanding the array as needed)
    while ((b = (byte) resourceStream.read()) != -1) {
        if (i == buf.length) {
            byte[] newBuf = new byte[buf.length * 2];
            System.arraycopy(buf, 0, newBuf, 0, i);
            buf = newBuf;
        }
        buf[i++] = b;
    }

    // close the resource stream
    resourceStream.close();

    // copy the contents of buf to an array of the correct size
    byte[] bytes = new byte[i];
    // copy from buf to bytes
    System.arraycopy(buf, 0, bytes, 0, i);
    return bytes;
}

From source file:gate.util.Files.java

/** Get a resource from the GATE resources directory as a byte array.
  * The resource name should be relative to <code>resourcePath</code> which
  * is equal with <TT>gate/resources</TT>; e.g.
  * for a resource stored as <TT>gate/resources/jape/Test11.jape</TT>,
  * this method should be passed the name <TT>jape/Test11.jape</TT>.
  *//*from ww  w  . j a va2 s.  c o m*/
public static byte[] getGateResourceAsByteArray(String resourceName)
        throws IOException, IndexOutOfBoundsException, ArrayStoreException {

    InputStream resourceInputStream = getGateResourceAsStream(resourceName);
    BufferedInputStream resourceStream = new BufferedInputStream(resourceInputStream);
    byte b;
    final int bufSize = 1024;
    byte[] buf = new byte[bufSize];
    int i = 0;

    // get the whole resource into buf (expanding the array as needed)
    while ((b = (byte) resourceStream.read()) != -1) {
        if (i == buf.length) {
            byte[] newBuf = new byte[buf.length * 2];
            System.arraycopy(buf, 0, newBuf, 0, i);
            buf = newBuf;
        }
        buf[i++] = b;
    }

    // close the resource stream
    resourceStream.close();

    // copy the contents of buf to an array of the correct size
    byte[] bytes = new byte[i];

    // copy from buf to bytes
    System.arraycopy(buf, 0, bytes, 0, i);
    return bytes;
}

From source file:com.seajas.search.contender.scripting.XmlHtmlReader.java

private static String getBOMEncoding(final BufferedInputStream is) throws IOException {
    String encoding = null;/* ww w .j a v a2 s  . c o  m*/
    int[] bytes = new int[3];
    is.mark(3);
    bytes[0] = is.read();
    bytes[1] = is.read();
    bytes[2] = is.read();

    if (bytes[0] == 0xFE && bytes[1] == 0xFF) {
        encoding = UTF_16BE;
        is.reset();
        is.read();
        is.read();
    } else if (bytes[0] == 0xFF && bytes[1] == 0xFE) {
        encoding = UTF_16LE;
        is.reset();
        is.read();
        is.read();
    } else if (bytes[0] == 0xEF && bytes[1] == 0xBB && bytes[2] == 0xBF) {
        encoding = UTF_8;
    } else {
        is.reset();
    }
    return encoding;
}

From source file:com.zimbra.cs.service.FeedManager.java

private static int getLeadingChar(BufferedInputStream is, StringBuilder charset) throws IOException {
    is.mark(128);/*  w w  w  .j a va 2 s .co  m*/
    // check for any BOMs that would override the specified charset
    int ch = is.read();
    switch (ch) {
    case 0xEF:
        if (is.read() == 0xBB && is.read() == 0xBF) {
            is.mark(128);
            ch = is.read();
            charset.setLength(0);
            charset.append("utf-8");
        }
        break;
    case 0xFE:
        if (is.read() == 0xFF && is.read() == 0x00) {
            ch = is.read();
            charset.setLength(0);
            charset.append("utf-16");
        }
        break;
    case 0xFF:
        if (is.read() == 0xFE) {
            ch = is.read();
            charset.setLength(0);
            charset.append("utf-16");
        }
        break;
    }
    // skip up to 120 bytes of leading whitespace
    for (int index = 0; index < 120 && (ch == '\0' || Character.isWhitespace(ch)); index++)
        ch = is.read();
    // reset to the original state and return the first non-whtespace character
    is.reset();
    return ch;
}

From source file:com.sim2dial.dialer.ChatFragment.java

public static Bitmap downloadImage(String stringUrl) {
    URL url;/*from ww w  .j a  va  2s. c o m*/
    Bitmap bm = null;
    try {
        url = new URL(stringUrl);
        URLConnection ucon = url.openConnection();
        InputStream is = ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        ByteArrayBuffer baf = new ByteArrayBuffer(50);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }

        byte[] rawImage = baf.toByteArray();
        bm = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
        bis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return bm;
}

From source file:de.betterform.xml.xforms.XFormsProcessorImpl.java

/**
 * Returns betterForm version string./* w ww  .j  av  a 2 s . com*/
 *
 * @return betterForm version string.
 */
public static String getAppInfo() {
    synchronized (XFormsProcessorImpl.class) {
        if (APP_INFO == null) {
            try {
                BufferedInputStream stream = new BufferedInputStream(
                        XFormsProcessorImpl.class.getResourceAsStream("/META-INF/version.info"));
                StringBuffer buffer = new StringBuffer();
                int c;

                while ((c = stream.read()) > -1) {
                    if (c != 10 && c != 13) {
                        buffer.append((char) c);
                    }
                }
                stream.close();
                APP_INFO = buffer.toString();
            } catch (IOException e) {
                APP_INFO = "betterFORM";
            }
        }
        return APP_INFO;
    }
}

From source file:fr.cnes.sitools.metacatalogue.resources.proxyservices.SecureOutputRepresentation.java

@Override
public void write(OutputStream os) throws IOException {

    Writer out = new OutputStreamWriter(os);

    InputStream is = response.getEntity().getContent();

    BufferedInputStream buff = new BufferedInputStream(is);
    int i;/*  ww  w  .  j  a va2 s .c o m*/
    do {
        i = buff.read();
        out.write((char) i);
    } while (i != -1);

}