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:mobisocial.musubi.social.FacebookFriendFetcher.java

public static byte[] getImageFromURL(String remoteUrl) {
    try {//from ww w . j av  a 2 s .  c o  m
        // Grab the content
        URL url = new URL(remoteUrl);
        URLConnection ucon = url.openConnection();
        InputStream is = ucon.getInputStream();

        // Read the content chunk by chunk
        BufferedInputStream bis = new BufferedInputStream(is, 8192);
        ByteArrayBuffer baf = new ByteArrayBuffer(0);
        byte[] chunk = new byte[CHUNK_SIZE];
        int current = bis.read(chunk);
        while (current != -1) {
            baf.append(chunk, 0, current);
            current = bis.read(chunk);
        }
        return baf.toByteArray();
    } catch (IOException e) {
        Log.e(TAG, "HTTP error", e);
    }
    return null;
}

From source file:com.googlecode.xmlzen.utils.FileUtils.java

/**
 * Reads a {@link File} and returns the contents as {@link String}
 * /*from ww  w . j av a2  s.c o  m*/
 * @param file File to read
 * @param charset Charset of this File
 * @return File contents as String
 */
public static String readFile(final File file, final String charset) {
    InputStream in = null;
    try {
        if (!file.isFile()) {
            return null;
        }
        in = new FileInputStream(file);
        final BufferedInputStream buffIn = new BufferedInputStream(in);
        final byte[] buffer = new byte[(int) Math.min(file.length(), BUFFER)];
        int read;
        final StringBuilder result = new StringBuilder();
        while ((read = buffIn.read(buffer)) != -1) {
            result.append(new String(buffer, 0, read, charset));
        }
        return result.toString();
    } catch (final Exception e) {
        throw new XmlZenException("Failed reading file: " + file, e);
    } finally {
        close(in);
    }
}

From source file:com.amalto.workbench.utils.ResourcesUtil.java

private static String readFileAsString(String fileName) throws Exception {
    FileInputStream fis = new FileInputStream(fileName);
    BufferedInputStream in = new BufferedInputStream(fis);
    byte buffer[] = new byte[256];
    StringBuffer picStr = new StringBuffer();
    Encoder encoder = Base64.getEncoder();
    while (in.read(buffer) >= 0) {
        picStr.append(encoder.encodeToString(buffer));
    }//  w  w w. ja va  2 s  . c  o m
    fis.close();
    fis = null;
    in.close();
    in = null;
    buffer = null;
    return picStr.toString();
}

From source file:Main.java

public static boolean copyFile(final File srcFile, final File saveFile) {
    File parentFile = saveFile.getParentFile();
    if (!parentFile.exists()) {
        if (!parentFile.mkdirs())
            return false;
    }/*from w  ww . j a  v a  2s  . com*/

    BufferedInputStream inputStream = null;
    BufferedOutputStream outputStream = null;
    try {
        inputStream = new BufferedInputStream(new FileInputStream(srcFile));
        outputStream = new BufferedOutputStream(new FileOutputStream(saveFile));
        byte[] buffer = new byte[1024 * 4];
        int len;
        while ((len = inputStream.read(buffer)) != -1) {
            outputStream.write(buffer, 0, len);
        }
        outputStream.flush();
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    } finally {
        close(inputStream, outputStream);
    }
    return true;
}

From source file:Main.java

