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

Source Link

Document

Reads up to b.length bytes of data from this input stream into an array of bytes.

Usage

From source file:com.playhaven.android.diagnostic.test.PHTestCase.java

protected byte[] readFile(File file) throws IOException {
    PlayHaven.d("reading file: " + file.getAbsolutePath());
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
    ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
    byte[] buf = new byte[1024];
    int len;//w  w w .j a  v a 2 s . c om
    while ((len = in.read(buf)) != -1)
        out.write(buf, 0, len);

    out.flush();
    buf = out.toByteArray();
    in.close();
    out.close();
    return buf;
}

From source file:IDlook.java

private void buildGUI() {
    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    accountNumberList = new JList();
    loadAccounts();//from w  w  w .java 2 s  . c om
    accountNumberList.setVisibleRowCount(2);
    JScrollPane accountNumberListScrollPane = new JScrollPane(accountNumberList);

    //Do Get Account Button
    getAccountButton = new JButton("Get Account");
    getAccountButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                rs.beforeFirst();
                while (rs.next()) {
                    if (rs.getString("acc_id").equals(accountNumberList.getSelectedValue()))
                        break;
                }
                if (!rs.isAfterLast()) {
                    accountIDText.setText(rs.getString("acc_id"));
                    thumbIDText.setText(rs.getString("thumb_id"));

                    icon = new ImageIcon(rs.getBytes("pic"));
                    createThumbnail();
                    photographLabel.setIcon(iconThumbnail);
                }
            } catch (SQLException selectException) {
                displaySQLErrors(selectException);
            }
        }
    });

    //Do Update Account Button
    updateAccountButton = new JButton("Update Account");
    updateAccountButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                byte[] bytes = new byte[50000];
                FileInputStream fs = new FileInputStream(nailFileText.getText());
                BufferedInputStream bis = new BufferedInputStream(fs);
                bis.read(bytes);

                rs.updateBytes("thumbnail.pic", bytes);
                rs.updateRow();
                bis.close();

                accountNumberList.removeAll();
                loadAccounts();
            } catch (SQLException insertException) {
                displaySQLErrors(insertException);
            } catch (Exception generalE) {
                generalE.printStackTrace();
            }
        }
    });

    //Do insert Account Button
    insertAccountButton = new JButton("Insert Account");
    insertAccountButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                byte[] bytes = new byte[50000];
                FileInputStream fs = new FileInputStream(nailFileText.getText());
                BufferedInputStream bis = new BufferedInputStream(fs);
                bis.read(bytes);

                rs.moveToInsertRow();
                rs.updateInt("thumb_id", Integer.parseInt(thumbIDText.getText()));
                rs.updateInt("acc_id", Integer.parseInt(accountIDText.getText()));
                rs.updateBytes("pic", bytes);
                rs.updateObject("sysobject", null);
                rs.updateTimestamp("ts", new Timestamp(0));
                rs.updateTimestamp("act_ts", new Timestamp(new java.util.Date().getTime()));
                rs.insertRow();
                bis.close();

                accountNumberList.removeAll();
                loadAccounts();
            } catch (SQLException insertException) {
                displaySQLErrors(insertException);
            } catch (Exception generalE) {
                generalE.printStackTrace();
            }
        }
    });

    photographLabel = new JLabel();
    photographLabel.setHorizontalAlignment(JLabel.CENTER);
    photographLabel.setVerticalAlignment(JLabel.CENTER);
    photographLabel.setVerticalTextPosition(JLabel.CENTER);
    photographLabel.setHorizontalTextPosition(JLabel.CENTER);

    JPanel first = new JPanel(new GridLayout(4, 1));
    first.add(accountNumberListScrollPane);
    first.add(getAccountButton);
    first.add(updateAccountButton);
    first.add(insertAccountButton);

    accountIDText = new JTextField(15);
    thumbIDText = new JTextField(15);
    errorText = new JTextArea(5, 15);
    errorText.setEditable(false);

    JPanel second = new JPanel();
    second.setLayout(new GridLayout(2, 1));
    second.add(thumbIDText);
    second.add(accountIDText);

    JPanel third = new JPanel();
    third.add(new JScrollPane(errorText));

    nailFileText = new JTextField(25);

    c.add(first);
    c.add(second);
    c.add(third);
    c.add(nailFileText);
    c.add(photographLabel);

    setSize(500, 500);
    show();
}

