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

/**
 * Determine whether a file is a ZIP File.
 *//*from  w ww .  j  a va2s.  co m*/
public 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:Main.java

public static int byteArrayToInt(byte[] input) {
    try {//from  w  w w. ja  v a2s. c o  m
        ByteArrayInputStream bis = new ByteArrayInputStream(input);
        DataInputStream dis = new DataInputStream(bis);
        int numb = dis.readInt();
        dis.close();
        return numb;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static short byteArrayToShort(byte[] input) {
    try {/*from  w ww  .j a v  a 2  s. c  o  m*/
        ByteArrayInputStream bis = new ByteArrayInputStream(input);
        DataInputStream dis = new DataInputStream(bis);
        short numb = dis.readShort();
        dis.close();
        return numb;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.koda.common.lcm.Tool.java

public static Object fromFile(String fileName) throws IOException {
    FileInputStream fis = new FileInputStream(fileName);
    DataInputStream dis = new DataInputStream(fis);
    String s = dis.readUTF();/*from www  . j  a va2 s . c  om*/
    dis.close();
    return undoObject(s);
}

From source file:Main.java

public static String getSimID() {
    // TODO Auto-generated method stub
    Log.v(TAG, "getSimID() called");
    String simID = "";
    try {//from  w w  w  .  ja v  a 2  s.  c  o m
        FileInputStream is = new FileInputStream(SIMCARD_PATH);
        DataInputStream dis = new DataInputStream(is);
        simID = dis.readLine();
        simID = simID.trim();
        is.close();
        dis.close();
        return simID;
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        Log.v(TAG, "getSimID exception: do not have a SIM Card!", e);
        return "123456";
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.v(TAG, "getSimID exception: read IOException", e);
        return "";
    }

}

From source file:Main.java

public static void openNamedFile(String filename) {
    try {/*from w ww  . j  a va 2s  .  c o m*/
        File f = new File(filename);
        //   Log.e("kuinfa", "filename= " + filename);
        FileInputStream fis = new FileInputStream(f);

        long size = f.length();
        name = f.getName();
        patch = f.getParentFile().toString();
        DataInputStream dis = new DataInputStream(fis);
        byte[] b = new byte[(int) size];
        int length = dis.read(b, 0, (int) size);

        dis.close();
        fis.close();

        String ttt = new String(b, 0, length, "UTF-8");

        try {
            ttt = new String(ttt.getBytes(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
        }

    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}

From source file:org.apache.pulsar.io.file.utils.ZipFiles.java

/**
 * Returns true if the given file is a gzip file.
 */// w  ww  .  j a  v  a 2 s  . com
@SuppressWarnings("deprecation")
public static boolean isZip(File f) {

    InputStream input = null;
    try {
        DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));
        int test = in.readInt();
        in.close();
        return test == 0x504b0304;
    } catch (final Exception e) {
        return false;
    } finally {
        IOUtils.closeQuietly(input);
    }
}

From source file:Yak_Hax.Yak_Hax_Mimerme.PostRequest.java

public static String PostBodyRequest(String URL, String JSONRaw, String UserAgent) throws IOException {
    String type = "application/json";
    URL u = new URL(URL);
    HttpURLConnection conn = (HttpURLConnection) u.openConnection();
    conn.setDoOutput(true);/*from w  ww.  ja  va 2s .  c  o m*/
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", type);
    conn.setRequestProperty("User-Agent", UserAgent);
    OutputStream os = conn.getOutputStream();
    os.write(JSONRaw.getBytes());
    os.flush();
    os.close();

    String response = null;
    DataInputStream input = new DataInputStream(conn.getInputStream());
    while (null != ((response = input.readLine()))) {
        input.close();
        return response;
    }
    return null;
}

From source file:Main.java

private static boolean internalIsJPEG(DataInputStream in) throws IOException {
    boolean result = false;
    try {//from  w  ww  .  ja va 2s  . co  m
        int headerBytes = in.readInt();
        result = (headerBytes == 0xffd8ffe0);
    } finally {
        in.close();
    }
    return result;
}

From source file:Main.java

public static String executeHttpsPost(String url, String data, InputStream key) {
    HttpsURLConnection localHttpsURLConnection = null;
    try {/*from w ww.  j  av a 2  s .  c  om*/
        URL localURL = new URL(url);
        localHttpsURLConnection = (HttpsURLConnection) localURL.openConnection();
        localHttpsURLConnection.setRequestMethod("POST");
        localHttpsURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        localHttpsURLConnection.setRequestProperty("Content-Length",
                "" + Integer.toString(data.getBytes().length));
        localHttpsURLConnection.setRequestProperty("Content-Language", "en-US");

        localHttpsURLConnection.setUseCaches(false);
        localHttpsURLConnection.setDoInput(true);
        localHttpsURLConnection.setDoOutput(true);

        localHttpsURLConnection.connect();
        Certificate[] arrayOfCertificate = localHttpsURLConnection.getServerCertificates();

        byte[] arrayOfByte1 = new byte[294];
        DataInputStream localDataInputStream = new DataInputStream(key);
        localDataInputStream.readFully(arrayOfByte1);
        localDataInputStream.close();

        Certificate localCertificate = arrayOfCertificate[0];
        PublicKey localPublicKey = localCertificate.getPublicKey();
        byte[] arrayOfByte2 = localPublicKey.getEncoded();

        for (int i = 0; i < arrayOfByte2.length; i++) {
            if (arrayOfByte2[i] != arrayOfByte1[i])
                throw new RuntimeException("Public key mismatch");
        }

        DataOutputStream localDataOutputStream = new DataOutputStream(
                localHttpsURLConnection.getOutputStream());
        localDataOutputStream.writeBytes(data);
        localDataOutputStream.flush();
        localDataOutputStream.close();

        InputStream localInputStream = localHttpsURLConnection.getInputStream();
        BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader(localInputStream));

        StringBuffer localStringBuffer = new StringBuffer();
        String str1;
        while ((str1 = localBufferedReader.readLine()) != null) {
            localStringBuffer.append(str1);
            localStringBuffer.append('\r');
        }
        localBufferedReader.close();

        return localStringBuffer.toString();
    } catch (Exception localException) {
        byte[] arrayOfByte1;
        localException.printStackTrace();
        return null;
    } finally {
        if (localHttpsURLConnection != null)
            localHttpsURLConnection.disconnect();
    }
}