Example usage for java.io InputStream read

List of usage examples for java.io InputStream read

Introduction

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

Prototype

public int read(byte b[], int off, int len) throws IOException 

Source Link

Document

Reads up to len bytes of data from the input stream into an array of bytes.

Usage

From source file:Main.java

public static void copyStream(InputStream is, OutputStream os) throws IOException {
    byte[] bytes = new byte[BUFFER_SIZE];
    for (;;) {//from w ww .j ava  2 s  . com
        int count = is.read(bytes, 0, BUFFER_SIZE);
        if (count == -1)
            break;
        os.write(bytes, 0, count);
    }
}

From source file:Main.java

/**
 * Reads all data from stream and close it silently
 *
 * @param is Input stream/*from   w w  w  .j  ava  2 s . co m*/
 */
public static void readAndCloseStream(InputStream is) {
    final byte[] bytes = new byte[DEFAULT_BUFFER_SIZE];
    try {
        while (is.read(bytes, 0, DEFAULT_BUFFER_SIZE) != -1)
            ;
    } catch (IOException ignored) {
    } finally {
        closeSilently(is);
    }
}

From source file:Main.java

public static void copyStream(InputStream is, OutputStream os) {
    try {//from ww w .ja v  a  2s . c om
        final byte[] bytes = new byte[BUFFER_SIZE];

        int count = is.read(bytes, 0, BUFFER_SIZE);
        while (count > -1) {
            os.write(bytes, 0, count);
            count = is.read(bytes, 0, BUFFER_SIZE);
        }

        os.flush();
    } catch (Exception ex) {
        Log.e("", "", ex);
    }
}

From source file:Main.java

public static boolean isValidXmlFile(final File file) {
    boolean result;

    try {/*from  w  w  w. j  av a 2s.  co  m*/
        final InputStream input = new FileInputStream(file.getPath());
        final byte[] header = new byte[32];
        input.read(header, 0, 32);

        String headerStr = new String(header, "ASCII");
        String XML_REGEX = "^\\s*<(\\?|!(--)?)?\\s*\\w+.*";

        result = headerStr.matches(XML_REGEX);

        try {
            input.close();
        } catch (Exception e) {
            // ignore this exception
        }
    } catch (Exception e) {
        result = false;
    }

    return result;

}

From source file:Main.java

public static boolean isBinaryPlist(final File source) {
    boolean result;

    try {/*from   ww  w.  j av  a 2  s .co m*/
        final InputStream input = new FileInputStream(source.getPath());
        final byte[] header = new byte[8];

        input.read(header, 0, 8);

        result = true;
        result &= (header[0] == 0x62); // b
        result &= (header[1] == 0x70); // p
        result &= (header[2] == 0x6C); // l
        result &= (header[3] == 0x69); // i
        result &= (header[4] == 0x73); // s
        result &= (header[5] == 0x74); // t
        // The two next bytes are the version number

        input.close();
    } catch (Exception e) {
        result = false;
    }

    return result;
}

From source file:Utils.java

private static void copyStreams(InputStream input, OutputStream output) throws IOException {
    int count;// w  w  w  .  j  av a  2  s .  c  o  m
    byte data[] = new byte[1024];
    while ((count = input.read(data, 0, 1024)) != -1) {
        output.write(data, 0, count);
    }
}

From source file:Main.java

public static void copyStream(InputStream is, OutputStream os) throws IOException {
    byte[] bytes = new byte[BUFFER_SIZE];
    while (true) {
        int count = is.read(bytes, 0, BUFFER_SIZE);
        if (count == -1) {
            break;
        }//from   w  w  w.  ja v  a 2 s.c o  m
        os.write(bytes, 0, count);
    }
}

From source file:Main.java

public static void copyStream(InputStream is, OutputStream os) throws IOException {
    byte buffer[] = new byte[1024];
    int len = -1;
    while ((len = is.read(buffer, 0, 1024)) != -1) {
        os.write(buffer, 0, len);/* w w w  .ja  v  a  2s  . c  o m*/
    }
}

From source file:org.thiesen.osm.pt.ConverterMain.java

private static InputStream getOSMInputStream(final String source) throws ClientProtocolException, IOException {
    final HttpClient client = new DefaultHttpClient();

    final HttpGet get = new HttpGet(source);

    final HttpResponse response = client.execute(get);

    final HttpEntity entity = response.getEntity();

    if (entity != null) {
        final InputStream content = entity.getContent();

        if (content != null) {

            content.read(new byte[2], 0, 2); // Skip first two bytes, because they are invalid

            final CBZip2InputStream bzipStream = new CBZip2InputStream(new BufferedInputStream(content));

            return bzipStream;
        }/*from  w ww.  ja va2 s.  c om*/
    }

    return null;
}

From source file:JarUtils.java

/**
 * Copies the input stream to the output stream
 *//*from  w ww .  ja  va 2 s .co  m*/
public static void copyStream(OutputStream outputStream, InputStream inputStream) throws IOException {
    byte[] bytes = new byte[4096];
    int read = inputStream.read(bytes, 0, 4096);
    while (read > 0) {
        outputStream.write(bytes, 0, read);
        read = inputStream.read(bytes, 0, 4096);
    }
}