Example usage for java.io IOException IOException

List of usage examples for java.io IOException IOException

Introduction

In this page you can find the example usage for java.io IOException IOException.

Prototype

public IOException(Throwable cause) 

Source Link

Document

Constructs an IOException with the specified cause and a detail message of (cause==null ?

Usage

From source file:Main.java

public static void saveBitmap(Bitmap btm, final String destDir, final String name) {
    File dir = new File(destDir);
    File file = new File(destDir + "/" + name + ".png");

    FileOutputStream out = null;/*from  w w  w. ja v a  2 s  .  c o m*/
    try {
        if (!dir.mkdirs() && (!dir.exists() || !dir.isDirectory())) {
            throw new IOException("Cannot ensure parent directory for file " + file);
        }

        out = new FileOutputStream(file);
        btm.compress(Bitmap.CompressFormat.PNG, 100, out);
        // PNG is a lossless format, the compression factor (100) is ignored
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

static public String downloadFile(String downloadUrl, String fileName, String dir) throws IOException {
    URL url = new URL(downloadUrl);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setRequestMethod("GET");
    urlConnection.setDoOutput(true);/*from w w w  . j av  a 2 s  .c o  m*/
    urlConnection.connect();
    int code = urlConnection.getResponseCode();
    if (code > 300 || code == -1) {
        throw new IOException("Cannot read url: " + downloadUrl);
    }
    String filePath = prepareFilePath(fileName, dir);
    String tmpFilePath = prepareTmpFilePath(fileName, dir);
    FileOutputStream fileOutput = new FileOutputStream(tmpFilePath);
    BufferedInputStream inputStream = new BufferedInputStream(urlConnection.getInputStream());
    byte[] buffer = new byte[1024];
    int bufferLength;
    while ((bufferLength = inputStream.read(buffer)) > 0) {
        fileOutput.write(buffer, 0, bufferLength);
    }
    fileOutput.close();
    // move tmp to destination
    copyFile(new File(tmpFilePath), new File(filePath));
    return filePath;
}

From source file:Main.java

/**
 * Create a new blank XML document./*  w w w .ja  v a 2  s  .c  o  m*/
 *
 * @return The new blank XML document.
 *
 * @throws IOException If there is an error creating the XML document.
 */
public static Document newDocument() throws IOException {
    try {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        return builder.newDocument();
    } catch (Exception e) {
        IOException thrown = new IOException(e.getMessage());
        throw thrown;
    }
}

From source file:Main.java

public static void deleteDirectory(File directory) throws IOException {
    if (!directory.exists()) {
        return;//from  www . j a  v a  2 s  .  com
    }

    if (!isSymlink(directory)) {
        cleanDirectory(directory);
    }

    if (!directory.delete()) {
        String message = "Unable to delete directory " + directory + ".";

        throw new IOException(message);
    }
}

From source file:Main.java

public static void stringToResult(String source, Result result) throws IOException {

    try {// ww  w . ja v  a  2 s. c o  m
        Transformer trans = transFactory.newTransformer();
        StringReader reader = new StringReader(source);
        trans.transform(new StreamSource(reader), result);
    } catch (TransformerException ex) {
        throw new IOException(ex);
    }
}

From source file:Main.java

/**
 * Returns only one line from the server's response. This method should be
 * used if the server returns only a single line of String.
 *
 * @return a String of the server's response
 * @throws IOException//w w  w .jav  a  2  s. c om
 *             thrown if any I/O error occurred
 */
public static String readSingleLineRespone() throws IOException {
    InputStream inputStream = null;
    if (httpConn != null) {
        inputStream = httpConn.getInputStream();
    } else {
        throw new IOException("Connection is not established.");
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

    String response = reader.readLine();
    reader.close();

    return response;
}

From source file:Main.java

/**
 * Converts a DER-encoded Oid from an InputStream to a dot-separated
 * String representation.//from  w w w . j a  v a 2s .  c  o m
 *
 * @param oid DER-encoded Oid InputStream
 * @return Oid in dot-separated String representation
 *
 */
public static byte[] OidStream2DER(InputStream oid) throws IOException {

    int tag;
    int length;
    ByteArrayOutputStream outOid = new ByteArrayOutputStream();

    try {
        tag = oid.read();
        length = oid.read();
        outOid.write(tag);
        outOid.write(length);

        for (int i = 0; i < length; i++) {
            outOid.write(oid.read());
        }
    } catch (IOException e) {
        throw new IOException("I/O Error occurred when reading InputStream");
    }

    return outOid.toByteArray();
}

From source file:Main.java

public static byte[] decompressZLIB(byte[] input) throws IOException {
    Inflater decompressor = new Inflater();
    decompressor.setInput(input);/*from   ww w  .j  a  v a 2s .  c o  m*/
    // Create an expandable byte array to hold the decompressed data
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);

    // Decompress the data
    byte[] buf = new byte[1024];
    while (!decompressor.finished()) {
        try {
            int count = decompressor.inflate(buf);
            bos.write(buf, 0, count);
        } catch (DataFormatException e) {
            throw new IOException(e.toString());
        }
    }
    bos.close();

    // Get the decompressed data
    byte[] decompressedData = bos.toByteArray();
    return decompressedData;
}

From source file:Main.java

/**
 * Save a DOM document/*www. j  a v  a 2  s .  c  o m*/
 */
public static void saveDocument(Document document, OutputStream out, boolean indent) throws IOException {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        if (indent) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        }
        transformer.transform(new DOMSource(document), new StreamResult(out));
    } catch (TransformerException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

public static byte[] decompress(byte[] data) throws IOException {
    Inflater inflater = new Inflater();
    inflater.setInput(data);/*ww w.  j  a v  a2s . c  o m*/
    inflater.finished();

    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(data.length);
    try {
        byte[] buffer = new byte[1024];
        while (!inflater.finished()) {
            int count = inflater.inflate(buffer);
            outputStream.write(buffer, 0, count);
        }
        outputStream.close();
    } catch (DataFormatException ex) {
        throw new IOException(ex);
    }

    byte[] output = outputStream.toByteArray();
    inflater.end();
    return output;
}