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:nz.govt.natlib.ndha.wctdpsdepositor.extractor.XPathWctMetsExtractor.java

public void parseFile(File wctMets, FileArchiveBuilder fileBuilder) throws IOException {
    DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(wctMets)));
    byte[] buff = new byte[(int) wctMets.length()];
    dis.readFully(buff);//from  w ww . ja va  2 s.com
    dis.close();

    String xmlEscapedString = escapeXml(buff);
    parseFile(xmlEscapedString.getBytes(), wctMets.getName(), fileBuilder);
}

From source file:it.evilsocket.dsploit.core.System.java

private static void preloadServices() {
    if (mServices == null || mPorts == null) {
        try {// ww  w  . j a v a  2s .  co m
            // preload network service map and mac vendors 
            mServices = new HashMap<String, String>();
            mPorts = new HashMap<String, String>();

            FileInputStream fstream = new FileInputStream(
                    mContext.getFilesDir().getAbsolutePath() + "/tools/nmap/nmap-services");
            DataInputStream in = new DataInputStream(fstream);
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            String line;
            Matcher matcher;

            while ((line = reader.readLine()) != null) {
                line = line.trim();

                if ((matcher = SERVICE_PARSER.matcher(line)) != null && matcher.find()) {
                    String proto = matcher.group(1), port = matcher.group(2);

                    mServices.put(proto, port);
                    mPorts.put(port, proto);
                }
            }

            in.close();
        } catch (Exception e) {
            errorLogging(TAG, e);
        }
    }
}

From source file:com.datatorrent.contrib.hdht.MockFileAccess.java

@Override
public FileReader getReader(final long bucketKey, final String fileName) throws IOException {
    final HashMap<Slice, Pair<byte[], Integer>> data = Maps.newHashMap();
    final ArrayList<Slice> keys = Lists.newArrayList();
    final MutableInt index = new MutableInt();

    DataInputStream is = getInputStream(bucketKey, fileName);
    Input input = new Input(is);
    while (!input.eof()) {
        byte[] keyBytes = kryo.readObject(input, byte[].class);
        byte[] value = kryo.readObject(input, byte[].class);
        Slice key = new Slice(keyBytes, 0, keyBytes.length);
        data.put(key, new Pair<byte[], Integer>(value, keys.size()));
        keys.add(key);/*w w w  . j ava2 s  . c  om*/
    }
    input.close();
    is.close();

    return new FileReader() {

        @Override
        public void readFully(TreeMap<Slice, Slice> result) throws IOException {
            for (Map.Entry<Slice, Pair<byte[], Integer>> e : data.entrySet()) {
                result.put(e.getKey(), new Slice(e.getValue().first));
            }
        }

        @Override
        public void reset() throws IOException {
            index.setValue(0);
        }

        @Override
        public boolean seek(Slice key) throws IOException {
            Pair<byte[], Integer> v = data.get(key);
            if (v == null) {
                index.setValue(0);
                return false;
            }
            index.setValue(v.second);
            return true;
        }

        @Override
        public boolean next(Slice key, Slice value) throws IOException {

            if (deletedFiles.contains("" + bucketKey + fileName)) {
                throw new IOException("Simulated error for deleted file: " + fileName);
            }

            int pos = index.intValue();
            if (pos < keys.size()) {
                Slice k = keys.get(pos);
                key.buffer = k.buffer;
                key.offset = k.offset;
                key.length = k.length;
                Pair<byte[], Integer> v = data.get(k);
                value.buffer = v.first;
                value.offset = 0;
                value.length = v.first.length;
                index.increment();
                return true;
            }
            return false;
        }

        @Override
        public void close() throws IOException {
        }
    };
}

From source file:org.openxdata.server.servlet.WMDownloadServlet.java

private void downloadForms(HttpServletResponse response, int studyId) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);
    formDownloadService.downloadForms(studyId, dos, "", "");
    baos.flush();//from ww  w.  ja  v  a2 s .com
    dos.flush();
    byte[] data = baos.toByteArray();
    baos.close();
    dos.close();
    DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data));

    PrintWriter out = response.getWriter();
    out.println("<study>");

    try {
        dis.readByte(); //reads the size of the studies

        while (true) {
            String value = dis.readUTF();
            out.println("<form>" + value + "</form>");
        }
    } catch (EOFException exe) {
        //exe.printStackTrace();
    }

    out.println("</study>");
    out.flush();
    dis.close();
}

From source file:Bookmark.java