public static boolean copyFile(File fromFile, File toFile) {
    BufferedInputStream bis = null;
    BufferedOutputStream fos = null;
    try {//  w  w w .j  a va  2s.  com
        FileInputStream is = new FileInputStream(fromFile);
        bis = new BufferedInputStream(is);
        fos = new BufferedOutputStream(new FileOutputStream(toFile));
        byte[] buf = new byte[2048];
        int i;
        while ((i = bis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fos.flush();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    return false;
}

From source file:Main.java

public static boolean saveFileFromAssetsToSystem(Context context, String path, File destFile) {
    BufferedInputStream bis = null;
    BufferedOutputStream fos = null;
    try {/*from  w  ww . j av  a  2  s .  c o  m*/
        InputStream is = context.getAssets().open(path);
        bis = new BufferedInputStream(is);
        fos = new BufferedOutputStream(new FileOutputStream(destFile));
        byte[] buf = new byte[2048];
        int i;
        while ((i = bis.read(buf)) != -1) {
            fos.write(buf, 0, i);
        }
        fos.flush();
        return true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (bis != null) {
                bis.close();
            }
            if (fos != null) {
                fos.close();
            }
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
    return false;
}

From source file:org.dataone.proto.trove.mn.http.client.DataHttpClientHandler.java

public static synchronized String executePost(HttpClient httpclient, HttpPost httpPost)
        throws ServiceFailure, NotFound, InvalidToken, NotAuthorized, IdentifierNotUnique, UnsupportedType,
        InsufficientResources, InvalidSystemMetadata, NotImplemented, InvalidCredentials, InvalidRequest,
        AuthenticationTimeout, UnsupportedMetadataType, HttpException {
    String response = null;/*from   w w  w .  j  av  a 2 s  .c o m*/

    try {
        HttpResponse httpResponse = httpclient.execute(httpPost);
        if (httpResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
            HttpEntity resEntity = httpResponse.getEntity();
            if (resEntity != null) {
                logger.debug("Response content length: " + resEntity.getContentLength());
                logger.debug("Chunked?: " + resEntity.isChunked());
                BufferedInputStream in = new BufferedInputStream(resEntity.getContent());
                ByteArrayOutputStream resOutputStream = new ByteArrayOutputStream();
                byte[] buff = new byte[32 * 1024];
                int len;
                while ((len = in.read(buff)) > 0) {
                    resOutputStream.write(buff, 0, len);
                }
                response = new String(resOutputStream.toByteArray());
                logger.debug(response);
                resOutputStream.close();
                in.close();
            }
        } else {
            HttpExceptionHandler.deserializeAndThrowException(httpResponse);
        }
    } catch (IOException ex) {
        throw new ServiceFailure("1002", ex.getMessage());
    }
    return response;
}

From source file:Main.java

public static void copyFile(String sourcePath, String toPath) {
    File sourceFile = new File(sourcePath);
    File targetFile = new File(toPath);
    createDipPath(toPath);//ww  w  .ja  va  2  s  .c o m
    try {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        try {
            inBuff = new BufferedInputStream(new FileInputStream(sourceFile));

            outBuff = new BufferedOutputStream(new FileOutputStream(targetFile));

            byte[] b = new byte[1024 * 5];
            int len;
            while ((len = inBuff.read(b)) != -1) {
                outBuff.write(b, 0, len);
            }
            outBuff.flush();
        } finally {
            if (inBuff != null)
                inBuff.close();
            if (outBuff != null)
                outBuff.close();
        }
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.openflamingo.uploader.util.ResourceUtils.java

/**
 *  ?  {@link org.springframework.core.io.Resource}? ? ? .
 *
 * @param inputStream   /* ww  w  .  j a va2s.c  o  m*/
 * @return {@link org.springframework.core.io.Resource}? ? 
 * @throws java.io.IOException  ?    
 */
public static String getResourceTextContents(InputStream inputStream) throws IOException {
    BufferedInputStream bis = new BufferedInputStream(inputStream);
    int size = bis.available();
    byte[] bytes = new byte[size];
    bis.read(bytes);
    return new String(bytes, "UTF-8");
}

From source file:ca.mudar.parkcatcher.utils.ParserUtils.java

/**
 * @author Andrew Pearson {@link http/*from  w ww . jav  a 2  s .  c om*/
 *         ://blog.andrewpearson.org/2010/07/android
 *         -why-to-use-json-and-how-to-use.html}
 * @param archiveQuery URL of JSON resources
 * @return String Raw content, requires use of JSONArray() and
 *         getJSONObject()
 * @throws IOException
 * @throws JSONException
 */
public static JSONTokener newJsonTokenerParser(InputStream input) throws IOException {
    String queryResult = "";

    BufferedInputStream bis = new BufferedInputStream(input);
    ByteArrayBuffer baf = new ByteArrayBuffer(50);
    int read = 0;
    int bufSize = 512;
    byte[] buffer = new byte[bufSize];
    while (true) {

        read = bis.read(buffer);
        if (read == -1) {
            break;
        }
        baf.append(buffer, 0, read);
    }
    final byte[] ba = baf.toByteArray();
    queryResult = new String(ba);

    JSONTokener data = new JSONTokener(queryResult);
    return data;
}