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 valiFileCanWrite(File file) throws IOException {
    if (!file.canWrite()) {
        throw new IOException("For file '" + file.getName() + "' not write access!");
    }//from  w ww  .j a  v a 2 s  .  com
}

From source file:Main.java

public static void deepCopyDocument(Document ttml, File target) throws IOException {
    try {/*from  www .ja v  a  2s . com*/
        XPathFactory xPathfactory = XPathFactory.newInstance();
        XPath xpath = xPathfactory.newXPath();
        XPathExpression expr = xpath.compile("//*/@backgroundImage");
        NodeList nl = (NodeList) expr.evaluate(ttml, XPathConstants.NODESET);
        for (int i = 0; i < nl.getLength(); i++) {
            Node backgroundImage = nl.item(i);
            URI backgroundImageUri = URI.create(backgroundImage.getNodeValue());
            if (!backgroundImageUri.isAbsolute()) {
                copyLarge(new URI(ttml.getDocumentURI()).resolve(backgroundImageUri).toURL().openStream(),
                        new File(target.toURI().resolve(backgroundImageUri).toURL().getFile()));
            }
        }
        copyLarge(new URI(ttml.getDocumentURI()).toURL().openStream(), target);

    } catch (XPathExpressionException e) {
        throw new IOException(e);
    } catch (URISyntaxException e) {
        throw new IOException(e);
    }
}

From source file:com.diaw.lib.tool.FakeSocketFactory.java

private static SSLContext createEasySSLContext() throws IOException {
    try {//from ww w  .j  a v a 2 s  . co m
        final SSLContext context = SSLContext.getInstance("TLS");
        context.init(null, new TrustManager[] { new NaiveTrustManager() }, null);
        return context;
    } catch (GeneralSecurityException e) {
        throw new IOException(e.getMessage());
    }
}

From source file:Main.java

/**
 * @see net.yura.domination.mapstore.MapChooser#createImage(java.io.InputStream)
 *//*from  w w  w.  ja  v  a  2s .  c  o m*/
public static BufferedImage read(InputStream in) throws IOException {
    try {
        BufferedImage img = ImageIO.read(in);
        if (img == null) {
            throw new IOException("ImageIO.read returned null");
        }
        return img;
    } finally {
        try {
            in.close();
        } catch (Throwable th) {
        }
    }
}

From source file:Main.java

protected static String getResponseAsString(HttpURLConnection conn, boolean responseError) throws IOException {
    String charset = getResponseCharset(conn.getContentType());
    String header = conn.getHeaderField("Content-Encoding");
    boolean isGzip = false;
    if (header != null && header.toLowerCase().contains("gzip")) {
        isGzip = true;//from  ww  w.  j  ava  2  s .  co  m
    }
    InputStream es = conn.getErrorStream();
    if (es == null) {
        InputStream input = conn.getInputStream();
        if (isGzip) {
            input = new GZIPInputStream(input);
        }
        return getStreamAsString(input, charset);
    } else {
        if (isGzip) {
            es = new GZIPInputStream(es);
        }
        String msg = getStreamAsString(es, charset);
        if (TextUtils.isEmpty(msg)) {
            throw new IOException(conn.getResponseCode() + ":" + conn.getResponseMessage());
        } else if (responseError) {
            return msg;
        } else {
            throw new IOException(msg);
        }
    }
}

From source file:Main.java

/**
 * Generate an input stream reading from an android URI
 * /*from  ww w . j a  v a 2s.  c  o m*/
 * @param uri
 * @return
 * @throws IOException
 */
public static InputStream getFromURI(Context context, Uri uri) throws IOException {

    if (uri.getScheme().equals("content"))
        return context.getContentResolver().openInputStream(uri);
    else if (uri.getScheme().equals("file")) {
        URL url = new URL(uri.toString());

        return url.openStream();
    } else {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(uri.toString());
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null)
            return entity.getContent();
        else
            throw new IOException("No HTTP response");
        // Use the regular java stuff
        // URL url = new URL(uri.toString());

        // return url.openStream();
    }
}