From source file:IDlookBlob.java

private void buildGUI() {
    Container c = getContentPane();
    c.setLayout(new FlowLayout());

    accountNumberList = new JList();
    loadAccounts();/*from  w w w .  ja  v  a 2  s. co m*/
    accountNumberList.setVisibleRowCount(2);
    JScrollPane accountNumberListScrollPane = new JScrollPane(accountNumberList);

    //Do Get Account Button
    getAccountButton = new JButton("Get Account");
    getAccountButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                rs.beforeFirst();
                while (rs.next()) {
                    if (rs.getString("acc_id").equals(accountNumberList.getSelectedValue()))
                        break;
                }
                if (!rs.isAfterLast()) {
                    accountIDText.setText(rs.getString("acc_id"));
                    thumbIDText.setText(rs.getString("thumb_id"));
                    Blob b = rs.getBlob("pic");

                    icon = new ImageIcon(b.getBytes(1L, (int) b.length()));
                    createThumbnail();
                    photographLabel.setIcon(iconThumbnail);
                }
            } catch (SQLException selectException) {
                displaySQLErrors(selectException);
            }
        }
    });

    //Do Update Account Button
    updateAccountButton = new JButton("Update Account");
    updateAccountButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                byte[] bytes = new byte[50000];
                FileInputStream fs = new FileInputStream(nailFileText.getText());
                BufferedInputStream bis = new BufferedInputStream(fs);
                bis.read(bytes);

                rs.updateBytes("thumbnail.pic", bytes);
                rs.updateRow();
                bis.close();

                accountNumberList.removeAll();
                loadAccounts();
            } catch (SQLException insertException) {
                displaySQLErrors(insertException);
            } catch (Exception generalE) {
                generalE.printStackTrace();
            }
        }
    });

    //Do insert Account Button
    insertAccountButton = new JButton("Insert Account");
    insertAccountButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            try {
                byte[] bytes = new byte[50000];
                FileInputStream fs = new FileInputStream(nailFileText.getText());
                BufferedInputStream bis = new BufferedInputStream(fs);
                bis.read(bytes);

                rs.moveToInsertRow();
                rs.updateInt("thumb_id", Integer.parseInt(thumbIDText.getText()));
                rs.updateInt("acc_id", Integer.parseInt(accountIDText.getText()));
                rs.updateBytes("pic", bytes);
                rs.updateObject("sysobject", null);
                rs.updateTimestamp("ts", new Timestamp(0));
                rs.updateTimestamp("act_ts", new Timestamp(new java.util.Date().getTime()));
                rs.insertRow();
                bis.close();

                accountNumberList.removeAll();
                loadAccounts();
            } catch (SQLException insertException) {
                displaySQLErrors(insertException);
            } catch (Exception generalE) {
                generalE.printStackTrace();
            }
        }
    });

    photographLabel = new JLabel();
    photographLabel.setHorizontalAlignment(JLabel.CENTER);
    photographLabel.setVerticalAlignment(JLabel.CENTER);
    photographLabel.setVerticalTextPosition(JLabel.CENTER);
    photographLabel.setHorizontalTextPosition(JLabel.CENTER);

    JPanel first = new JPanel(new GridLayout(4, 1));
    first.add(accountNumberListScrollPane);
    first.add(getAccountButton);
    first.add(updateAccountButton);
    first.add(insertAccountButton);

    accountIDText = new JTextField(15);
    thumbIDText = new JTextField(15);
    errorText = new JTextArea(5, 15);
    errorText.setEditable(false);

    JPanel second = new JPanel();
    second.setLayout(new GridLayout(2, 1));
    second.add(thumbIDText);
    second.add(accountIDText);

    JPanel third = new JPanel();
    third.add(new JScrollPane(errorText));

    nailFileText = new JTextField(25);

    c.add(first);
    c.add(second);
    c.add(third);
    c.add(nailFileText);
    c.add(photographLabel);

    setSize(500, 500);
    show();
}

