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:com.ibuildapp.romanblack.CataloguePlugin.utils.Utils.java

/**
 * download file url and save it/* w w  w .j av a 2  s  . c om*/
 *
 * @param url
 */
public static String downloadFile(String url) {
    int BYTE_ARRAY_SIZE = 1024;
    int CONNECTION_TIMEOUT = 30000;
    int READ_TIMEOUT = 30000;

    // downloading cover image and saving it into file
    try {
        URL imageUrl = new URL(URLDecoder.decode(url));
        URLConnection conn = imageUrl.openConnection();
        conn.setConnectTimeout(CONNECTION_TIMEOUT);
        conn.setReadTimeout(READ_TIMEOUT);
        BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());

        File resFile = new File(
                Statics.moduleCachePath + File.separator + com.appbuilder.sdk.android.Utils.md5(url));
        if (!resFile.exists()) {
            resFile.createNewFile();
        }

        FileOutputStream fos = new FileOutputStream(resFile);
        int current = 0;
        byte[] buf = new byte[BYTE_ARRAY_SIZE];
        Arrays.fill(buf, (byte) 0);
        while ((current = bis.read(buf, 0, BYTE_ARRAY_SIZE)) != -1) {
            fos.write(buf, 0, current);
            Arrays.fill(buf, (byte) 0);
        }

        bis.close();
        fos.flush();
        fos.close();
        Log.d("", "");
        return resFile.getAbsolutePath();
    } catch (SocketTimeoutException e) {
        return null;
    } catch (IllegalArgumentException e) {
        return null;
    } catch (Exception e) {
        return null;
    }
}

From source file:org.openo.nfvo.vnfmadapter.service.csm.connect.AbstractSslContext.java

/**readSSLConfToJson
 * @return/*from   www  .j  a  v  a2s.co  m*/
 * @throws IOException
 * @since NFVO 0.5
 */
public static JSONObject readSSLConfToJson() throws IOException {
    JSONObject sslJson = null;
    InputStream ins = null;
    BufferedInputStream bins = null;
    String fileContent = "";

    String fileName = SystemEnvVariablesFactory.getInstance().getAppRoot()
            + System.getProperty("file.separator") + "etc" + System.getProperty("file.separator") + "conf"
            + System.getProperty("file.separator") + "sslconf.json";

    try {
        ins = new FileInputStream(fileName);
        bins = new BufferedInputStream(ins);

        byte[] contentByte = new byte[ins.available()];
        int num = bins.read(contentByte);

        if (num > 0) {
            fileContent = new String(contentByte);
        }
        sslJson = JSONObject.fromObject(fileContent);
    } catch (FileNotFoundException e) {
        LOG.error(fileName + "is not found!", e);
    } catch (Exception e) {
        LOG.error("read sslconf file fail.please check if the 'sslconf.json' is exist.");
    } finally {
        if (ins != null) {
            ins.close();
        }
        if (bins != null) {
            bins.close();
        }
    }

    return sslJson;
}

From source file:com.yunmel.syncretic.utils.io.IOUtils.java

/**
 * ?/*from www. jav a2s .  c  om*/
 * 
 * @param files 
 * @param out ?
 * @throws IOException
 * @throws Exception
 */
public static void zipDownLoad(Map<File, String> downQuene, HttpServletResponse response) throws IOException {

    ServletOutputStream out = response.getOutputStream();
    ZipOutputStream zipout = new ZipOutputStream(out);
    ZipEntry entry = null;
    zipout.setLevel(1);
    // zipout.setEncoding("GBK");
    if (downQuene != null && downQuene.size() > 0) {
        for (Entry<File, String> fileInfo : downQuene.entrySet()) {
            File file = fileInfo.getKey();
            try {
                String filename = new String(fileInfo.getValue().getBytes(), "GBK");
                entry = new ZipEntry(filename);
                entry.setSize(file.length());
                zipout.putNextEntry(entry);
            } catch (IOException e) {
                // Logger.getLogger(FileUtil.class).warn(":", e);
            }
            BufferedInputStream fr = new BufferedInputStream(new FileInputStream(fileInfo.getKey()));
            int len;
            byte[] buffer = new byte[1024];
            while ((len = fr.read(buffer)) != -1)
                zipout.write(buffer, 0, len);
            fr.close();
        }
    }

    zipout.finish();
    zipout.flush();
    // out.flush();
}

