Example usage for java.io DataInputStream close

List of usage examples for java.io DataInputStream close

Introduction

In this page you can find the example usage for java.io DataInputStream 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.bahmanm.karun.PacmanConfHelper.java

/**
 * Extracts valuable info from 'pacman.conf'.
 *///  w w  w  .jav a 2  s.c om
private void extractInfo() throws FileNotFoundException, IOException {
    FileInputStream fis = new FileInputStream(confPath);
    DataInputStream dis = new DataInputStream(fis);
    BufferedReader br = new BufferedReader(new InputStreamReader(dis));
    String line;
    try {
        while ((line = br.readLine()) != null) {
            line = StringUtils.normalizeSpace(line);
            if (line.startsWith("#")) {
                continue;
            } else if (line.startsWith("[") && line.endsWith("]")) {
                String section = line.substring(1, line.length() - 1);
                if (!section.equals("options")) {
                    repos.add(section);
                }
            } else if (line.startsWith("DBPath")) {
                dbPath = line.split("=")[1].trim();
            } else if (line.startsWith("CacheDir")) {
                cacheDir = line.split("=")[1].trim();
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(PacmanConfHelper.class.getName()).log(Level.SEVERE, null, ex);
        try {
            br.close();
            dis.close();
            fis.close();
        } catch (IOException ioex) {
            throw new IOException("Error closing stream or reader: " + ioex.getMessage());
        }
    }
}

From source file:com.dv.Utils.DvFileUtil.java

/**
 * ??byte[].//from w ww  .j a  v  a  2  s .  c  om
 * @param imgByte byte[]
 * @param fileName ?????.jpg
 * @param type ???AbConstant
 * @param newWidth 
 * @param newHeight 
 * @return Bitmap 
 */
public static Bitmap getBitmapFormByte(byte[] imgByte, String fileName, int type, int newWidth, int newHeight) {
    FileOutputStream fos = null;
    DataInputStream dis = null;
    ByteArrayInputStream bis = null;
    Bitmap b = null;
    File file = null;
    try {
        if (imgByte != null) {

            file = new File(imageDownFullDir + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            int readLength = 0;
            bis = new ByteArrayInputStream(imgByte);
            dis = new DataInputStream(bis);
            byte[] buffer = new byte[1024];

            while ((readLength = dis.read(buffer)) != -1) {
                fos.write(buffer, 0, readLength);
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                }
            }
            fos.flush();

            b = getBitmapFromSD(file, type, newWidth, newHeight);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dis != null) {
            try {
                dis.close();
            } catch (Exception e) {
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (Exception e) {
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (Exception e) {
            }
        }
    }
    return b;
}

From source file:core.Inject.java

private String[] loadPayloadsFromfile(String pathTo_payloads) {
    String[] _payloads = null;/*w w w  . ja v a2 s  .com*/

    try {
        FileInputStream fstream = new FileInputStream(pathTo_payloads);
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;
        int i = 0;

        LineNumberReader lnr = new LineNumberReader(new FileReader(pathTo_payloads));
        lnr.skip(Long.MAX_VALUE);

        _payloads = new String[lnr.getLineNumber()];

        while ((strLine = br.readLine()) != null) {
            _payloads[i] = strLine;
            i++;
        }

        in.close();
    } catch (Exception e) {
        Debug.printError("ERROR: unable to load the attack payloads");
        HaltHandler.quit_nok();
    }

    return _payloads;
}

From source file:de.tudarmstadt.ukp.dkpro.lexsemresource.graph.PersistentAdjacencyMatrix.java

/**
  * Read the file with the adjacencies and load it into a sparse matrix object
  *//* ww w. j a  v  a2s . co m*/
public FlexCompRowMatrix loadAdjMatrix() {

    logger.info("Loading the adjacency matrix...");

    // initialize adjacency matrix
    flexAdjMatrix = new FlexCompRowMatrix(resourceSize, resourceSize);
    int row = 0;

    try {
        // Open the file
        FileInputStream fstream = new FileInputStream("PersistentAdjacencies" + "_" + resourceName);
        // Get the object of DataInputStream
        DataInputStream in = new DataInputStream(fstream);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        String strLine;

        //Read File Line By Line
        while ((strLine = br.readLine()) != null) {

            // if the line is not empty, read it and fill the corresponding adjacency matrix entries:
            if (!strLine.equals("")) {
                String[] adjacencies = strLine.split(" ");
                for (String adjacency : adjacencies) {
                    int col = Integer.parseInt(adjacency);
                    flexAdjMatrix.set(row, col, 1);
                }
            }
            row++;

            // print progress
            if (row % 10000 == 0) {
                logger.info("Progress: " + (100 * row / resourceSize) + "%");
            }
        }

        //Close the input stream
        in.close();

        //Catch exception if any
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
    }

    logger.info("Adjacency matrix loaded from file.\n");
    return flexAdjMatrix;
}

From source file:com.pszh.ablibrary.util.AbFileUtil.java

/**
 * ??byte[].//from  w w  w. java  2  s.  c o  m
 * @param imgByte byte[]
 * @param fileName ?????.jpg
 * @param type ???AbConstant
 * @param desiredWidth 
 * @param desiredHeight 
 * @return Bitmap 
 */
public static Bitmap getBitmapFromByte(byte[] imgByte, String fileName, int type, int desiredWidth,
        int desiredHeight) {
    FileOutputStream fos = null;
    DataInputStream dis = null;
    ByteArrayInputStream bis = null;
    Bitmap bitmap = null;
    File file = null;
    try {
        if (imgByte != null) {

            file = new File(imageDownloadDir, fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            int readLength = 0;
            bis = new ByteArrayInputStream(imgByte);
            dis = new DataInputStream(bis);
            byte[] buffer = new byte[1024];

            while ((readLength = dis.read(buffer)) != -1) {
                fos.write(buffer, 0, readLength);
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                }
            }
            fos.flush();

            bitmap = getBitmapFromSD(file, type, desiredWidth, desiredHeight);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dis != null) {
            try {
                dis.close();
            } catch (Exception e) {
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (Exception e) {
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (Exception e) {
            }
        }
    }
    return bitmap;
}

From source file:org.agnitas.util.AgnUtils.java

/**
 * Reads a file.//  w ww .java  2 s .co  m
 */
public static String readFile(String path) {
    String value = null;

    try {
        File aFile = new File(path);
        byte[] b = new byte[(int) aFile.length()];
        DataInputStream in = new DataInputStream(new FileInputStream(aFile));
        in.readFully(b);
        value = new String(b);
        in.close();
    } catch (Exception e) {
        value = null;
    }

    return value;
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.harvester.FileHarvestController.java

private void doDownloadTemplatePost(HttpServletRequest request, HttpServletResponse response) {

    VitroRequest vreq = new VitroRequest(request);
    FileHarvestJob job = getJob(vreq, vreq.getParameter(PARAMETER_JOB));
    File fileToSend = new File(job.getTemplateFilePath());

    response.setContentType("application/octet-stream");
    response.setContentLength((int) (fileToSend.length()));
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileToSend.getName() + "\"");

    try {/*from w  w w . j a  v a 2s .  c o  m*/
        byte[] byteBuffer = new byte[(int) (fileToSend.length())];
        DataInputStream inStream = new DataInputStream(new FileInputStream(fileToSend));

        ServletOutputStream outputStream = response.getOutputStream();
        for (int length = inStream.read(byteBuffer); length != -1; length = inStream.read(byteBuffer)) {
            outputStream.write(byteBuffer, 0, length);
        }

        inStream.close();
        outputStream.flush();
        outputStream.close();
    } catch (IOException e) {
        log.error(e, e);
    }
}

From source file:cn.org.eshow.framwork.util.AbFileUtil.java

/**
 * ??byte[].//from w w w  . j  a v  a2s . c  o m
 * @param imgByte byte[]
 * @param fileName ?????.jpg
 * @param type ???AbConstant
 * @param desiredWidth 
 * @param desiredHeight 
 * @return Bitmap 
 */
public static Bitmap getBitmapFromByte(byte[] imgByte, String fileName, int type, int desiredWidth,
        int desiredHeight) {
    FileOutputStream fos = null;
    DataInputStream dis = null;
    ByteArrayInputStream bis = null;
    Bitmap bitmap = null;
    File file = null;
    try {
        if (imgByte != null) {

            file = new File(imageDownloadDir + fileName);
            if (!file.exists()) {
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            int readLength = 0;
            bis = new ByteArrayInputStream(imgByte);
            dis = new DataInputStream(bis);
            byte[] buffer = new byte[1024];

            while ((readLength = dis.read(buffer)) != -1) {
                fos.write(buffer, 0, readLength);
                try {
                    Thread.sleep(500);
                } catch (Exception e) {
                }
            }
            fos.flush();

            bitmap = getBitmapFromSD(file, type, desiredWidth, desiredHeight);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (dis != null) {
            try {
                dis.close();
            } catch (Exception e) {
            }
        }
        if (bis != null) {
            try {
                bis.close();
            } catch (Exception e) {
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (Exception e) {
            }
        }
    }
    return bitmap;
}

From source file:BA.Server.FileUpload.java

/** 
 * Handles the HTTP <code>POST</code> method.
 * @param request servlet request//from   w  w  w .  j av a2s . c o m
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@SuppressWarnings("unchecked")
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    PrintWriter outp = resp.getWriter();

    PrintWriter writer = null;

    try {
        writer = resp.getWriter();
    } catch (IOException ex) {
        log(FileUpload.class.getName() + "has thrown an exception: " + ex.getMessage());
    }

    StringBuffer buff = new StringBuffer();

    File file1 = (File) req.getAttribute("file");

    if (file1 == null || !file1.exists()) {
        System.out.println("File does not exist");
    } else if (file1.isDirectory()) {
        System.out.println("File is a directory");
    }

    else {
        File outputFile = new File("/tmp/" + req.getParameter("file"));
        file1.renameTo(outputFile);

        FileInputStream f = new FileInputStream(outputFile);

        // Here BufferedInputStream is added for fast reading.
        BufferedInputStream bis = new BufferedInputStream(f);
        DataInputStream dis = new DataInputStream(bis);
        int i = 0;
        String result = "";

        writer.write("<html>");
        writer.write("<head><script type='text/javascript'>");
        while (dis.available() != 0) {
            String current = dis.readLine();

            if (((String) req.getParameter("uploadType")).equals("equations")) {
                if (FormulaTester.testInput(current) == -1) {
                    writer.write("window.opener.addTabExt(\"" + current + "\" , \"" + req.getParameter("file")
                            + "\");");
                }
            } else {
                writer.write("window.opener.addMacroExt(\"" + current + "\");");
            }
            i++;

        }

        writer.write("this.close();</script></head>");
        writer.write("</script></head>");
        writer.write("<body>");
        writer.write("</body>");
        writer.write("</html>");

        writer.flush();
        writer.close();

        dis.close();
        bis.close();
        f.close();
        outputFile.delete();

    }
}

From source file:bobs.is.compress.sevenzip.SevenZFile.java

private DataInputStream readEncodedHeader(final DataInputStream header, final Archive archive,
        final byte[] password) throws IOException {
    readStreamsInfo(header, archive);//w ww  .  j av a  2 s.  c o  m

    // FIXME: merge with buildDecodingStream()/buildDecoderStack() at some stage?
    final Folder folder = archive.folders[0];
    final int firstPackStreamIndex = 0;
    final long folderOffset = SIGNATURE_HEADER_SIZE + archive.packPos + 0;

    file.seek(folderOffset);
    InputStream inputStreamStack = new BoundedRandomAccessFileInputStream(file,
            archive.packSizes[firstPackStreamIndex]);
    for (final Coder coder : folder.getOrderedCoders()) {
        if (coder.numInStreams != 1 || coder.numOutStreams != 1) {
            throw new IOException("Multi input/output stream coders are not yet supported");
        }
        inputStreamStack = Coders.addDecoder(fileName, inputStreamStack, folder.getUnpackSizeForCoder(coder),
                coder, password);
    }
    if (folder.hasCrc) {
        inputStreamStack = new CRC32VerifyingInputStream(inputStreamStack, folder.getUnpackSize(), folder.crc);
    }
    final byte[] nextHeader = new byte[(int) folder.getUnpackSize()];
    final DataInputStream nextHeaderInputStream = new DataInputStream(inputStreamStack);
    try {
        nextHeaderInputStream.readFully(nextHeader);
    } finally {
        nextHeaderInputStream.close();
    }
    return new DataInputStream(new ByteArrayInputStream(nextHeader));
}