protected Object nativeToJava(TransferData transferData) {
    if (isSupportedType(transferData)) {
        byte[] raw = (byte[]) super.nativeToJava(transferData);
        if (raw == null)
            return null;
        Bookmark bookmark = new Bookmark();

        try {//from  w w  w .  j  a  va2 s.c om
            ByteArrayInputStream stream = new ByteArrayInputStream(raw);
            DataInputStream in = new DataInputStream(stream);
            bookmark.name = in.readUTF();
            bookmark.href = in.readUTF();
            bookmark.addDate = in.readUTF();
            bookmark.lastVisited = in.readUTF();
            bookmark.lastModified = in.readUTF();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        return bookmark;
    } else {
        return null;
    }
}

From source file:com.momock.http.HttpSession.java

public static DownloadInfo getDownloadInfo(File file) {
    if (file.exists()) {
        return new DownloadInfo(file.length(), file.length());
    }/*from  w w  w.  j  av a  2  s .  c o  m*/
    DownloadInfo di = null;
    File fileData = new File(file.getPath() + ".data");
    File fileInfo = new File(file.getPath() + ".info");
    if (fileData.exists() && fileInfo.exists()) {
        long downloadedLength = fileData.length();
        long contentLength = -1;
        DataInputStream din;
        if (fileInfo.length() == 0)
            return new DownloadInfo(0, 0);
        try {
            din = new DataInputStream(new FileInputStream(fileInfo));

            int headerCount = din.readInt();
            for (int i = 0; i < headerCount; i++) {
                String key = din.readUTF();
                int count = din.readInt();
                List<String> vals = new ArrayList<String>();
                for (int j = 0; j < count; j++) {
                    String val = din.readUTF();
                    vals.add(val);
                }
                if ("Content-Length".equals(key)) {
                    if (contentLength == -1)
                        contentLength = Convert.toInteger(vals.get(0));
                } else if ("Content-Range".equals(key)) {
                    int pos = vals.get(0).indexOf('/');
                    contentLength = Convert.toInteger(vals.get(0).substring(pos + 1));
                }
            }
            din.close();
            di = new DownloadInfo(downloadedLength, contentLength);
        } catch (Exception e) {
            Logger.error(e);
        }
    }
    return di;
}

From source file:WriteReadMixedDataTypesExample.java

public void commandAction(Command command, Displayable displayable) {
    if (command == exit) {
        destroyApp(true);// ww w.ja  va 2  s .  c o m
        notifyDestroyed();
    } else if (command == start) {
        try {
            recordstore = RecordStore.openRecordStore("myRecordStore", true);
        } catch (Exception error) {
            alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            byte[] outputRecord;
            String outputString = "First Record";
            int outputInteger = 15;
            boolean outputBoolean = true;
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream outputDataStream = new DataOutputStream(outputStream);
            outputDataStream.writeUTF(outputString);
            outputDataStream.writeBoolean(outputBoolean);
            outputDataStream.writeInt(outputInteger);
            outputDataStream.flush();
            outputRecord = outputStream.toByteArray();
            recordstore.addRecord(outputRecord, 0, outputRecord.length);
            outputStream.reset();
            outputStream.close();
            outputDataStream.close();
        } catch (Exception error) {
            alert = new Alert("Error Writing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            String inputString = null;
            int inputInteger = 0;
            boolean inputBoolean = false;
            byte[] byteInputData = new byte[100];
            ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
            DataInputStream inputDataStream = new DataInputStream(inputStream);
            for (int x = 1; x <= recordstore.getNumRecords(); x++) {
                recordstore.getRecord(x, byteInputData, 0);
                inputString = inputDataStream.readUTF();
                inputBoolean = inputDataStream.readBoolean();
                inputInteger = inputDataStream.readInt();
                inputStream.reset();
            }
            inputStream.close();
            inputDataStream.close();
            alert = new Alert("Reading", inputString + " " + inputInteger + " " + inputBoolean, null,
                    AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        } catch (Exception error) {
            alert = new Alert("Error Reading", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        try {
            recordstore.closeRecordStore();
        } catch (Exception error) {
            alert = new Alert("Error Closing", error.toString(), null, AlertType.WARNING);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert);
        }
        if (RecordStore.listRecordStores() != null) {
            try {
                RecordStore.deleteRecordStore("myRecordStore");
            } catch (Exception error) {
                alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
                alert.setTimeout(Alert.FOREVER);
                display.setCurrent(alert);
            }
        }
    }
}

From source file:com.bigdata.dastor.io.SSTableReader.java

void loadBloomFilter() throws IOException {
    DataInputStream stream = new DataInputStream(new FileInputStream(filterFilename()));
    try {/*from   w  w w . j ava 2s  . c  o  m*/
        bf = BloomFilter.serializer().deserialize(stream);
    } finally {
        stream.close();
    }
}

From source file:ZipImploder.java

protected void copyFileEntry(ZipOutputStream zos, File f) throws IOException {
    DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
    try {//  w  w w  .j a  va 2 s .  c o m
        copyFileEntry(zos, dis);
    } finally {
        try {
            dis.close();
        } catch (IOException ioe) {
        }
    }
}

From source file:org.apache.hadoop.hdfs.server.namenode.TestStorageRestore.java

/**
 * This function returns a md5 hash of a file.
 *
 * @param file input file//from  w w w  . j  ava 2  s.c om
 * @return The md5 string
 */
public String getFileMD5(File file) throws Exception {
    String res = new String();
    MessageDigest mD = MessageDigest.getInstance("MD5");
    DataInputStream dis = new DataInputStream(new FileInputStream(file));

    try {
        while (true) {
            mD.update(dis.readByte());
        }
    } catch (EOFException eof) {
    }

    BigInteger bigInt = new BigInteger(1, mD.digest());
    res = bigInt.toString(16);
    dis.close();

    return res;
}