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

/**
 * Reads in model 83 points and returns a double array float containing the
 * coordinates x & y.//from w  w w  .j  av  a2  s . c  om
 */
public static float[][] readBinModel83Pt2DFloat(String dir, String fileName) {
    float[][] array2D = new float[83][2];
    float x;
    int i = 0;
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        while (true) {
            x = in.readFloat();
            array2D[i][0] = x;
            x = in.readFloat();
            array2D[i][1] = x;
            i++;
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return array2D;
}

From source file:FileUtil.java

public static byte[] readFileAsBytes(String path, Integer start, Integer length) {

    byte[] byteData = null;

    try {/*ww w . j  a  va2 s. c o m*/

        File file = new File(path);

        DataInputStream dis;
        dis = new DataInputStream(new FileInputStream(file));

        if (dis.available() > Integer.MAX_VALUE) {
            System.out.println("dis.available() > Integer.MAX_VALUE");
        }

        ByteArrayOutputStream os = new ByteArrayOutputStream(length);
        byte[] bytes = new byte[length];

        dis.skipBytes(start);
        int readBytes = dis.read(bytes, 0, length);
        os.write(bytes, 0, readBytes);

        byteData = os.toByteArray();

        dis.close();
        os.close();

    } catch (Exception e) {
        System.out.println(e);
    }

    return byteData;
}

From source file:genepi.db.DatabaseUpdater.java

public static String readFileAsString(String filename) throws java.io.IOException, URISyntaxException {

    InputStream is = new FileInputStream(filename);

    DataInputStream in = new DataInputStream(is);
    BufferedReader br = new BufferedReader(new InputStreamReader(in));
    String strLine;//from ww  w  .  ja va  2s  .  c  o m
    StringBuilder builder = new StringBuilder();
    while ((strLine = br.readLine()) != null) {
        // builder.append("\n");
        builder.append(strLine);
    }

    in.close();

    return builder.toString();
}

From source file:Main.java

public static String[] getUvTableNames() {
    ArrayList<String> Tokens = new ArrayList<String>();

    try {/*from   www . ja  v a 2 s. c o m*/
        // Open the file that is the first
        // command line parameter
        FileInputStream fstream = null;
        File f = new File("/sys/devices/system/cpu/cpufreq/vdd_table/vdd_levels");
        if (f.exists()) {
            fstream = new FileInputStream("/sys/devices/system/cpu/cpufreq/vdd_table/vdd_levels");
        } else {
            File ff = new File("/sys/devices/system/cpu/cpu0/cpufreq/UV_mV_table");
            if (ff.exists()) {
                fstream = new FileInputStream("/sys/devices/system/cpu/cpu0/cpufreq/UV_mV_table");
            }
        }
        // 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) {
            strLine = strLine.trim();

            if ((strLine.length() != 0)) {
                String[] names = strLine.replaceAll(":", "").split("\\s+");
                Tokens.add(names[0]);
            }

        }
        // Close the input stream
        in.close();
    } catch (Exception e) {// Catch exception if any
        System.err.println("Error: " + e.getMessage());
    }
    String[] names = new String[Tokens.size() - 1];
    names = Tokens.toArray(names);
    return names;
}

From source file:com.alphabetbloc.accessmrs.utilities.NetworkUtils.java

public static DataInputStream getOdkStream(HttpClient client, String url) throws Exception {

    // get prefs//from   www.  j av a 2  s . com
    HttpPost request = new HttpPost(url);
    request.setEntity(new OdkAuthEntity());
    HttpResponse response = client.execute(request);
    response.getStatusLine().getStatusCode();
    HttpEntity responseEntity = response.getEntity();

    DataInputStream zdis = new DataInputStream(new GZIPInputStream(responseEntity.getContent()));

    int status = zdis.readInt();
    if (status == HttpURLConnection.HTTP_UNAUTHORIZED) {
        zdis.close();
        throw new IOException("Access denied. Check your username and password.");
    } else if (status <= 0 || status >= HttpURLConnection.HTTP_BAD_REQUEST) {
        zdis.close();
        throw new IOException(App.getApp().getString(R.string.error_connection));
    } else {
        assert (status == HttpURLConnection.HTTP_OK); // success
        return zdis;
    }
}