From source file:de.betterform.xml.xforms.ui.UploadTest.java

/**
 * Tests ui element state./*w w  w. j a va 2s  . co m*/
 *
 * @throws Exception if any error occurred during the test.
 */
public void testUploadBase64() throws Exception {
    Upload upload = (Upload) this.xformsProcesssorImpl.getContainer().lookup("upload-base64");
    upload.getTarget().addEventListener(XFormsEventNames.VALUE_CHANGED, this.valueChangedListener, false);

    String filename = "UploadTest.xhtml";
    String mediatype = "application/xhtml+xml";

    BufferedInputStream bis = new BufferedInputStream(getClass().getResourceAsStream(filename));
    byte[] data = new byte[bis.available()];
    bis.read(data);
    upload.setValue(data, filename, mediatype);
    upload.getTarget().removeEventListener(XFormsEventNames.VALUE_CHANGED, this.valueChangedListener, false);

    assertEquals(new String(data), new String(Base64.decodeBase64(((String) upload.getValue()).getBytes())));
    assertEquals(filename, upload.getFilename().getValue());
    assertEquals(mediatype, upload.getMediatype().getValue());

    ModelItem modelItem = upload.getModel().getInstance(upload.getInstanceId())
            .getModelItem(upload.getInstanceNode());
    assertEquals(filename, modelItem.getFilename());
    assertEquals(mediatype, modelItem.getMediatype());

    assertEquals("upload-base64", this.valueChangedListener.getId());
}

From source file:de.betterform.xml.xforms.ui.UploadTest.java

/**
 * Tests ui element state.//from   w  w w .j  a v a2 s.c o  m
 *
 * @throws Exception if any error occurred during the test.
 */
public void testUploadHex() throws Exception {
    Upload upload = (Upload) this.xformsProcesssorImpl.getContainer().lookup("upload-hex");
    upload.getTarget().addEventListener(XFormsEventNames.VALUE_CHANGED, this.valueChangedListener, false);

    String filename = "UploadTest.xhtml";
    String mediatype = "application/xhtml+xml";

    BufferedInputStream bis = new BufferedInputStream(getClass().getResourceAsStream(filename));
    byte[] data = new byte[bis.available()];
    bis.read(data);
    upload.setValue(data, filename, mediatype);
    upload.getTarget().removeEventListener(XFormsEventNames.VALUE_CHANGED, this.valueChangedListener, false);

    assertEquals(new String(data), new String(Hex.decodeHex(((String) upload.getValue()).toCharArray())));
    assertEquals(filename, upload.getFilename().getValue());
    assertEquals(mediatype, upload.getMediatype().getValue());

    ModelItem modelItem = upload.getModel().getInstance(upload.getInstanceId())
            .getModelItem(upload.getInstanceNode());
    assertEquals(filename, modelItem.getFilename());
    assertEquals(mediatype, modelItem.getMediatype());

    assertEquals("upload-hex", this.valueChangedListener.getId());
}

From source file:com.ibm.dbwkl.helper.CryptionModule.java

/**
 * @param path//from   ww  w.  j av  a2 s .c  o  m
 * @return byte Array with the encrypted password
 * @throws IOException
 */
public String readPassfile(String path) throws IOException {
    String encryptedPassword;
    byte[] passbytes;

    if (path != null) {
        if (path.length() != 0) {
            this.passpath = path;
        }
    }

    File passfile = new File(this.passpath);
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(passfile));
    passbytes = new byte[(int) passfile.length()];
    bis.read(passbytes);
    bis.close();

    encryptedPassword = new String(passbytes, "UTF8");

    return encryptedPassword;
}

From source file:eu.scape_project.arc2warc.identification.PayloadContent.java

