Example usage for java.io BufferedInputStream close

List of usage examples for java.io BufferedInputStream close

Introduction

In this page you can find the example usage for java.io BufferedInputStream close.

Prototype

public void close() throws IOException 

Source Link

Document

Closes this input stream and releases any system resources associated with the stream.

Usage

From source file:edu.du.penrose.systems.util.HttpClientUtils.java

/**
 * Appends response form URL to a StringBuffer
 * //from   w w w.java  2s . c  o  m
 * @param requestUrl
 * @param resultStringBuffer
 * @return int request status code OR -1 if an exception occurred
 */
static public int getAsString(String requestUrl, StringBuffer resultStringBuffer) {
    HttpClient client = new HttpClient();

    //   client.getState().setCredentials(
    //         new AuthScope("localhost", 7080, null ),
    //         new UsernamePasswordCredentials("nation", "nationPW") 
    //     );
    //   client.getParams().setAuthenticationPreemptive(true);

    HttpMethod method = new GetMethod(requestUrl);

    // method.setDoAuthentication( true );   
    // client.getParams().setAuthenticationPreemptive(true);

    // Execute and print response
    try {
        client.executeMethod(method);
        InputStream is = method.getResponseBodyAsStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        String datastr = null;
        byte[] bytes = new byte[8192]; // reading as chunk of 8192 bytes
        int count = bis.read(bytes);
        while (count != -1 && count <= 8192) {
            datastr = new String(bytes, 0, count);
            resultStringBuffer.append(datastr);
            count = bis.read(bytes);
        }
        bis.close();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        method.releaseConnection();
    }

    return method.getStatusCode();
}

From source file:eu.scape_project.spacip.ContainerProcessing.java

/**
 * Write ARC record content to output stream
 *
 * @param nativeArchiveRecord/*w  w  w .j  a va2 s .com*/
 * @param outputStream Output stream
 * @throws IOException
 */
public static void recordToOutputStream(ArchiveRecord nativeArchiveRecord, OutputStream outputStream)
        throws IOException {
    ARCRecord arcRecord = (ARCRecord) nativeArchiveRecord;
    ARCRecordMetaData metaData = arcRecord.getMetaData();
    long contentBegin = metaData.getContentBegin();
    BufferedInputStream bis = new BufferedInputStream(arcRecord);
    BufferedOutputStream bos = new BufferedOutputStream(outputStream);
    byte[] tempBuffer = new byte[BUFFER_SIZE];
    int bytesRead;
    // skip record header
    bis.skip(contentBegin);
    while ((bytesRead = bis.read(tempBuffer)) != -1) {
        bos.write(tempBuffer, 0, bytesRead);
    }
    bos.flush();
    bis.close();
    bos.close();
}

From source file:com.all.client.data.Hashcoder.java

public static String createHashCode(File file) {
    if (file == null) {
        return null;
    }//from  www.j av a 2  s  . c o m
    byte[] hashCode = null;
    try {
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);

        byte[] buffer = new byte[65536];

        bis.read(buffer);

        bis.close();
        fis.close();

        hashCode = md.digest(buffer);

    } catch (FileNotFoundException e) {
        log.error(e.getMessage(), e);
    } catch (IOException e) {
        log.error(e.getMessage(), e);
    }
    return toHex(hashCode);
}

From source file:Main.java

/**
 * Utility method that creates a <code>UIDefaults.LazyValue</code> that
 * creates an <code>ImageIcon</code> <code>UIResource</code> for the
 * specified image file name. The image is loaded using
 * <code>getResourceAsStream</code>, starting with a call to that method
 * on the base class parameter. If it cannot be found, searching will
 * continue through the base class' inheritance hierarchy, up to and
 * including <code>rootClass</code>.
 *
 * @param baseClass the first class to use in searching for the resource
 * @param rootClass an ancestor of <code>baseClass</code> to finish the
 *                  search at//from   w  ww. ja  v  a  2 s . c om
 * @param imageFile the name of the file to be found
 * @return a lazy value that creates the <code>ImageIcon</code>
 *         <code>UIResource</code> for the image,
 *         or null if it cannot be found
 */
public static Object makeIcon(final Class<?> baseClass, final Class<?> rootClass, final String imageFile) {

    return new UIDefaults.LazyValue() {
        public Object createValue(UIDefaults table) {
            /* Copy resource into a byte array.  This is
             * necessary because several browsers consider
             * Class.getResource a security risk because it
             * can be used to load additional classes.
             * Class.getResourceAsStream just returns raw
             * bytes, which we can convert to an image.
             */
            byte[] buffer = java.security.AccessController
                    .doPrivileged(new java.security.PrivilegedAction<byte[]>() {
                        public byte[] run() {
                            try {
                                InputStream resource = null;
                                Class<?> srchClass = baseClass;

                                while (srchClass != null) {
                                    resource = srchClass.getResourceAsStream(imageFile);

                                    if (resource != null || srchClass == rootClass) {
                                        break;
                                    }

                                    srchClass = srchClass.getSuperclass();
                                }

                                if (resource == null) {
                                    return null;
                                }

                                BufferedInputStream in = new BufferedInputStream(resource);
                                ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
                                byte[] buffer = new byte[1024];
                                int n;
                                while ((n = in.read(buffer)) > 0) {
                                    out.write(buffer, 0, n);
                                }
                                in.close();
                                out.flush();
                                return out.toByteArray();
                            } catch (IOException ioe) {
                                System.err.println(ioe.toString());
                            }
                            return null;
                        }
                    });

            if (buffer == null) {
                return null;
            }
            if (buffer.length == 0) {
                System.err.println("warning: " + imageFile + " is zero-length");
                return null;
            }

            return new ImageIconUIResource(buffer);
        }
    };
}