From source file:Main.java

public static float[] readBinTextureArray(String dir, String fileName, int size) {
    float x;//from  w  ww  . j av a  2s.c o  m
    int i = 0;
    float[] tab = new float[size];
    File sdLien = Environment.getExternalStorageDirectory();
    File inFile = new File(sdLien + File.separator + dir + File.separator + fileName);
    Log.d(TAG, "path of file : " + inFile);
    if (!inFile.exists()) {
        throw new RuntimeException("File doesn't exist");
    }
    DataInputStream in = null;
    try {
        in = new DataInputStream(new FileInputStream(inFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    try {
        x = in.readFloat(); // convert here for OpenGl
        while (true) {
            tab[i] = x / 255.0f;
            i++;
            x = in.readFloat(); // convert here for OpenGl
        }
    } catch (EOFException e) {
        try {
            Log.d(TAG, "close");
            in.close();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try { //free ressources
                Log.d(TAG, "close");
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tab;
}

From source file:com.flexdesktop.connections.restfulConnection.java

public static ArrayList<ArrayList<String>> getRESTful(String RESTfull_URL, ArrayList<String> columnasTabla) {
    try {//  w  w  w  .java2  s .c om
        URL url;
        URLConnection urlConnection;
        DataInputStream readString;
        url = new URL(RESTfull_URL);
        urlConnection = url.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
        readString = new DataInputStream(urlConnection.getInputStream());
        String s;

        String getRequest = "";
        while ((s = readString.readLine()) != null) {
            getRequest += s;
        }
        readString.close();
        if (!"".equals(getRequest)) {
            return completeArray(getRequest, columnasTabla);
        }

    } catch (Exception ex) {
        Logger.getLogger(com.flexdesktop.connections.restfulConnection.class.getName()).log(Level.SEVERE, null,
                ex);
    }
    return null;
}

From source file:flexpos.restfulConnection.java

public static ArrayList<ArrayList<String>> getRESTful(String RESTfull_URL, ArrayList<String> columnasTabla) {
    try {//from  w  w  w.  j a  v  a 2s . c  o m
        URL url;
        URLConnection urlConnection;
        DataInputStream readString;
        url = new URL(RESTfull_URL);
        urlConnection = url.openConnection();
        urlConnection.setDoInput(true);
        urlConnection.setUseCaches(false);
        readString = new DataInputStream(urlConnection.getInputStream());
        String s;

        String getRequest = "";
        while ((s = readString.readLine()) != null) {
            getRequest += s;
        }
        readString.close();
        if (!"".equals(getRequest)) {
            return completeArray(getRequest, columnasTabla);
        }

    } catch (Exception ex) {
        Logger.getLogger(restfulConnection.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

From source file:it.infn.ct.InstantiateVM.java

public static String doCreate(Properties properties, EntityBuilder eb, Model model, Client client,
        JSONObject egiInput) {// ww w .  j a v a  2 s.  c o  m

    URI uri_location = null;
    String networkInterfaceLocation = "";
    String networkInterfaceLocation_stripped = "";
    Resource vm_resource = null;

    try {

        if (properties.getProperty("RESOURCE").equals("compute")) {

            String segments[] = properties.getProperty("OCCI_OS_TPL").split("#");
            String OCCI_OS_TPL = segments[segments.length - 1];

            String segments2[] = properties.getProperty("OCCI_RESOURCE_TPL").split("#");
            String OCCI_RESOURCE_TPL = segments2[segments2.length - 1];

            System.out.println("[+] Creating a new compute Virtual Machine (VM)");

            // Creating a compute instance
            Resource compute = eb.getResource("compute");
            Mixin mixin = model.findMixin(OCCI_OS_TPL);
            compute.addMixin(mixin);
            compute.addMixin(model.findMixin(OCCI_OS_TPL, "os_tpl"));
            compute.addMixin(model.findMixin(OCCI_RESOURCE_TPL, "resource_tpl"));

            // Checking the context
            if (properties.getProperty("PUBLIC_KEY_FILE") != null
                    && !properties.getProperty("PUBLIC_KEY_FILE").isEmpty()) {
                String _public_key_file = properties.getProperty("PUBLIC_KEY_FILE")
                        .substring(properties.getProperty("PUBLIC_KEY_FILE").lastIndexOf(":") + 1);

                File f = new File(_public_key_file);

                FileInputStream fis = new FileInputStream(f);
                DataInputStream dis = new DataInputStream(fis);
                byte[] keyBytes = new byte[(int) f.length()];
                dis.readFully(keyBytes);
                dis.close();
                String _publicKey = new String(keyBytes).trim();

                // Add SSH public key
                compute.addMixin(model
                        .findMixin(URI.create("http://schemas.openstack.org/instance/credentials#public_key")));
                compute.addAttribute("org.openstack.credentials.publickey.data", _publicKey);

                // Add the name for the public key   
                if (OCCI_PUBLICKEY_NAME != null && !OCCI_PUBLICKEY_NAME.isEmpty())
                    compute.addAttribute("org.openstack.credentials.publickey.name",
                            properties.getProperty("OCCI_PUBLICKEY_NAME"));
            }

            if (properties.getProperty("USER_DATA") != null && !properties.getProperty("USER_DATA").isEmpty()) {
                String _user_data = properties.getProperty("USER_DATA")
                        .substring(properties.getProperty("USER_DATA").lastIndexOf(":") + 1);

                File f = new File(_user_data);
                FileInputStream fis = new FileInputStream(f);
                DataInputStream dis = new DataInputStream(fis);
                byte[] keyBytes = new byte[(int) f.length()];
                dis.readFully(keyBytes);
                dis.close();
                byte[] data = Base64.encodeBase64(keyBytes);
                String user_data = new String(data);

                compute.addMixin(
                        model.findMixin(URI.create("http://schemas.openstack.org/compute/instance#user_data")));

                compute.addAttribute("org.openstack.compute.user_data", user_data);
            }

            // Set VM title
            compute.setTitle(properties.getProperty("OCCI_CORE_TITLE"));
            URI location = client.create(compute);

            return location.toString();

        }

        if (properties.getProperty("RESOURCE").equals("storage")) {
            System.out.println("[+] Creating a volume storage");

            // Creating a storage instance
            Storage storage = eb.getStorage();
            storage.setTitle(properties.getProperty("OCCI_CORE_TITLE"));
            storage.setSize(properties.getProperty("OCCI_STORAGE_SIZE"));

            URI storageLocation = client.create(storage);

            List<URI> list = client.list("storage");
            List<URI> storageURIs = new ArrayList<URI>();

            for (URI uri : list) {
                if (uri.toString().contains("storage"))
                    storageURIs.add(uri);
            }

            System.out.println("URI = " + storageLocation);
        }

    }

    catch (FileNotFoundException ex) {
        throw new RuntimeException(ex);
    }

    catch (IOException ex) {
        throw new RuntimeException(ex);
    }

    catch (EntityBuildingException | AmbiguousIdentifierException | InvalidAttributeValueException
            | CommunicationException ex) {
        throw new RuntimeException(ex);
    }

    return "";
}

From source file:com.cedarsoft.crypt.X509Support.java

/**
 * Reads the x509 certificate from the given url
 *
 * @param certificateUrl the certificate url
 * @return the certificate/*w w w . ja v a2s.  c  o  m*/
 *
 * @throws IOException if any.
 * @throws GeneralSecurityException
 *                             if any.
 */
@Nonnull
public static X509Certificate readCertificate(@Nonnull URL certificateUrl)
        throws IOException, GeneralSecurityException {
    //Read the cert
    DataInputStream in = new DataInputStream(certificateUrl.openStream());
    try {
        CertificateFactory cf = CertificateFactory.getInstance(X_509_CERTIFICATE_TYPE);
        X509Certificate certificate = (X509Certificate) cf.generateCertificate(in);
        certificate.checkValidity();
        return certificate;
    } finally {
        in.close();
    }
}