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

static String validGet(URL url) throws IOException {
    String html = "";
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.STRICT_HOSTNAME_VERIFIER;
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    // TODO ensure HttpsURLConnection.setDefaultSSLSocketFactory isn't
    // required to be reset like hostnames are
    HttpURLConnection conn = null;
    try {//from   w  w  w . j a va  2 s .c om
        conn = (HttpURLConnection) url.openConnection();
        conn.setUseCaches(false);
        conn.setRequestMethod("GET");
        setBasicAuthentication(conn, url);
        int status = conn.getResponseCode();
        if (status != 200) {
            Logd(TAG, "Failed to get from server, returned code: " + status);
            throw new IOException("Get failed with error code " + status);
        } else {
            InputStreamReader in = new InputStreamReader(conn.getInputStream());
            BufferedReader br = new BufferedReader(in);
            String decodedString;
            while ((decodedString = br.readLine()) != null) {
                html += decodedString;
            }
            in.close();
        }
    } catch (IOException e) {
        Logd(TAG, "Failed to get from server: " + e.getMessage());
    }
    if (conn != null) {
        conn.disconnect();
    }
    return html;
}

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

private static byte[] makeRequest(Context context, MmsConnectionParameters connectionParameters, String url)
        throws IOException {
    AndroidHttpClient client = null;//from w  w  w. ja v  a2  s .c  o  m

    try {
        client = constructHttpClient(context, connectionParameters);
        URI targetUrl = new URI(url.trim());
        HttpHost target = new HttpHost(targetUrl.getHost(), targetUrl.getPort(), HttpHost.DEFAULT_SCHEME_NAME);
        HttpGet request = new HttpGet(url.trim());

        request.setParams(client.getParams());
        request.addHeader("Accept", "*/*, application/vnd.wap.mms-message, application/vnd.wap.sic");

        HttpResponse response = client.execute(target, request);
        StatusLine status = response.getStatusLine();

        if (status.getStatusCode() != 200)
            throw new IOException("Non-successful HTTP response: " + status.getReasonPhrase());

        return parseResponse(response.getEntity());
    } catch (URISyntaxException use) {
        Log.w("MmsDownloadHelper", use);
        throw new IOException("Couldn't parse URI");
    } finally {
        if (client != null)
            client.close();
    }
}

From source file:mvm.rya.indexing.accumulo.StatementSerializer.java

/**
 * Read a {@link Statement} from a {@link String}
 * //from w  ww  .  j a  va2  s.c  om
 * @param in
 *            the {@link String} to parse
 * @return a {@link Statement}
 */
public static Statement readStatement(String in) throws IOException {
    String[] parts = in.split(SEP);

    if (parts.length != 4) {
        throw new IOException("Not a valid statement: " + in);
    }

    String contextString = parts[0];
    String subjectString = parts[1];
    String predicateString = parts[2];
    String objectString = parts[3];
    return readStatement(subjectString, predicateString, objectString, contextString);
}

From source file:com.picdrop.guice.provider.implementation.FileItemFactoryProviderImpl.java

protected void init() throws IOException {
    if (this.fac == null) {
        if (!root.exists() || !root.canWrite() || !root.isDirectory()) {
            throw new IOException(String.format("'%s' is not a valid directory", this.root.getAbsolutePath()));
        }/* ww  w .  j  a  va2 s  .  c  o  m*/
        DiskFileItemFactory dfac = new DiskFileItemFactory(maxmem, root);
        fac = dfac;
    }
}

From source file:com.android.dialer.lookup.LookupUtils.java

public static String httpGet(HttpGet request) throws IOException {
    HttpClient client = new DefaultHttpClient();

    request.setHeader("User-Agent", USER_AGENT);

    HttpResponse response = client.execute(request);
    int status = response.getStatusLine().getStatusCode();

    // Android's org.apache.http doesn't have the RedirectStrategy class
    if (status == HttpStatus.SC_MOVED_PERMANENTLY || status == HttpStatus.SC_MOVED_TEMPORARILY) {
        Header[] headers = response.getHeaders("Location");

        if (headers != null && headers.length != 0) {
            HttpGet newGet = new HttpGet(headers[headers.length - 1].getValue());
            for (Header header : request.getAllHeaders()) {
                newGet.addHeader(header);
            }/*  w  ww .java2  s  . c om*/
            return httpGet(newGet);
        } else {
            throw new IOException("Empty redirection header");
        }
    }

    if (status != HttpStatus.SC_OK) {
        throw new IOException("HTTP failure (status " + status + ")");
    }

    return EntityUtils.toString(response.getEntity());
}

From source file:fi.hsl.parkandride.front.geojson.GeojsonSerializer.java

@Override
public void serialize(T value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
    try {/*from  w  w w  .j  a  v a 2  s. co  m*/
        //            jgen.writeRaw(':');
        jgen.writeRawValue(jsonMapper.toJson(value));
    } catch (JsonException e) {
        throw new IOException(e);
    }
}

From source file:Main.java

/**
 * Grab the Document found at the specified URL.
 * //from   w w w . j a  v  a  2 s .c o m
 * @param protocol
 * @param is
 * @return a Document loaded from the specified URL.
 * @throws SAXException
 * @throws IOException
 * @throws ParserConfigurationException
 */
public static Document parse(final String protocol, final InputSource is)
        throws SAXException, IOException, ParserConfigurationException {
    if ("http".equals(protocol)) {
        return getThreadedDocumentBuilder().parse(is);
    }
    throw new IOException("Protocol handler not implemented yet: " + protocol);
}

From source file:net.darkmist.alib.io.Slurp.java

public static byte[] slurp(InputStream stream, long amount) throws IOException {
    if (amount > Integer.MAX_VALUE)
        throw new IOException("Size larger than max int");
    return slurp(stream, (int) amount);
}

From source file:UrlHelper.java

public static void downloadFile(String adresse, File dest) {
    BufferedReader reader = null;
    FileOutputStream fos = null;/*w  w w. j  a  va  2  s. com*/
    InputStream in = null;
    try {

        // cration de la connection
        URL url = new URL(adresse);
        URLConnection conn = url.openConnection();
        String FileType = conn.getContentType();

        int FileLenght = conn.getContentLength();
        if (FileLenght == -1) {
            throw new IOException("Fichier non valide.");
        }

        // lecture de la rponse
        in = conn.getInputStream();
        reader = new BufferedReader(new InputStreamReader(in));
        if (dest == null) {
            String FileName = url.getFile();
            FileName = FileName.substring(FileName.lastIndexOf('/') + 1);
            dest = new File(FileName);
        }
        fos = new FileOutputStream(dest);
        byte[] buff = new byte[1024];
        int l = in.read(buff);
        while (l > 0) {
            fos.write(buff, 0, l);
            l = in.read(buff);
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            fos.flush();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            reader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

From source file:com.codercowboy.photo.organizer.service.PhotoIndexer.java

public static Photo indexFile(File f) throws Exception {
    if (!f.exists()) {
        throw new IOException("Cannot index file, file does not exist: " + f.getAbsolutePath());
    }//w ww . j  av a2s.  co  m
    if (!f.canRead()) {
        throw new IOException(
                "Cannot index file, file is not readable, permission denied:" + f.getAbsolutePath());
    }

    Photo p = new Photo();
    p.setName(f.getName());
    //TODO: this should be relative path to parent
    p.setFileRelativePath(f.getAbsolutePath());
    p.setFileSize(f.length());
    FileInputStream fis = new FileInputStream(f);
    try {
        p.setMd5Checksum(DigestUtils.md5Hex(fis));
    } finally {
        fis.close();
    }
    return p;
}