From source file:com.piaoyou.util.FileUtil.java

/**
 * This method write srcFile to the output, and does not close the output
 * @param srcFile File the source (input) file
 * @param output OutputStream the stream to write to, this method will not buffered the output
 * @throws IOException/*  w w  w . j a  va  2 s .  c  o  m*/
 */
public static void popFile(File srcFile, OutputStream output) throws IOException {

    BufferedInputStream input = null;
    byte[] block = new byte[4096];
    try {
        input = new BufferedInputStream(new FileInputStream(srcFile), 4096);
        while (true) {
            int length = input.read(block);
            if (length == -1)
                break;// end of file
            output.write(block, 0, length);
        }
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException ex) {
                log.error("Cannot close stream", ex);
            }
        }
    }
}

From source file:gate.util.Files.java

/** Get a resource from the GATE ClassLoader as a byte array.
  *//*from   www . java  2 s.  c o  m*/
public static byte[] getResourceAsByteArray(String resourceName)
        throws IOException, IndexOutOfBoundsException, ArrayStoreException {

    InputStream resourceInputStream = getResourceAsStream(resourceName);
    BufferedInputStream resourceStream = new BufferedInputStream(resourceInputStream);
    byte b;
    final int bufSize = 1024;
    byte[] buf = new byte[bufSize];
    int i = 0;

    // get the whole resource into buf (expanding the array as needed)
    while ((b = (byte) resourceStream.read()) != -1) {
        if (i == buf.length) {
            byte[] newBuf = new byte[buf.length * 2];
            System.arraycopy(buf, 0, newBuf, 0, i);
            buf = newBuf;
        }
        buf[i++] = b;
    }

    // close the resource stream
    resourceStream.close();

    // copy the contents of buf to an array of the correct size
    byte[] bytes = new byte[i];
    // copy from buf to bytes
    System.arraycopy(buf, 0, bytes, 0, i);
    return bytes;
}

From source file:gate.util.Files.java

/** Get a resource from the GATE resources directory as a byte array.
  * The resource name should be relative to <code>resourcePath</code> which
  * is equal with <TT>gate/resources</TT>; e.g.
  * for a resource stored as <TT>gate/resources/jape/Test11.jape</TT>,
  * this method should be passed the name <TT>jape/Test11.jape</TT>.
  *//*ww  w.j  a v  a2s .  c om*/
public static byte[] getGateResourceAsByteArray(String resourceName)
        throws IOException, IndexOutOfBoundsException, ArrayStoreException {

    InputStream resourceInputStream = getGateResourceAsStream(resourceName);
    BufferedInputStream resourceStream = new BufferedInputStream(resourceInputStream);
    byte b;
    final int bufSize = 1024;
    byte[] buf = new byte[bufSize];
    int i = 0;

    // get the whole resource into buf (expanding the array as needed)
    while ((b = (byte) resourceStream.read()) != -1) {
        if (i == buf.length) {
            byte[] newBuf = new byte[buf.length * 2];
            System.arraycopy(buf, 0, newBuf, 0, i);
            buf = newBuf;
        }
        buf[i++] = b;
    }

    // close the resource stream
    resourceStream.close();

    // copy the contents of buf to an array of the correct size
    byte[] bytes = new byte[i];

    // copy from buf to bytes
    System.arraycopy(buf, 0, bytes, 0, i);
    return bytes;
}

From source file:de.tudarmstadt.ukp.teaching.uima.nounDecompounding.ranking.ProbabilityBased.java

/**
 * Constructor/*  w  w  w.  j  a  va2  s.com*/
 * @param aFinder
 */
