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

private static File commit(String basename, File tmpFile) throws IOException {
    try {//from ww  w. j a v  a2 s. co m
        final String backupName = basename + "~";
        final File desired = new File(basename);
        final File backup = new File(backupName);
        backup.delete();
        if (desired.exists()) {
            if (!desired.renameTo(new File(backupName)))
                throw new IOException("can't rename to " + backupName);
        }
        if (!tmpFile.renameTo(new File(basename)))
            throw new IOException("can't rename to " + basename);
    } catch (IOException x) {
        tmpFile.delete();
        throw x;
    }
    return new File(basename);
}

From source file:Main.java

public static void writeInputStreamToOutputStream(InputStream in, OutputStream out, long maxLength)
        throws IOException {
    byte[] buf = new byte[4096];
    int bytesRead;

    while ((bytesRead = in.read(buf)) != -1) {
        maxLength -= bytesRead;//from w  w  w  .j ava  2  s  .  com
        if (maxLength <= 0) {
            throw new IOException("Stream exceeded max size");
        }
        out.write(buf, 0, bytesRead);
    }
}

From source file:Main.java

/**
 * utility method to create a folder/*w  ww.  j  a  v a  2s. c om*/
 *
 * @param path full path to create
 * @return {@link File} object of the folder just created
 * @throws IOException
 */
private static File createFolder(String path) throws IOException {
    File file = new File(path);
    if (!file.exists()) {
        if (!file.mkdir()) {
            throw new IOException("couldn't create folder '" + path + "'");
        }
    }
    return file;
}

From source file:Main.java

/**
 * Read xml Signature by given name from local Signature store
 * @param filename/*w  ww . j  a va 2s.  c om*/
 * @return File object with xml signature
 * @throws Exception when file doesn't exists
 */
public static File getSignatureXMLFile(String filename) throws IOException {
    File file = new File(baseDir + File.separator + filename);
    if (!file.exists())
        throw new IOException("given xml signature file doesn't exist :" + filename);
    return file;
}

From source file:Main.java

public static byte[] getFileBytes(File file) throws IOException {
    BufferedInputStream bis = null;
    try {//from  w w w .  ja v a  2 s .com
        bis = new BufferedInputStream(new FileInputStream(file));
        int bytes = (int) file.length();
        byte[] buffer = new byte[bytes];
        int readBytes = bis.read(buffer);
        if (readBytes != buffer.length) {
            throw new IOException("Entire file not read");
        }
        return buffer;
    } finally {
        if (bis != null) {
            bis.close();
        }
    }
}

From source file:Main.java

public static File saveText(String text) throws IOException {
    File f;/*w  w w  . j  a v a  2s.  c  o  m*/
    String filename;
    if (!isExternalStorageWritable())
        throw new IOException("Storage not available");
    DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    filename = dateFormat.format(new Date());

    f = new File(getAlbumStorageDir(FOLDER_NAME), "Message " + filename + ".txt");
    FileWriter fw = new FileWriter(f);
    fw.write(text);
    fw.close();
    return f;
}

From source file:Main.java

public static String responseAsString(HttpResponse response) throws IOException {

    StatusLine status = response.getStatusLine();
    if (status.getStatusCode() != HTTP_STATUS_OK) {
        throw new IOException("Invalid response from server! " + status.toString());
    }//from  w  w w. ja v a  2 s  . c o m

    //do the herdy gerdy and convert the response to a String
    InputStream ist = response.getEntity().getContent();

    ByteArrayOutputStream content = new ByteArrayOutputStream();
    int readCount = 0;
    while ((readCount = ist.read(buff)) != -1)
        content.write(buff, 0, readCount);

    String thePagesContent = EncodingUtils.getString(content.toByteArray(), "UTF-8");

    return thePagesContent;
}

From source file:Main.java

public static Document parse(InputStream is) throws IOException {
    try {//from   w w w .j  a  v a  2s  .c  om
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        return builder.parse(is);
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:Utils.java

public static void deleteRecursive(File dir) throws IOException {
    if (dir.isDirectory())
        deleteContentsRecursive(dir);/*from   www . j  a v  a  2  s . com*/
    if (!dir.delete())
        throw new IOException("Unable to delete " + dir);
}

From source file:Main.java

private static byte[] readSmallerStream(final InputStream in, final int streamlength) throws IOException {
    byte[] bytes = new byte[streamlength];
    int readed = in.read(bytes);
    int patchsize = 1;
    while (patchsize > 0 && readed != streamlength) {
        patchsize = in.read(bytes, readed, streamlength - readed);
        if (patchsize > 0) {
            readed += patchsize;/*from  www .ja v a 2  s  . c o m*/
        }
    }
    if (readed != streamlength) {
        throw new IOException("InputStream(" + in + ", streamlength=" + streamlength + ", readedlength="
                + readed + ") read fail");
    }
    return bytes;
}