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(byte b[], int off, int len) throws IOException 

Source Link

Document

Reads bytes from this byte-input stream into the specified byte array, starting at the given offset.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    InputStream is = Main.class.getResourceAsStream("image.gif");
    BufferedInputStream bis = new BufferedInputStream(is);
    byte[] byBuf = new byte[10000];

    int byteRead = bis.read(byBuf, 0, 10000);
    Image img = Toolkit.getDefaultToolkit().createImage(byBuf);
}

From source file:Main.java

  public static void main(String[] args) throws IOException {
  ServerSocket servsock = new ServerSocket(123456);
  File myFile = new File("s.pdf");
  while (true) {//from w  ww  . ja  va 2s  . c  om
    Socket sock = servsock.accept();
    byte[] mybytearray = new byte[(int) myFile.length()];
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(myFile));
    bis.read(mybytearray, 0, mybytearray.length);
    OutputStream os = sock.getOutputStream();
    os.write(mybytearray, 0, mybytearray.length);
    os.flush();
    sock.close();
  }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream inStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(inStream);

    // read number of bytes available
    int numByte = bis.available();

    // byte array declared
    byte[] buf = new byte[numByte];

    // read byte into buf , starts at offset 2, 3 bytes to read
    bis.read(buf, 2, 3);

    // for each byte in buf
    for (byte b : buf) {
        System.out.println((char) b + ": " + b);
    }//  w w  w  .ja  va 2 s .c o m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String zipname = "data.zip";
    ZipFile zipFile = new ZipFile(zipname);
    Enumeration enumeration = zipFile.entries();
    while (enumeration.hasMoreElements()) {
        ZipEntry zipEntry = (ZipEntry) enumeration.nextElement();
        System.out.println("Unzipping: " + zipEntry.getName());
        BufferedInputStream bis = new BufferedInputStream(zipFile.getInputStream(zipEntry));
        int size;
        byte[] buffer = new byte[2048];
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(zipEntry.getName()),
                buffer.length);/* w w  w.j  a  va 2  s . c  o m*/
        while ((size = bis.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
        bis.close();
    }
}

From source file:com.manning.blogapps.chapter10.examples.AuthPostJava.java

public static void main(String[] args) throws Exception {
    if (args.length < 4) {
        System.out.println("USAGE: authpost <username> <password> <filepath> <url>");
        System.exit(-1);//from   w w  w .j  av  a  2  s  . c  om
    }
    String credentials = args[0] + ":" + args[1];
    String filepath = args[2];

    URL url = new URL(args[3]);
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);

    conn.setRequestProperty("Authorization",
            "Basic " + new String(Base64.encodeBase64(credentials.getBytes())));

    File upload = new File(filepath);
    conn.setRequestProperty("name", upload.getName());

    String contentType = "application/atom+xml; charset=utf8";
    if (filepath.endsWith(".gif"))
        contentType = "image/gif";
    else if (filepath.endsWith(".jpg"))
        contentType = "image/jpg";
    conn.setRequestProperty("Content-type", contentType);

    BufferedInputStream filein = new BufferedInputStream(new FileInputStream(upload));
    BufferedOutputStream out = new BufferedOutputStream(conn.getOutputStream());
    byte buffer[] = new byte[8192];
    for (int count = 0; count != -1;) {
        count = filein.read(buffer, 0, 8192);
        if (count != -1)
            out.write(buffer, 0, count);
    }
    filein.close();
    out.close();

    String s = null;
    BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    while ((s = in.readLine()) != null) {
        System.out.println(s);
    }
}

From source file:Main.java

public static byte[] readZipEntry(File zfile, ZipEntry entry) throws ZipException, IOException {
    Log.d("file3: ", zfile.toString());
    Log.d("zipEntry3: ", entry.toString());
    ZipFile zipFile = new ZipFile(zfile);
    if (entry != null && !entry.isDirectory()) {
        byte[] barr = new byte[(int) entry.getSize()];
        int read = 0;
        int len = 0;
        InputStream is = zipFile.getInputStream(entry);
        BufferedInputStream bis = new BufferedInputStream(is);
        int length = barr.length;
        while ((len = bis.read(barr, read, length - read)) != -1) {
            read += len;//  ww  w.j  a va  2s .  c  o m
        }
        bis.close();
        is.close();
        zipFile.close();
        return barr;
    } else {
        zipFile.close();
        return new byte[0];
    }
}

From source file:com.phonegap.plugins.xapkreader.XAPKReader.java

private static byte[] readFile(Context ctx, String filename) throws IOException {
    // Get APKExpensionFile
    ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(ctx, XAPKReader.mainVersion,
            XAPKReader.patchVersion);/*from w ww .  j  a v  a 2s .  c o m*/

    if (null == expansionFile) {
        Log.e("XAPKReader", "APKExpansionFile not found.");
        return null;
    }

    // Find file in ExpansionFile
    String fileName = Helpers.getExpansionAPKFileName(ctx, true, XAPKReader.patchVersion);
    fileName = fileName.substring(0, fileName.lastIndexOf("."));
    AssetFileDescriptor file = expansionFile.getAssetFileDescriptor(fileName + "/" + filename);

    if (null == file) {
        Log.e("XAPKReader", "File not found (" + filename + ").");
        return null;
    }

    // Read file
    int size = (int) file.getLength();
    byte[] data = new byte[size];
    BufferedInputStream buf = new BufferedInputStream(file.createInputStream());
    buf.read(data, 0, data.length);
    buf.close();

    return data;
}

From source file:Main.java

public static String readInputStream(InputStream i) throws IOException {
    BufferedInputStream s = new BufferedInputStream(i);
    byte[] buf = new byte[512];
    int len = -1;
    StringBuilder sb = new StringBuilder();

    while ((len = s.read(buf, 0, buf.length)) != -1) {
        sb.append(new String(buf, 0, len));
    }/*from w  w w  . j  a  v a2 s  .c o m*/

    return sb.toString();
}

From source file:librarymanagementsystem.Base64Converter.java

/**
 * This method loads a file from file system and returns the byte array of
 * the content.//from  www  .  ja v  a 2 s  .  c om
 */
public static byte[] loadFileAsBytesArray(String fileName) throws Exception {

    File file = new File(fileName);
    int length = (int) file.length();
    BufferedInputStream reader = new BufferedInputStream(new FileInputStream(file));
    byte[] bytes = new byte[length];
    reader.read(bytes, 0, length);
    reader.close();
    return bytes;

}

From source file:Main.java

public static byte[] readFileToMemory(File file) throws IOException {
    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    byte[] data = new byte[(int) file.length()];
    int start = 0;
    int read = 0;
    while ((read = bis.read(data, start, data.length - start)) > 0) {
        start += read;/*from w  ww  . j  a v a  2  s  .co  m*/
    }
    bis.close();
    fis.close();
    return data;
}