List of usage examples for java.io IOException IOException
public IOException(Throwable cause)
From source file:Main.java
/** * This will parse an InputSource and create a DOM document. * //from w ww . jav a2 s . c om * @param is * The stream to get the XML from. * @return The DOM document. * @throws IOException * It there is an error creating the dom. * @throws ParseException */ public static Document parse(InputSource is) throws IOException, ParseException { final DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = null; try { builder = builderFactory.newDocumentBuilder(); } catch (final ParserConfigurationException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } try { return builder.parse(is); } catch (final IOException e) { final IOException thrown = new IOException(e.getMessage()); throw thrown; } catch (final SAXException e) { final ParseException thrown = new ParseException(e.getMessage(), 0); throw thrown; } }
From source file:Main.java
public static String readElementValue(String elementName, String s) throws IOException { int indexOfElementStart = s.indexOf("<" + elementName); if (indexOfElementStart == -1) { throw new IOException("Couldn't find element '" + elementName + "'!"); }/*w w w .j ava 2 s . c o m*/ int indexOfValueStart = s.indexOf(">", indexOfElementStart) + 1; if (indexOfElementStart == -1) { throw new IOException("Couldn't find element '" + elementName + "'! (couldn't find end of start tag)"); } int indexOfValueEnd = s.indexOf("<", indexOfValueStart); if (indexOfElementStart == -1) { throw new IOException("Couldn't find element '" + elementName + "'! (couldn't find end tag)"); } return s.substring(indexOfValueStart, indexOfValueEnd); }
From source file:org.radiognu.radiognu.utils.utilsradiognu.java
public static final String getResponseServiceAPI(String... uri) { HttpClient httpclient = new DefaultHttpClient(); HttpResponse response;// w w w .j ava 2 s . co m String responseString = null; try { // make a HTTP request response = httpclient.execute(new HttpGet(uri[0])); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpStatus.SC_OK) { // request successful - read the response and close the connection ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); responseString = out.toString(); } else { // request failed - close the connection response.getEntity().getContent().close(); throw new IOException(statusLine.getReasonPhrase()); } } catch (Exception e) { } return responseString; }
From source file:Main.java
/** * Creates an output stream to write to a regular or compressed file. * /*from w ww . j a v a2 s . co m*/ * @param fileName a file name with an extension .gz or without it; * if the user specifies an extension .gz, we assume * that the output file should be compressed. * @return an output stream to write to a file. * @throws IOException */ public static OutputStream createOutputStream(String fileName) throws IOException { OutputStream foutp = new FileOutputStream(fileName); if (fileName.endsWith(".gz")) return new GZIPOutputStream(foutp); if (fileName.endsWith(".bz2")) { throw new IOException("bz2 is not supported for writing"); } return foutp; }
From source file:com.amazonaws.services.iot.client.shadow.AwsIotJsonDeserializer.java
public static void deserialize(AbstractAwsIotDevice device, String jsonState) throws IOException { ObjectMapper jsonObjectMapper = device.getJsonObjectMapper(); JsonNode node = jsonObjectMapper.readTree(jsonState); if (node == null) { throw new IOException("Invalid delta update received for " + device.getThingName()); }/*from w w w . j a v a 2s. com*/ for (Iterator<String> it = node.fieldNames(); it.hasNext();) { String property = it.next(); Field field = device.getUpdatableProperties().get(property); JsonNode fieldNode = node.get(property); if (field == null || fieldNode == null) { continue; } updateDeviceProperty(jsonObjectMapper, fieldNode, device, field); } }
From source file:it.polimi.brusamentoceruti.moviebookrest.boundary.JsonRequest.java
public static JSONObject doQuery(String Url) throws JSONException, IOException { String responseBody = null;//w w w.j av a 2 s .c om HttpGet httpget; HttpClient httpClient = new DefaultHttpClient(); try { httpget = new HttpGet(Url); } catch (IllegalArgumentException iae) { return null; } HttpResponse response = httpClient.execute(httpget); InputStream contentStream = null; try { StatusLine statusLine = response.getStatusLine(); if (statusLine == null) { throw new IOException(String.format("Unable to get a response from server")); } int statusCode = statusLine.getStatusCode(); if (statusCode < 200 && statusCode >= 300) { throw new IOException( String.format("OWM server responded with status code %d: %s", statusCode, statusLine)); } /* Read the response content */ HttpEntity responseEntity = response.getEntity(); contentStream = responseEntity.getContent(); Reader isReader = new InputStreamReader(contentStream); int contentSize = (int) responseEntity.getContentLength(); if (contentSize < 0) contentSize = 8 * 1024; StringWriter strWriter = new StringWriter(contentSize); char[] buffer = new char[8 * 1024]; int n = 0; while ((n = isReader.read(buffer)) != -1) { strWriter.write(buffer, 0, n); } responseBody = strWriter.toString(); contentStream.close(); } catch (IOException e) { throw e; } catch (RuntimeException re) { httpget.abort(); throw re; } finally { if (contentStream != null) contentStream.close(); } return new JSONObject(responseBody); }
From source file:com.github.parisoft.resty.utils.JacksonUtils.java
public static <T> T read(HttpEntity entity, Class<T> someClass, MediaType contentType) throws IOException { for (ProviderBase<?, ?, ?, ?> provider : DataProcessors.getInstance().values()) { if (provider.isReadable(someClass, null, null, contentType)) { return provider.locateMapper(someClass, contentType).readValue(entity.getContent(), someClass); }//from w ww . jav a 2 s . co m } throw new IOException(String.format("no processors found for class=%s and Content-Type=%s", someClass.getName(), contentType)); }
From source file:Utils.java
/** * Unpack a zip file//from w ww . j a v a 2 s.co m * * @param theFile * @param targetDir * @return the file * @throws IOException */ public static File unpackArchive(File theFile, File targetDir) throws IOException { if (!theFile.exists()) { throw new IOException(theFile.getAbsolutePath() + " does not exist"); } if (!buildDirectory(targetDir)) { throw new IOException("Could not create directory: " + targetDir); } ZipFile zipFile = new ZipFile(theFile); for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) { ZipEntry entry = (ZipEntry) entries.nextElement(); File file = new File(targetDir, File.separator + entry.getName()); if (!buildDirectory(file.getParentFile())) { throw new IOException("Could not create directory: " + file.getParentFile()); } if (!entry.isDirectory()) { copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(file))); } else { if (!buildDirectory(file)) { throw new IOException("Could not create directory: " + file); } } } zipFile.close(); return theFile; }
From source file:com.yata.core.FileManager.java
public static boolean contentEquals(String actualDataFileName, String expectedDataFileName, String encoding) throws IOException { File actualDataFile = new File(actualDataFileName); File expectedDataFile = new File(expectedDataFileName); if (!actualDataFile.exists()) { System.out.println("contentEquals@" + className + " : ActualDataFile :-> " + actualDataFileName + " : doesn't Exist..."); throw new IOException("contentEquals@" + className + " : ActualDataFile :-> " + actualDataFileName + " : doesn't Exist..."); } else if (!expectedDataFile.exists()) { System.out.println("contentEquals@" + className + " : ExpectedDataFile :-> " + expectedDataFileName + " : doesn't Exist..."); throw new IOException("contentEquals@" + className + " : ExpectedDataFile :-> " + expectedDataFileName + " : doesn't Exist..."); }/* ww w . j ava 2s . c o m*/ boolean contentMatched; try { contentMatched = FileUtils.contentEqualsIgnoreEOL(actualDataFile, expectedDataFile, encoding); } catch (IOException e) { e.printStackTrace(); throw e; } return contentMatched; }
From source file:de.flapdoodle.embedmongo.Files.java
public static File createTempFile(String tempFileName) throws IOException { File tempDir = new File(System.getProperty("java.io.tmpdir")); File tempFile = new File(tempDir, tempFileName); if (!tempFile.createNewFile()) throw new IOException("Could not create Tempfile: " + tempFile); return tempFile; }