List of usage examples for java.io IOException IOException
public IOException(String message, Throwable cause)
From source file:Main.java
public static Element loadXml(final InputStream inStream) throws IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false);/*from w ww . ja va2 s .c o m*/ Document doc; try { doc = factory.newDocumentBuilder().parse(inStream); } catch (SAXException e) { throw new IOException(e.getLocalizedMessage(), e); } catch (ParserConfigurationException e) { throw new IOException(e.getLocalizedMessage(), e); } return doc.getDocumentElement(); }
From source file:cn.dreampie.FileUtilities.java
@SuppressWarnings("unchecked") public static List<File> getFilesFromFileSet(FileSet fileSet) throws IOException { List<File> files = Lists.newArrayList(); try {//from www . j a va 2 s .c om File directory = new File(fileSet.getDirectory()); String includes = getCommaSeparatedList(fileSet.getIncludes()); String excludes = getCommaSeparatedList(fileSet.getExcludes()); files = FileUtils.getFiles(directory, includes, excludes); } catch (IOException e) { throw new IOException("Unable to access files in fileSet", e); } return files; }
From source file:com.trin.nilmobile.serializer.ObjectSerializer.java
public static String serialize(Serializable obj) throws IOException { if (obj == null) return ""; try {//ww w. j ava 2 s .c o m ByteArrayOutputStream serialObj = new ByteArrayOutputStream(); ObjectOutputStream objStream = new ObjectOutputStream(serialObj); objStream.writeObject(obj); objStream.close(); return encodeBytes(serialObj.toByteArray()); } catch (Exception e) { throw new IOException("Serialization error: " + e.getMessage(), e); } }
From source file:Main.java
/** * Load the given {@code file} as an XML document. * * @param file/*from ww w . ja v a 2 s.c om*/ * @return the loaded XML document * @throws ParserConfigurationException * @throws IOException * @throws SAXException */ @Nonnull public static Document loadXmlDocumentFromFile(@Nonnull File file) throws ParserConfigurationException, IOException, SAXException { Preconditions.checkArgument(file.exists(), "File not found %s", file); Preconditions.checkArgument(!file.isDirectory(), "Expected file and not directory: %s", file); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); try { return documentBuilder.parse(file); } catch (IOException | SAXException e) { throw new IOException("Exception loading XML document " + file, e); } }
From source file:net.oneandone.sushi.fs.webdav.MultiStatus.java
public static List<MultiStatus> fromResponse(Xml xml, HttpResponse response) throws IOException { Builder builder;//from w w w .jav a 2 s . co m InputStream in; Element root; List<MultiStatus> result; ChildElements iter; in = response.getEntity().getContent(); try { builder = xml.getBuilder(); synchronized (builder) { root = builder.parse(in).getDocumentElement(); } } catch (SAXException e) { throw new IOException(e.getMessage(), e); } finally { in.close(); } Dom.require(root, "multistatus", Method.DAV); result = new ArrayList<MultiStatus>(); iter = Method.DAV.childElements(root, Method.XML_RESPONSE); while (iter.hasNext()) { fromXml(iter.next(), result); } return result; }
From source file:com.trusolve.ant.filters.DereferenceJSONFilter.java
public static Reader documentReader(Reader in) throws IOException { String doc;/*from w w w .j a v a2s .c o m*/ try { doc = JsonDereferencer.dereferenceToString(in); } catch (Exception e) { throw new IOException("Problem reading JSON document", e); } return new StringReader(doc); }
From source file:Main.java
/** * Load the given {@code path} as an XML document. * * @param path/*from w w w . j av a2 s . com*/ * @return the loaded XML document * @throws ParserConfigurationException * @throws IOException * @throws SAXException */ @Nonnull public static Document loadXmlDocumentFromPath(@Nonnull Path path) throws ParserConfigurationException, IOException, SAXException { Preconditions.checkArgument(Files.exists(path), "File not found %s", path); Preconditions.checkArgument(!Files.isDirectory(path), "Expected file and not directory: %s", path); DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); try { return documentBuilder.parse(Files.newInputStream(path)); } catch (IOException | SAXException e) { throw new IOException("Exception loading XML document " + path, e); } }
From source file:io.flutter.server.vmService.VmOpenSourceLocationListener.java
/** * Connect to the VM observatory service via the specified URI * * @return an API object for interacting with the VM service (not {@code null}). *///w w w . j a va 2 s . c om public static VmOpenSourceLocationListener connect(@NotNull final String url) throws IOException { // Validate URL final URI uri; try { uri = new URI(url); } catch (URISyntaxException e) { throw new IOException("Invalid URL: " + url, e); } final String wsScheme = uri.getScheme(); if (!"ws".equals(wsScheme) && !"wss".equals(wsScheme)) { throw new IOException("Unsupported URL scheme: " + wsScheme); } // Create web socket and observatory final WebSocket webSocket; try { webSocket = new WebSocket(uri); } catch (WebSocketException e) { throw new IOException("Failed to create websocket: " + url, e); } final VmOpenSourceLocationListener listener = new VmOpenSourceLocationListener(new MessageSender() { @Override public void sendMessage(JsonObject message) { try { webSocket.send(message.toString()); } catch (WebSocketException e) { LOG.warn(e); } } @Override public void close() { try { webSocket.close(); } catch (WebSocketException e) { LOG.warn(e); } } }); webSocket.setEventHandler(new WebSocketEventHandler() { final JsonParser parser = new JsonParser(); @Override public void onClose() { // ignore } @Override public void onMessage(WebSocketMessage message) { listener.onMessage(parser.parse(message.getText()).getAsJsonObject()); } @Override public void onOpen() { listener.onOpen(); } @Override public void onPing() { // ignore } @Override public void onPong() { // ignore } }); // Establish WebSocket Connection try { webSocket.connect(); } catch (WebSocketException e) { throw new IOException("Failed to connect: " + url, e); } return listener; }
From source file:com.sangupta.jerry.unsafe.UnsafeMemoryUtils.java
public static void writeToFile(UnsafeMemory memory, File file) throws IOException { FileOutputStream stream = null; BufferedOutputStream boss = null; try {/*from w w w. ja va 2 s. c om*/ final byte[] bytes = memory.getBuffer(); final int length = memory.getPosition(); stream = new FileOutputStream(file); boss = new BufferedOutputStream(stream); int CHUNK_SIZE = 1 << 20; int chunks = length / CHUNK_SIZE; int extra = length % CHUNK_SIZE; if (extra > 0) { chunks++; } for (int ki = 0; ki < chunks; ki++) { int delta = CHUNK_SIZE; if ((ki + 1) == chunks) { delta = extra; } boss.write(bytes, ki * CHUNK_SIZE, delta); } } catch (IOException e) { throw new IOException("Unable to write bytes to disk", e); } finally { IOUtils.closeQuietly(boss); IOUtils.closeQuietly(stream); } }
From source file:com.twitter.distributedlog.feature.AbstractFeatureProvider.java
public static FeatureProvider getFeatureProvider(String rootScope, DistributedLogConfiguration conf, StatsLogger statsLogger) throws IOException { Class<? extends FeatureProvider> featureProviderClass; try {/*from w ww. jav a 2s. c o m*/ featureProviderClass = conf.getFeatureProviderClass(); } catch (ConfigurationException e) { throw new IOException("Can't initialize the feature provider : ", e); } // create feature provider Constructor<? extends FeatureProvider> constructor; try { constructor = featureProviderClass.getDeclaredConstructor(String.class, DistributedLogConfiguration.class, StatsLogger.class); } catch (NoSuchMethodException e) { throw new IOException("No constructor found for feature provider class " + featureProviderClass + " : ", e); } try { return constructor.newInstance(rootScope, conf, statsLogger); } catch (InstantiationException e) { throw new IOException("Failed to instantiate feature provider : ", e); } catch (IllegalAccessException e) { throw new IOException("Encountered illegal access when instantiating feature provider : ", e); } catch (InvocationTargetException e) { Throwable targetException = e.getTargetException(); if (targetException instanceof IOException) { throw (IOException) targetException; } else { throw new IOException( "Encountered invocation target exception while instantiating feature provider : ", e); } } }