public ProbabilityBased(Finder aFinder) {
    super(aFinder);

    try {
        Properties properties = new Properties();
        BufferedInputStream stream = new BufferedInputStream(
                new FileInputStream("src/main/resources/index.properties"));
        properties.load(stream);
        stream.close();

        FREQUENCY = new BigInteger(properties.getProperty("frequency"));
    } catch (Exception e) {
        FREQUENCY = new BigInteger("143782944956");
    }
}

From source file:de.thorstenberger.taskmodel.complex.complextaskdef.impl.ComplexTaskDefDAOImpl.java

public ComplexTaskDefRoot getComplexTaskDefRoot(InputStream complexTaskIS) throws TaskApiException {
    ComplexTaskDef complexTask;/*w  w  w. j  a  va2s.c om*/
    JAXBContext jc = createJAXBContext();
    Unmarshaller unmarshaller = null;
    try {
        unmarshaller = JAXBUtils.getJAXBUnmarshaller(jc);
        // enable validation
        unmarshaller.setSchema(JAXB2Validation.loadSchema("complexTaskDef.xsd"));

        BufferedInputStream bis = new BufferedInputStream(complexTaskIS);
        complexTask = (ComplexTaskDef) unmarshaller.unmarshal(bis);
        bis.close();

    } catch (JAXBException e1) {
        throw new TaskModelPersistenceException(e1);
    } catch (IOException e2) {
        throw new TaskModelPersistenceException(e2);
    } finally {
        if (unmarshaller != null)
            JAXBUtils.releaseJAXBUnmarshaller(jc, unmarshaller);
    }

    return new ComplexTaskDefRootImpl(complexTask, complexTaskFactory);
}

From source file:mpimp.assemblxweb.util.J5FileUtils.java

private static void zipDirectoryRecursively(String dirPath, BufferedInputStream origin, int BUFFER,
        ZipOutputStream out, byte[] data, String parentDirName) throws Exception {
    File directory = new File(dirPath);
    File content[] = directory.listFiles();

    for (int i = 0; i < content.length; i++) {
        if (content[i].isDirectory()) {
            String parentPath = parentDirName + File.separator + content[i].getName();
            zipDirectoryRecursively(content[i].getAbsolutePath(), origin, BUFFER, out, data, parentPath);
        } else {/*from w  w w  .j  a v  a 2s .co  m*/
            String filePathInDirectory = parentDirName + File.separator + content[i].getName();
            FileInputStream in = new FileInputStream(content[i].getAbsolutePath());
            origin = new BufferedInputStream(in, BUFFER);
            ZipEntry zipEntry = new ZipEntry(filePathInDirectory);
            out.putNextEntry(zipEntry);
            int count;
            while ((count = origin.read(data, 0, BUFFER)) != -1) {
                out.write(data, 0, count);
            }
            origin.close();
            in.close();
        }
    }
}

From source file:echopoint.tucana.event.DefaultUploadCallback.java

/**
 * Over-ridden to save the contents of the uploaded file to {@link #directory}.
 * Client-code should ideally wrap this method in a try-catch clause to
 * handle file copying errors and update the UI as appropriate.
 *
 * {@inheritDoc}// www . j av a  2s  .  c o  m
 * @throws RuntimeException If errors are encountered while copying the
 *   uploaded file to the specified directory.
 */
@Override
public void uploadSucceeded(final UploadFinishEvent event) {
    final File temp = getTempFile();
    final File file = getFileName(event.getFileName());

    try {
        event.getFileItem().write(temp);

        if (!temp.renameTo(file)) {
            final BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
            final BufferedInputStream bis = new BufferedInputStream(new FileInputStream(temp));
            IOUtils.copy(bis, bos);
            bis.close();
            bos.close();
            temp.delete();
            logger.log(level,
                    "Rename of temp file to " + file.getAbsolutePath() + " failed.  Recopied from source.");
        }

        event.getFileItem().delete();

        logger.log(level, "Copied upload file contents to: " + file.getAbsolutePath());
    } catch (Exception e) {
        throw new RuntimeException("Error copying uploaded file!", e);
    }

    super.uploadSucceeded(event);
}