From source file:Main.java

public static void checkForBlacklistedURLs(URL url)

        throws IOException {
    for (int i = 0; i < BLACKLISTED_HOSTS.length; i++) {

        if (url.getHost().equalsIgnoreCase(BLACKLISTED_HOSTS[i]) && url.getPort() == BLACKLISTED_PORTS[i]) {

            throw (new IOException(
                    "http://" + BLACKLISTED_HOSTS[i] + ":" + BLACKLISTED_PORTS[i] + "/ is not a tracker"));
        }//from  ww  w.  ja  v  a2s  .  c o  m
    }
}

From source file:cfa.vo.interop.EncodeDoubleArray.java

private static double[] byteToDouble(byte[] data) throws IOException {

    int len = data.length;

    if (len % WORDSIZE != 0) {
        throw new IOException("Array length is not divisible by wordsize");
    }/*from   w  ww  .j a v a  2  s.  c o  m*/

    int size = len / WORDSIZE;
    double[] result = new double[size];

    DataInputStream inputStream = new DataInputStream(new ByteArrayInputStream(data));

    try {

        int ii = 0;
        while (inputStream.available() > 0) {
            result[ii] = inputStream.readDouble();
            ii++;
        }

    } catch (EOFException e) {
        throw new IOException("Unable to read from dataInputStream, found EOF");

    } catch (IOException e) {
        throw new IOException("Unable to read from dataInputStream, IO error");
    }

    return result;

}

From source file:Main.java

/**
 * Reads the in_stream and extracts them to out_dir.
 * @param in_stream Input stream corresponding to the zip file.
 * @param out_dir Output directory for the zip file contents.
 * @throws IOException/*from   w  w  w  . jav a  2s.  co  m*/
 */
public static void zipExtract(InputStream in_stream, File out_dir) throws IOException {
    if (!out_dir.exists()) {
        if (!out_dir.mkdirs()) {
            throw new IOException("Could not create output directory: " + out_dir.getAbsolutePath());
        }
    }
    ZipInputStream zis = new ZipInputStream(in_stream);
    ZipEntry ze;
    byte[] buffer = new byte[BUFFER_SIZE];
    int count;
    while ((ze = zis.getNextEntry()) != null) {
        if (ze.isDirectory()) {
            File fmd = new File(out_dir.getAbsolutePath() + "/" + ze.getName());
            fmd.mkdirs();
            continue;
        }
        FileOutputStream fout = new FileOutputStream(out_dir.getAbsolutePath() + "/" + ze.getName());
        while ((count = zis.read(buffer)) != -1) {
            fout.write(buffer, 0, count);
        }

        fout.close();
        zis.closeEntry();
    }
    zis.close();
}

From source file:net.sf.dsig.helpers.UserHomeSettingsParser.java

public static Properties parse() {
    try {/*from  w  ww.j  a v  a 2 s .c o m*/
        String userHome = System.getProperty("user.home");
        File dsigFolder = new File(userHome, ".dsig");
        if (!dsigFolder.exists() && !dsigFolder.mkdir()) {
            throw new IOException("Could not create .dsig folder in user home directory");
        }

        File settingsFile = new File(dsigFolder, "settings.properties");
        if (!settingsFile.exists()) {
            InputStream is = UserHomeSettingsParser.class.getResourceAsStream("/defaultSettings.properties");
            if (is != null) {
                IOUtils.copy(is, new FileOutputStream(settingsFile));
            }
        }

        if (settingsFile.exists()) {
            Properties p = new Properties();
            FileInputStream fis = new FileInputStream(settingsFile);
            p.load(fis);
            IOUtils.closeQuietly(fis);
            return p;
        }
    } catch (IOException e) {
        logger.warn("Error while initialize settings", e);
    }

    return null;
}