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:Main.java

public static String readFile(String path) throws IOException {
    InputStream is = new FileInputStream(new File(path));
    DataInputStream ds = new DataInputStream(is);
    // Estimated capacity, potential risk ???
    byte[] bytes = new byte[is.available()];
    ds.readFully(bytes);//w w w. j a  v  a 2 s .  co m
    String text = new String(bytes);
    is.close();
    ds.close();
    return text;
}

From source file:Main.java

private static String bytesToString(byte[] bytes) {
    if (bytes == null) {
        return null;
    }/*ww  w .  j a  v  a2  s  . c  o m*/
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    DataInputStream dis = new DataInputStream(bais);
    String res = "";
    try {
        res = dis.readUTF();
    } catch (IOException ex) {
    } finally {
        try {
            dis.close();
            bais.close();
        } catch (IOException ex1) {
        }
        dis = null;
        bais = null;
    }
    return res;
}

From source file:Main.java

public static File WriteStreamToFile(InputStream resStream) {
    try {/*w w  w  . j  a  v  a 2 s .  c o  m*/
        byte[] bytes = new byte[resStream.available()];
        File tmpFile = File.createTempFile("z4-", ".tmp");
        tmpFile.deleteOnExit();
        DataInputStream dis = new DataInputStream(resStream);
        dis.readFully(bytes);
        FileOutputStream foutStream = new FileOutputStream(tmpFile.getPath());
        foutStream.write(bytes);
        foutStream.close();
        dis.close();
        return tmpFile;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:TripleDES.java

/** Read a TripleDES secret key from the specified file */
public static SecretKey readKey(File f)
        throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
    // Read the raw bytes from the keyfile
    DataInputStream in = new DataInputStream(new FileInputStream(f));
    byte[] rawkey = new byte[(int) f.length()];
    in.readFully(rawkey);//  www.  ja  va2 s  .  c  om
    in.close();

    // Convert the raw bytes to a secret key like this
    DESedeKeySpec keyspec = new DESedeKeySpec(rawkey);
    SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("DESede");
    SecretKey key = keyfactory.generateSecret(keyspec);
    return key;
}

From source file:org.thoughtcrime.securesms.mms.MmsCommunication.java

protected static byte[] parseResponse(HttpEntity entity) throws IOException {
    if (entity == null || entity.getContentLength() == 0)
        throw new IOException("Null response");

    byte[] responseBytes = new byte[(int) entity.getContentLength()];
    DataInputStream dataInputStream = new DataInputStream(entity.getContent());
    dataInputStream.readFully(responseBytes);
    dataInputStream.close();

    entity.consumeContent();/*  w  w  w  .  j a v a  2s .  c o  m*/
    return responseBytes;
}

From source file:com.wso2telco.services.bw.FileUtil.java

public static String ReadFullyIntoVar(String fullpath) {

    String result = "";

    try {//from w w w . ja  v  a 2s . c o m
        FileInputStream file = new FileInputStream(fullpath);
        DataInputStream in = new DataInputStream(file);
        byte[] b = new byte[in.available()];
        in.readFully(b);
        in.close();
        result = new String(b, 0, b.length, "Cp850");
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static byte[] readToEndAsArray(InputStream input) throws IOException {
    DataInputStream dis = new DataInputStream(input);
    byte[] stuff = new byte[1024];
    ByteArrayOutputStream buff = new ByteArrayOutputStream();
    int read = 0;
    while ((read = dis.read(stuff)) != -1) {
        buff.write(stuff, 0, read);//w  w w . ja  v a  2 s. c o  m
    }
    dis.close();
    return buff.toByteArray();
}

From source file:com.google.code.rptm.mailarchive.DefaultMailingListArchive.java

private static boolean isMboxFile(File file) throws IOException {
    byte[] firstBytes = new byte[5];
    DataInputStream in = new DataInputStream(new FileInputStream(file));
    try {/*from   w w  w  .  j a  v  a  2  s.  c o m*/
        in.readFully(firstBytes);
    } finally {
        in.close();
    }
    return Arrays.equals(firstBytes, MBOX_MAGIC);
}

From source file:tachyon.master.Image.java

/**
 * Load an image into the masterinfo./*from   w ww. ja  va 2  s  .  c  om*/
 * 
 * @param info the masterinfo to fill.
 * @param path the data to load
 * @throws IOException
 */
public static void load(MasterInfo info, String path) throws IOException {
    UnderFileSystem ufs = UnderFileSystem.get(path, info.getTachyonConf());
    if (!ufs.exists(path)) {
        LOG.info("Image " + path + " does not exist.");
        return;
    }
    LOG.info("Loading image " + path);
    DataInputStream imageIs = new DataInputStream(ufs.open(path));
    JsonParser parser = JsonObject.createObjectMapper().getFactory().createParser(imageIs);

    info.loadImage(parser, new TachyonURI(path));
    imageIs.close();
    ufs.close();
}

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

/**
 * Read fully into var./*from w w  w .j a va  2s.  co  m*/
 *
 * @param fullpath the fullpath
 * @return the string
 */
public static String ReadFullyIntoVar(String fullpath) {

    String result = "";

    try {
        FileInputStream file = new FileInputStream(fullpath);
        DataInputStream in = new DataInputStream(file);
        byte[] b = new byte[in.available()];
        in.readFully(b);
        in.close();
        result = new String(b, 0, b.length, "Cp850");
    } catch (Exception e) {
        log.error("Read fully into var Error" + e);
    }
    return result;
}