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:cn.isif.util_plus.http.ResponseStream.java

public void readFile(String savePath) throws IOException {
    if (_directResult != null)
        return;//www . j  a v a 2s.  c o  m
    if (baseStream == null)
        return;
    BufferedOutputStream out = null;
    try {
        out = new BufferedOutputStream(new FileOutputStream(savePath));
        BufferedInputStream ins = new BufferedInputStream(baseStream);
        byte[] buffer = new byte[4096];
        int len = 0;
        while ((len = ins.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        out.flush();
    } finally {
        IOUtils.closeQuietly(out);
        IOUtils.closeQuietly(baseStream);
    }
}

From source file:com.norconex.committer.gsa.XmlOutput.java

private void writeContent(IAddOperation op) throws XMLStreamException, IOException {
    writer.writeStartElement("content");
    BufferedInputStream bufferedInput = null;
    byte[] buffer = new byte[1024 * 1024]; // 1MB
    try {/*from   w  ww  .j  a v  a  2 s .  co m*/
        bufferedInput = new BufferedInputStream(op.getContentStream());
        int bytesRead = 0;
        while ((bytesRead = bufferedInput.read(buffer)) != -1) {
            writer.writeCharacters(new String(buffer, 0, bytesRead));
        }
    } finally {
        IOUtils.closeQuietly(bufferedInput);
    }
    writer.writeEndElement();
}

From source file:com.panet.imeta.core.xml.XMLHandler.java

/**
 * Convert a XML encoded binary string back to binary format
 * /*from   w ww.j av a2s  .com*/
 * @param string
 *            the (Byte64/GZip) encoded string
 * @return the decoded binary (byte[]) object
 * @throws IOException
 *             In case there is a decoding error
 */
public static byte[] stringToBinary(String string) throws IOException {
    byte[] bytes;
    if (string == null) {
        bytes = new byte[] {};
    } else {
        bytes = Base64.decodeBase64(string.getBytes());
    }
    if (bytes.length > 0) {
        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
        GZIPInputStream gzip = new GZIPInputStream(bais);
        BufferedInputStream bi = new BufferedInputStream(gzip);
        byte[] result = new byte[] {};

        byte[] extra = new byte[1000000];
        int nrExtra = bi.read(extra);
        while (nrExtra >= 0) {
            // add it to bytes...
            //
            int newSize = result.length + nrExtra;
            byte[] tmp = new byte[newSize];
            for (int i = 0; i < result.length; i++)
                tmp[i] = result[i];
            for (int i = 0; i < nrExtra; i++)
                tmp[result.length + i] = extra[i];

            // change the result
            result = tmp;
            nrExtra = bi.read(extra);
        }
        bytes = result;
        gzip.close();
    }

    return bytes;
}

From source file:de.betterform.agent.web.servlet.XSLTServlet.java

private StringBuffer generateError(String error) throws IOException, XFormsConfigException {

    String path = WebFactory.getRealPath("forms/incubator/editor", getServletContext());
    File f = new File(path, "callerror.html");
    FileInputStream fs = new FileInputStream(f);
    BufferedInputStream bis = new BufferedInputStream(fs);
    int numBytes = bis.available();
    byte b[] = new byte[numBytes];
    // read the template into a StringBuffer (via a byte array)
    bis.read(b);
    StringBuffer buf = new StringBuffer();
    buf.append(b);//from   w  w w .ja  v a  2  s .  c o m
    int start = buf.indexOf(errorTemplate);
    int end = start + errorTemplate.length();
    // replace placeholder with errormessage
    // (will automatically adjust to take longer or shorter data into account)
    buf.replace(start, end, error);
    return buf;
}

From source file:github.srlee309.lessWrongBookCreator.scraper.PostSectionExtractor.java

public boolean downloadFileFromURL(String fetchUrl, File outputFile)
        throws IOException, FileNotFoundException, IOException {

    HttpURLConnection c;/*  w  w w  . j  a v  a2s.  co m*/

    //save file       
    URL url = new URL(fetchUrl);
    c = (HttpURLConnection) url.openConnection();

    //set cache and request method settings  
    c.setUseCaches(false);
    c.setDoOutput(false);

    //set other headers  
    c.setRequestProperty("Content-Type", "image/jpeg");
    System.out.println(c.getContentType());
    //connect  
    c.connect();

    BufferedInputStream in = new BufferedInputStream(c.getInputStream());

    OutputStream out = new BufferedOutputStream(new FileOutputStream(outputFile));
    byte[] buf = new byte[256];
    int n = 0;
    while ((n = in.read(buf)) >= 0) {
        out.write(buf, 0, n);
    }
    out.flush();
    out.close();

    return true;
}

From source file:com.example.base.http.ResponseStream.java

public void readFile(String savePath) throws IOException {
    if (directResult != null)
        return;/* ww  w .j  av a2  s .c o m*/
    if (baseStream == null)
        return;
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(savePath);
        BufferedInputStream ins = new BufferedInputStream(baseStream);
        byte[] buffer = new byte[4096];
        int len = 0;
        while ((len = ins.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        out.flush();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (Throwable e) {
            }
        }
        if (baseStream != null) {
            try {
                baseStream.close();
            } catch (Throwable e) {
            }
        }
    }
}

From source file:com.teasoft.teavote.util.Signature.java

public boolean verifyFile(String pathToFile) throws FileNotFoundException, NoSuchAlgorithmException,
        NoSuchProviderException, InvalidKeyException, IOException, SignatureException, InvalidKeySpecException {
    File fileToVerify = new File(pathToFile);
    FileInputStream originalFileIs = new FileInputStream(fileToVerify);

    String dataPathToVerify = new ConfigLocation().getConfigPath() + File.separator + "dataToVerify.sql";
    File dataToVerify = new File(dataPathToVerify);
    FileOutputStream fw = new FileOutputStream(dataToVerify);

    //Read the caution message and signature out
    byte[] cautionMsg = new byte[175]; //Caution section occupies 175 bytes
    originalFileIs.read(cautionMsg);/*from w  w w. j  a v  a  2s .c o  m*/

    //Read and Write the rest of the bytes in original file into data to verify
    byte[] buffer = new byte[1024];
    int nRead = 0;
    while ((nRead = originalFileIs.read(buffer)) != -1) {
        fw.write(buffer, 0, nRead);
    }
    //Close originalFileIs and dataToVerify
    originalFileIs.close();
    fw.close();

    //Get signature from caution/signation message
    byte[] sigToVerify = new byte[64];
    for (int i = 0, j = 0; i < cautionMsg.length; i++) {
        if (i >= 106 && i < 170) {
            sigToVerify[j++] = cautionMsg[i];
        }
    }
    //decode signature bytes
    sigToVerify = Base64.decodeBase64(sigToVerify);
    //        sigToVerify = com.sun.org.apache.xml.internal.security.utils.Base64.decode(sigToVerify);

    java.security.Signature sig = java.security.Signature.getInstance("SHA1withDSA", "SUN");
    sig.initVerify(getPublicKey());
    //Get digital signature
    FileInputStream datafis = new FileInputStream(dataPathToVerify);
    BufferedInputStream bufin = new BufferedInputStream(datafis);
    int len;
    while (bufin.available() != 0) {
        len = bufin.read(buffer);
        sig.update(buffer, 0, len);
    }
    bufin.close();
    //Delete fileToVerify
    dataToVerify.delete();
    return sig.verify(sigToVerify);

}

From source file:com.sharksharding.util.web.http.QueryViewServlet.java

/**
 * ????//from   w  w  w .j  av  a 2  s.  c  o  m
 * 
 * @author gaoxianglong
 * 
 * @param path
 *            ?
 * 
 * @return byte[] ??
 */
protected byte[] initView(String path) {
    byte[] value = null;
    BufferedInputStream reader = null;
    try {
        reader = new BufferedInputStream(QueryViewServlet.class.getClassLoader().getResourceAsStream(path));
        value = new byte[reader.available()];
        reader.read(value);
    } catch (IOException e) {
        throw new FileNotFoundException("can not find config");
    } finally {
        if (null != reader) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return value;
}

From source file:com.ottogroup.bi.asap.node.server.AsapProcessingNodeServer.java

/**
 * Reads the contents from the referenced files and returns them as array of bytes
 * @param fileName//from   ww  w  .  ja  v a  2 s .co m
 * @return
 * @throws IOException
 */
protected byte[] readFileContents(final String fileName) throws IOException, RequiredInputMissingException {

    if (StringUtils.isBlank(fileName))
        throw new RequiredInputMissingException("Missing required input for parameter 'fileName'");

    ///////////////////////////////////////////////////////////////////////////////////
    // read repo file contents
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(fileName));
    int read = 0;
    final ByteArrayOutputStream os = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024]; // buffer size
    while ((read = bis.read(buffer)) != -1) {
        os.write(buffer, 0, read);
    }
    bis.close();
    //
    ///////////////////////////////////////////////////////////////////////////////////

    return os.toByteArray();
}

From source file:ca.christophersaunders.tutorials.sqlite.picasa.PicasaImage.java

private byte[] getBitmapBytesForLocation(String location) {
    try {//from  w  w  w.  j a v a2  s  .c  o  m
        URL thumbnailUrl = new URL(location);
        URLConnection conn = thumbnailUrl.openConnection();

        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        ByteArrayBuffer imageBytesBuffer = new ByteArrayBuffer(0x2000);
        byte[] buffer = new byte[1024];
        int current = 0;
        while ((current = bis.read(buffer)) != -1) {
            imageBytesBuffer.append(buffer, 0, current);
        }

        byte[] imageBytes = imageBytesBuffer.toByteArray();
        return imageBytes;

    } catch (Exception gottaCatchemAll) {
        Log.w("PicasaImage", "Gotta catch em' all!");
        Log.e("PicasaImage", String.format("Location string was probably bad: %s", location));
        gottaCatchemAll.printStackTrace();
    }
    return new byte[0];
}