private byte[] inputStreamToByteArray() throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    BufferedInputStream buffis = new BufferedInputStream(inputStream);
    BufferedOutputStream buffos = new BufferedOutputStream(baos);
    byte[] tempBuffer = new byte[BUFFER_SIZE];
    int bytesRead;
    boolean firstByteArray = true;
    while ((bytesRead = buffis.read(tempBuffer)) != -1) {
        buffos.write(tempBuffer, 0, bytesRead);
        if (applyIdentification && firstByteArray && tempBuffer != null && bytesRead > 0) {
            identified = identifyPayloadType(tempBuffer);
        }//from  ww w.ja  v  a  2s . c  o m
        firstByteArray = false;
    }
    buffis.close();
    buffos.flush();
    buffos.close();

    return baos.toByteArray();
}

From source file:cc.aileron.commons.resource.ResourceImpl.java

/**
 * @return byte[]//from   w  ww. j  a v  a  2 s .  co m
 * @throws IOException
 */
@Override
public byte[] toBytes() throws IOException {
    final BufferedInputStream in = new BufferedInputStream(content.getInputStream());
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final DataOutputStream os = new DataOutputStream(baos);

    byte[] buffer = new byte[8192];
    while (in.read(buffer) != -1) {
        os.write(buffer);
        buffer = new byte[8192];
    }

    return baos.toByteArray();
}

From source file:edu.stanford.epadd.launcher.Main.java

public static void copy_stream_to_file(InputStream is, String filename) throws IOException {
    int bufsize = 64 * 1024;
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {/*from  w w  w  .ja v  a 2  s.c o m*/
        File f = new File(filename);
        if (f.exists()) {
            // out.println ("File " + filename + " exists");
            boolean b = f.delete(); // best effort to delete file if it exists. this is because windows often complains about perms 
            if (!b)
                out.println("Warning: failed to delete " + filename);
        }
        bis = new BufferedInputStream(is, bufsize);
        bos = new BufferedOutputStream(new FileOutputStream(filename), bufsize);
        byte buf[] = new byte[bufsize];
        while (true) {
            int n = bis.read(buf);
            if (n <= 0)
                break;
            bos.write(buf, 0, n);
        }
    } catch (IOException ioe) {
        out.println("ERROR trying to copy data to file: " + filename + ", forging ahead nevertheless");
    } finally {
        if (bis != null)
            bis.close();
        if (bos != null)
            bos.close();
    }
}

From source file:raymond.mockftpserver.S3CachedFtpServer.java

private void readKeyFile(File _accessFile) throws IOException, EncryptionException {
    logger.info("@@ reading cypher from:" + _accessFile);
    BufferedInputStream in = new BufferedInputStream(new FileInputStream(_accessFile));
    int len = in.available();
    byte[] buffer = new byte[len];
    in.read(buffer);
    in.close();//from   w  w w  . j  a  va  2  s  .co  m
    logger.info("@@ len read is:" + len);

    byte[] cleartext;

    Encryptor crypt = new ThreeDESEncryptor(chave + keyBase);
    cleartext = crypt.decrypt(buffer);

    int nameLen = cleartext[0];
    int accessKeyLen = cleartext[1];
    int secretKeyLen = cleartext.length - 2 - nameLen - accessKeyLen;
    if (secretKeyLen % 2 != 0) {
        throw new RuntimeException("internal error: secret key has odd bytecount");
    }

    byte[] nameRaw = new byte[nameLen];
    for (int i = 0; i < nameLen; i++) {
        nameRaw[i] = cleartext[i + 2];
    }
    String nameCheck = new String(nameRaw, charset);
    if (!chave.equals(nameCheck)) {
        throw new RuntimeException("internal error: secret key name mismatch");
    }

    // Get the Access Key
    int offset = 2 + nameLen;
    byte[] accessKeyRaw = new byte[accessKeyLen];
    for (int i = 0; i < accessKeyLen; i++) {
        accessKeyRaw[i] = cleartext[i + offset];
    }
    apiKey = new String(accessKeyRaw, charset);

    // Get the Secret Key
    char[] secretKey = new char[secretKeyLen / 2];
    offset += accessKeyLen;
    for (int i = 0; i < secretKeyLen / 2; i++) {
        int spot = i + i + offset;
        char ch = (char) ((cleartext[spot] << 8) + cleartext[spot + 1]);
        secretKey[i] = ch;
    }
    apiKeySecret = new String(secretKey);
}