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[]) throws IOException 

Source Link

Document

Reads some number of bytes from the input stream and stores them into the buffer array b.

Usage

From source file:net.sf.keystore_explorer.utilities.io.CopyUtil.java

/**
 * Copy data from one stream to another and do not close I/O.
 *
 * @param in/* ww w.ja v  a 2  s  . c  om*/
 *            Input stream
 * @param out
 *            Output stream
 * @throws IOException
 *             If an I/O problem occurred
 */
public static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[2048];
    int i;
    while ((i = in.read(buffer)) > 0) {
        out.write(buffer, 0, i);
    }
}

From source file:Main.java

/**
 * Copy the content of the input stream into the output stream, using a
 * temporary byte array buffer whose size is defined by
 * {@link #IO_BUFFER_SIZE}./*  w w w .j  a  v a 2  s .com*/
 *
 * @param in The input stream to copy from.
 * @param out The output stream to copy to.
 * @throws IOException If any error occurs during the copy.
 */
private static void copy(InputStream in, OutputStream out) throws IOException {
    byte[] b = new byte[IO_BUFFER_SIZE];
    int read;
    while ((read = in.read(b)) != -1) {
        out.write(b, 0, read);
    }
}

From source file:Main.java

public static void setupDatabase(Context context, String db_name) {
    ContextWrapper cw = new ContextWrapper(context);
    String db_path = cw.getDatabasePath(db_name).getPath();

    try {/*from   www. j a  v a  2s. c  om*/
        // Setup
        byte[] buffer = new byte[1024];
        int length;
        InputStream myInput = context.getAssets().open(db_name);
        OutputStream myOutput = new FileOutputStream(db_path);

        // Write all the things.
        while ((length = myInput.read(buffer)) > 0)
            myOutput.write(buffer, 0, length);

        // Cleanup
        myOutput.close();
        myOutput.flush();
        myInput.close();
    } catch (IOException e) {
        // You done goofed.
        e.printStackTrace();
    }
}

From source file:Main.java

public static void copyFile(String oldPath, String newPath) {
    try {//from  www  . j a va  2s. c o  m
        int byteRead;
        File oldFile = new File(oldPath);
        if (oldFile.exists()) {
            InputStream inStream = new FileInputStream(oldPath);
            FileOutputStream fs = new FileOutputStream(newPath);
            byte[] buffer = new byte[1444];
            while ((byteRead = inStream.read(buffer)) != -1) {
                fs.write(buffer, 0, byteRead);
            }
            inStream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:DBMS.UpdateFileUpload.java

public static boolean processFile(String path, FileItemStream item, int id) {
    try {//from   w w  w  .j  a va2s  .co m
        String check = item.getName();
        if (check.endsWith(".jpg") || check.endsWith(".JPG")) {
            String imstring = "images/" + Integer.toString(id);
            File f = new File(path + File.separator + imstring);
            if (!f.exists())
                f.mkdir();
            File savedFile = new File(f.getAbsolutePath() + File.separator + item.getName());
            FileOutputStream fos = new FileOutputStream(savedFile);
            InputStream is = item.openStream();
            int x = 0;
            byte[] b = new byte[1024];
            while ((x = is.read(b)) != -1) {
                fos.write(b, 0, x);
            }
            fos.flush();
            fos.close();
            String dbimage = imstring + "/a.jpg";
            //dc.enterImage(dbimage);
            //im =dbimage;
            //System.out.println("Resizing!");
            //Resize rz = new Resize();
            //rz.resize(dbimage);
            BufferedImage originalImage = ImageIO.read(savedFile);
            int type = originalImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : originalImage.getType();
            BufferedImage resizeImageJpg = resizeImage(originalImage, type);
            ImageIO.write(resizeImageJpg, "jpg", savedFile);
            File rFile = new File(f.getAbsolutePath() + "/a.jpg");
            savedFile.renameTo(rFile);
            ProfileEditDB dc = new ProfileEditDB();
            dc.enterImage(id, dbimage);
            System.out.println("Link Entered to Database!");
            return true;
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:Main.java

private static void streamToFile(InputStream is, File targetFile) throws IOException {
    FileOutputStream fos = new FileOutputStream(targetFile);
    byte[] bytes = new byte[1024];
    while (true) {
        int n = is.read(bytes);
        if (n < 0) {
            break;
        }/*  w  w  w.  jav a  2  s  .  c o m*/
        fos.write(bytes, 0, n);
    }
    is.close();
    fos.close();
}

From source file:Main.java

private static void copyResourceFile(Context context, int rid, String targetFile) throws IOException {
    InputStream fin = context.getResources().openRawResource(rid);
    FileOutputStream fos = new FileOutputStream(targetFile);

    int length;//from  w w  w  .  ja v  a  2  s  . c o m
    byte[] buffer = new byte[1024 * 32];
    while ((length = fin.read(buffer)) != -1) {
        fos.write(buffer, 0, length);
    }
    fin.close();
    fos.close();
}

From source file:Main.java

public static byte[] streamToByteArray(InputStream stream) throws IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    try {//w  w w .  j  a v  a  2 s. c  o m
        int count;
        while ((count = stream.read(buffer)) != -1) {
            bytes.write(buffer, 0, count);
        }
        return bytes.toByteArray();
    } finally {
        stream.close();
        bytes.close();
    }
}

From source file:Main.java

private static String readTextFile(InputStream inputStream) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte buf[] = new byte[1024];
    int len;/*  w w  w  .  j  av a 2s .  c  om*/
    try {
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
    } catch (IOException e) {
        Log.i("IO error", e.getMessage());
        return "Sorry, help file not found.";
    }
    return outputStream.toString();
}

From source file:Main.java

public static void copyfile(File src, File dec) {
    try {// ww  w.  j  a  v  a 2 s. c  o m
        if (src == null || dec == null) {
            return;
        }

        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dec);

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    } catch (FileNotFoundException ex) {
        ex.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}