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:de.tudarmstadt.ukp.dkpro.keyphrases.bookindexing.util.SerializationUtils.java

/**
 * @param object//from  w  w w  .  j a va 2 s  .co m
 *          object to be serialized
 * @param filePath
 *          the object will be written at this location, directories will be
 *          created according to path
 * @throws Exception exception
 */
public static void serializeToDisk(Serializable object, String filePath) throws Exception {
    File dir = new File(FilenameUtils.getFullPathNoEndSeparator(filePath));
    if (!dir.exists()) {
        FileUtils.forceMkdir(dir);
    } else {
        if (dir.isFile()) {
            throw new IOException("Path to dir is a file!");
        }
    }
    ObjectOutputStream objOut = null;
    objOut = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(filePath)));
    objOut.writeObject(object);
    objOut.flush();
    objOut.close();
}

From source file:com.cyphermessenger.client.SyncRequest.java

public static Captcha requestCaptcha() throws IOException, APIErrorException {
    HttpURLConnection conn = doRequest("captcha");
    if (conn.getResponseCode() != 200) {
        throw new IOException("Server error");
    }/*www  . j  a  va  2  s  .  co  m*/
    InputStream in = conn.getInputStream();
    JsonNode node = MAPPER.readTree(in);
    conn.disconnect();
    int statusCode = node.get("status").asInt();
    String captchaTokenString = node.get("captchaToken").asText();
    String captchaHashString = node.get("captchaHash").asText();
    String captchaImageString = node.get("captchaBytes").asText();

    byte[] captchaHash = Utils.BASE64_URL.decode(captchaHashString);
    byte[] captchaImage = Utils.BASE64_URL.decode(captchaImageString);
    if (statusCode == StatusCode.OK) {
        return new Captcha(captchaTokenString, captchaHash, captchaImage);
    } else {
        throw new APIErrorException(statusCode);
    }
}

From source file:Main.java

/**
 * Writes the given {@link Document} to an {@link OutputStream} using
 * UTF-8 encoding.//from  w ww .  j  a  v  a2 s  .co m
 *
 * @param doc The {@link Document} to write.
 * @param out The {@link OutputStream} to write to.
 * @see #encodeDocument(Document,boolean)
 * @see #writeDocumentTo(Document,File)
 */
public static void writeDocumentTo(Document doc, OutputStream out) throws IOException {
    final DOMSource source = new DOMSource(doc);
    final Writer writer = new OutputStreamWriter(out, "UTF-8");
    final StreamResult result = new StreamResult(writer);
    final Transformer xform = createTransformer();
    try {
        xform.transform(source, result);
    } catch (TransformerException e) {
        throw new IOException("Couldn't write XML: " + e.getMessage());
    }
}

From source file:edu.illinois.cs.cogcomp.core.utilities.SerializationHelper.java

/**
 * Serialize a TextAnnotation and then write to file. If forceOverwrite_ is set to false and
 * file already exists, this method throws an exception.
 *
 * @param ta The text annotation to be serialized
 * @param fileName Name of file to write to
 * @param forceOverwrite Whether or not to overwrite existing file.
 *///from www  . ja  va 2s.  c  o  m
public static void serializeTextAnnotationToFile(TextAnnotation ta, String fileName, boolean forceOverwrite)
        throws IOException {
    File outFile = new File(fileName);
    if (outFile.exists() && !forceOverwrite)
        throw new IOException(
                "ERROR: " + NAME + ".serializeTextAnnotationToFile(): file '" + fileName + "' already exists.");
    FileUtils.writeByteArrayToFile(outFile, serializeTextAnnotationToBytes(ta));
}

From source file:net.idlesoft.android.apps.github.utils.GravatarCache.java

private static File ensure_directory(final String path) throws IOException {
    File root = Environment.getExternalStorageDirectory();
    if (!root.canWrite()) {
        throw new IOException("External storage directory is not writable");
    }// ww w .  ja v  a 2 s.  c  om
    final String[] parts = path.split("/");
    for (final String part : parts) {
        final File f = new File(root, part);
        if (!f.exists()) {
            final boolean created = f.mkdir();
            if (!created) {
                throw new IOException("Unable to create directory " + part);
            }
        } else {
            if (!f.isDirectory()) {
                throw new IOException("Unable to create directory " + part);
            }
        }
        root = f;
    }
    return root;
}

From source file:com.ebay.logstorm.core.compiler.PipelineCompiler.java

public static Pipeline compileResource(String resource) throws IOException, PipelineException {
    URL resourceUrl = PipelineCompiler.class.getResource(resource);
    if (resourceUrl == null) {
        throw new IOException("Resource " + resource + " not found");
    } else {//from   ww w .ja  v a 2s  . c o  m
        return compileConfigString(FileUtils.readFileToString(new File(resourceUrl.getPath())));
    }
}

From source file:Main.java

public static byte[] serializeToByteArray(Document doc) throws IOException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    try {//from w w  w  . j a  va 2  s. c  o m
        transformer = tFactory.newTransformer();
    } catch (TransformerConfigurationException e) {
        throw new IOException("Unable to serialize XML document");
    }
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    String encoding = doc.getInputEncoding();
    if (encoding == null)
        encoding = "UTF-8";
    transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    DOMSource source = new DOMSource(doc);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Result result = new StreamResult(out);
    try {
        transformer.transform(source, result);
    } catch (TransformerException e) {
        throw new IOException("Unable to serialize XML document");
    }

    return out.toByteArray();
}

From source file:com.qwazr.search.index.IndexManager.java

public synchronized static Class<? extends IndexServiceInterface> load(ExecutorService executorService,
        File dataDirectory) throws IOException {
    if (INSTANCE != null)
        throw new IOException("Already loaded");
    File indexesDirectory = new File(dataDirectory, INDEXES_DIRECTORY);
    if (!indexesDirectory.exists())
        indexesDirectory.mkdir();/*w  ww.  ja  v a 2  s  .c  o m*/
    if (!indexesDirectory.isDirectory())
        throw new IOException(
                "This name is not valid. No directory exists for this location: " + indexesDirectory.getName());
    INSTANCE = new IndexManager(executorService, indexesDirectory);
    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            INSTANCE.shutdown();
        }
    });
    return IndexServiceImpl.class;
}

From source file:ja.centre.util.io.Files.java

public static void ensureDirectoryExists(String directoryName) throws IOException {
    Arguments.assertNotNull("directoryName", directoryName);

    File dirFile = new File(directoryName);
    if (dirFile.exists()) {
        if (!dirFile.isDirectory()) {
            throw new IOException("File '" + directoryName
                    + "' already exists and it's not a directory. It should not exist, or should be a directoryName.");
        }//from  ww  w . j a v  a 2 s. co  m
    } else if (!dirFile.mkdirs()) {
        throw new IOException("Unable to create directory \"" + directoryName + "\"");
    }
}

From source file:foam.dao.index.PersistedIndex.java

public PersistedIndex(String filename, Index index) throws IOException {
    this.file_ = new File(filename).getAbsoluteFile();
    if (!file_.exists()) {
        if (!this.file_.createNewFile()) {
            throw new IOException("Unable to create file: " + filename);
        }//from  w w w  . jav a 2 s  .  com
    }
    this.fis_ = new FileInputStream(this.file_);
    this.fos_ = new FileOutputStream(this.file_);
    setDelegate(index);
}