From source file:org.fusesource.meshkeeper.distribution.PluginClassLoader.java

private static Properties loadProperties(ClassLoader cl, String uri) throws IOException {
    InputStream in = cl.getResourceAsStream(uri);
    if (in == null) {
        return null;
    }//from  ww  w  .j  a  v  a  2  s  . c om

    // lets load the file
    BufferedInputStream reader = new BufferedInputStream(in);
    try {
        Properties properties = new Properties();
        properties.load(reader);
        return properties;
    } finally {
        try {
            reader.close();
        } catch (Exception e) {
        }
    }
}

From source file:com.linkedin.pinot.core.startree.StarTreeSerDe.java

/**
 * Given a star tree file, return its version (on-heap or off-heap).
 * Assumes that the file is a valid star tree file.
 *
 * @param starTreeFile/*  w ww  .j  ava 2s  .c  om*/
 * @return
 * @throws IOException
 */
public static StarTreeFormatVersion getStarTreeVersion(File starTreeFile) throws IOException {
    InputStream inputStream = new FileInputStream(starTreeFile);
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
    StarTreeFormatVersion version = getStarTreeVersion(bufferedInputStream);
    bufferedInputStream.close();
    return version;
}

From source file:edu.isi.wings.portal.classes.StorageHandler.java

private static void zipAndStream(File dir, ZipOutputStream zos, String prefix) throws Exception {
    byte bytes[] = new byte[2048];
    for (File file : dir.listFiles()) {
        if (file.isDirectory())
            StorageHandler.zipAndStream(file, zos, prefix + file.getName() + "/");
        else {// w  w w . ja  v  a2  s .  co  m
            FileInputStream fis = new FileInputStream(file.getAbsolutePath());
            BufferedInputStream bis = new BufferedInputStream(fis);
            zos.putNextEntry(new ZipEntry(prefix + file.getName()));
            int bytesRead;
            while ((bytesRead = bis.read(bytes)) != -1) {
                zos.write(bytes, 0, bytesRead);
            }
            zos.closeEntry();
            bis.close();
            fis.close();
        }
    }
}

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 . j  av a2s  . 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:com.dnielfe.manager.utils.SimpleUtils.java

/**
 * //from  www  .  j  a v  a2s . c o  m
 * @param old
 *            the file to be copied/ moved
 * @param newDir
 *            the directory to copy/move the file to
 */
public static void copyToDirectory(String old, String newDir) {
    File old_file = new File(old);
    File temp_dir = new File(newDir);
    byte[] data = new byte[BUFFER];
    int read = 0;

    if (old_file.isFile() && temp_dir.isDirectory() && temp_dir.canWrite()) {
        String file_name = old.substring(old.lastIndexOf("/"), old.length());
        File cp_file = new File(newDir + file_name);

        try {
            BufferedOutputStream o_stream = new BufferedOutputStream(new FileOutputStream(cp_file));
            BufferedInputStream i_stream = new BufferedInputStream(new FileInputStream(old_file));

            while ((read = i_stream.read(data, 0, BUFFER)) != -1)
                o_stream.write(data, 0, read);

            o_stream.flush();
            i_stream.close();
            o_stream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return;
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
    } else if (old_file.isDirectory() && temp_dir.isDirectory() && temp_dir.canWrite()) {
        String files[] = old_file.list();
        String dir = newDir + old.substring(old.lastIndexOf("/"), old.length());
        int len = files.length;

        if (!new File(dir).mkdir())
            return;

        for (int i = 0; i < len; i++)
            copyToDirectory(old + "/" + files[i], dir);

    } else if (old_file.isFile() && !temp_dir.canWrite() && SimpleExplorer.rootAccess) {
        RootCommands.moveCopyRoot(old, newDir);
    } else if (!temp_dir.canWrite())
        return;

    return;
}

From source file:Main.java

/**
 * Compress a String to a zip file that has only one entry like zipName
 * The name shouldn't have .zip/*from  w ww.  j a  va2  s.com*/
 * 
 * @param content
 * @param fName
 * @throws IOException 
 */
public static void zipAContentAsFileName(String fName, String content, String charset) throws IOException {
    ByteArrayInputStream bais = new ByteArrayInputStream(content.getBytes(charset));
    BufferedInputStream bis = new BufferedInputStream(bais);

    File f = new File(fName);
    File parentFile = f.getParentFile();
    if (!parentFile.exists()) {
        parentFile.mkdirs();
    }
    ZipOutputStream zf = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(f + ".zip")));

    ZipEntry entry = new ZipEntry(f.getName());
    zf.putNextEntry(entry);
    byte[] barr = new byte[8192];
    int len = 0;
    while ((len = bis.read(barr)) != -1) {
        zf.write(barr, 0, len);
    }
    zf.flush();
    zf.close();
    bis.close();
    bais.close();
}