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.orange.ocara.model.export.docx.DocxWriterTest.java

/**
 * Determine whether a file is a ZIP File.
 *///  w  ww .  j  a v a 2 s.  c  o  m
private static boolean isZipFile(File file) throws IOException {
    if (file.isDirectory()) {
        return false;
    }
    if (!file.canRead()) {
        throw new IOException("Cannot read file " + file.getAbsolutePath());
    }
    if (file.length() < 4) {
        return false;
    }
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file)));
    int test = in.readInt();
    in.close();
    return test == 0x504b0304;
}

From source file:com.hernandez.rey.crypto.TripleDES.java

/**
 * Read a TripleDES secret key from the specified file
 * /*from  w  w w . jav a2s .c  om*/
 * @param keyFile key file to read
 * @return secret key appropriate for encryption/decryption with 3DES
 * @throws IOException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeyException
 * @throws InvalidKeySpecException
 */
public static SecretKey readKey(final File keyFile)
        throws IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
    // Read the raw bytes from the keyfile
    final DataInputStream in = new DataInputStream(new FileInputStream(keyFile));
    final byte[] rawkey = new byte[(int) keyFile.length()];
    in.readFully(rawkey);
    in.close();

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

From source file:br.com.manish.ahy.kernel.util.FileUtil.java

public static byte[] readFileAsBytes(String path) {

    byte[] fileArray = null;

    try {//from  w w w .  ja  v a 2s.  c om
        File file = new File(path);
        if (file.length() > Integer.MAX_VALUE) {
            throw new OopsException("Oversized file :-( can't read it, sorry: " + path);
        }

        fileArray = new byte[(int) file.length()];
        DataInputStream dis;
        dis = new DataInputStream(new FileInputStream(file));
        dis.readFully(fileArray);
        dis.close();

    } catch (Exception e) {
        throw new OopsException(e, "Problems when reading: [" + path + "].");
    }

    return fileArray;
}

From source file:org.fdroid.enigtext.mms.MmsCommunication.java

protected static byte[] parseResponse(HttpEntity entity) throws IOException {
    if (entity == null || entity.getContentLength() == 0)
        return null;

    if (entity.getContentLength() < 0)
        throw new IOException("Unknown content length!");

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

    entity.consumeContent();//from ww w .  ja  v a 2s.  c  o m
    return responseBytes;
}

From source file:org.apache.hadoop.fs.FileContextTestHelper.java

public static byte[] readFile(FileContext fc, Path path, int len) throws IOException {
    DataInputStream dis = fc.open(path);
    byte[] buffer = new byte[len];
    IOUtils.readFully(dis, buffer, 0, len);
    dis.close();
    return buffer;
}

From source file:Main.java

/**
 * @param context//from w  w  w  . j  a v a2s .  com
 * @param filePath file path relative to assets, like request_init1/search_index.json
 * @return
 */
public static String readAssert(Context context, String filePath) {
    try {
        if (filePath.startsWith(File.separator)) {
            filePath = filePath.substring(File.separator.length());
        }
        AssetManager assetManager = context.getAssets();
        InputStream inputStream = assetManager.open(filePath);
        DataInputStream stream = new DataInputStream(inputStream);
        int length = stream.available();
        byte[] buffer = new byte[length];
        stream.readFully(buffer);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byteArrayOutputStream.write(buffer);
        stream.close();
        return byteArrayOutputStream.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static int[] readInts(String file) throws IOException {
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file), 4 * 1024));
    try {//w w  w.ja  v  a 2 s .c  o  m
        int len = in.readInt();
        int[] ints = new int[len];
        for (int i = 0; i < len; i++) {
            ints[i] = in.readInt();
        }
        return ints;
    } finally {
        in.close();
    }
}

From source file:Main.java

public static float[] readFloats(String file) throws IOException {
    DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(file), 4 * 1024));
    try {//  w  ww .ja v  a2s . c  o m
        int len = in.readInt();
        float[] floats = new float[len];
        for (int i = 0; i < len; i++) {
            floats[i] = in.readFloat();
        }
        return floats;
    } finally {
        in.close();
    }
}

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

/**
 * Determine the checkpoint time of the specified StorageDirectory
 *
 * @param sd StorageDirectory to check/* w  w  w . j a  v  a2  s.c om*/
 * @return If file exists and can be read, last checkpoint time. If not, 0L.
 * @throws IOException On errors processing file pointed to by sd
 */
static long readCheckpointTime(StorageDirectory sd) throws IOException {
    File timeFile = NNStorage.getStorageFile(sd, NameNodeFile.TIME);
    long timeStamp = 0L;
    if (timeFile.exists() && FileUtil.canRead(timeFile)) {
        DataInputStream in = new DataInputStream(new FileInputStream(timeFile));
        try {
            timeStamp = in.readLong();
            in.close();
            in = null;
        } finally {
            IOUtils.cleanup(LOG, in);
        }
    }
    return timeStamp;
}

From source file:org.codice.opendx.TestNITFInputTransformer.java

private static byte[] getThumbnailBytes() throws IOException {
    DataInputStream is = new DataInputStream(new FileInputStream(
            Thread.currentThread().getContextClassLoader().getResource("binfile.dat").getFile()));
    byte[] thumbnail = new byte[is.available()];
    is.readFully(thumbnail);// w  ww  . j av  a  2s.co  m
    is.close();
    return thumbnail;
}