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

/**
 * Deletes the specified diretory and any files and directories in it
 * recursively./*from   w w w  .  ja  va  2  s.  co  m*/
 * 
 * @param dir The directory to remove.
 * @throws IOException If the directory could not be removed.
 */
public static void deleteDir(File dir) throws IOException {
    if (!dir.isDirectory()) {
        throw new IOException("Not a directory " + dir);
    }

    File[] files = dir.listFiles();
    for (int i = 0; i < files.length; i++) {
        File file = files[i];

        if (file.isDirectory()) {
            deleteDir(file);
        } else {
            boolean deleted = file.delete();
            if (!deleted) {
                throw new IOException("Unable to delete file" + file);
            }
        }
    }

    dir.delete();
}

From source file:Main.java

public static void writeBytes(File dest, byte[] data, int off, int len, boolean append) throws IOException {
    if (dest.exists() == true) {
        if (dest.isFile() == false) {
            throw new IOException("Not a file: " + dest);
        }//from   w w w . j av  a  2s .  c  o  m
    }
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(dest, append);
        out.write(data, off, len);
    } finally {
        close(out);
    }
}

From source file:Main.java

public static XMLStreamReader stringToStream(String content) throws IOException {

    //get Reader connected to XML input from somewhere..?
    Reader reader = new StringReader(content);
    try {//w  w w . ja va2  s  . c  o m
        return xiFactory.createXMLStreamReader(reader);
    } catch (XMLStreamException ex) {
        throw new IOException(ex);
    }
}

From source file:Main.java

public static byte[] rotateNV21(@NonNull final byte[] yuv, final int width, final int height,
        final int rotation, final boolean flipHorizontal) throws IOException {
    if (rotation == 0)
        return yuv;
    if (rotation % 90 != 0 || rotation < 0 || rotation > 270) {
        throw new IllegalArgumentException("0 <= rotation < 360, rotation % 90 == 0");
    } else if ((width * height * 3) / 2 != yuv.length) {
        throw new IOException("provided width and height don't jive with the data length (" + yuv.length
                + "). Width: " + width + " height: " + height + " = data length: " + (width * height * 3) / 2);
    }//from   www  .  java 2  s.com

    final byte[] output = new byte[yuv.length];
    final int frameSize = width * height;
    final boolean swap = rotation % 180 != 0;
    final boolean xflip = flipHorizontal ? rotation % 270 == 0 : rotation % 270 != 0;
    final boolean yflip = rotation >= 180;

    for (int j = 0; j < height; j++) {
        for (int i = 0; i < width; i++) {
            final int yIn = j * width + i;
            final int uIn = frameSize + (j >> 1) * width + (i & ~1);
            final int vIn = uIn + 1;

            final int wOut = swap ? height : width;
            final int hOut = swap ? width : height;
            final int iSwapped = swap ? j : i;
            final int jSwapped = swap ? i : j;
            final int iOut = xflip ? wOut - iSwapped - 1 : iSwapped;
            final int jOut = yflip ? hOut - jSwapped - 1 : jSwapped;

            final int yOut = jOut * wOut + iOut;
            final int uOut = frameSize + (jOut >> 1) * wOut + (iOut & ~1);
            final int vOut = uOut + 1;

            output[yOut] = (byte) (0xff & yuv[yIn]);
            output[uOut] = (byte) (0xff & yuv[uIn]);
            output[vOut] = (byte) (0xff & yuv[vIn]);
        }
    }
    return output;
}

From source file:Main.java

public static Document parse(File file) throws IOException {
    try {/*from  w  ww . j  a v  a  2s .c  o m*/
        return createDocumentBuilder().parse(file);
    } catch (Exception e) {
        throw new IOException(e);
    }
}

From source file:Main.java

/** 
 * Reads bytes from the specified inputstream into the specified target buffer until it is filled up     
 *///from   ww w  .  j av  a 2s.  c  o m
public static void readBytesUntilFull(InputStream in, byte[] targetBuffer) throws IOException {
    int totalBytesRead = 0;
    int read;
    final int targetBytes = targetBuffer.length;
    do {
        read = in.read(targetBuffer, totalBytesRead, (targetBytes - totalBytesRead));
        if (read != -1) {
            totalBytesRead += read;
        } else {
            throw new IOException("Unexpected EOF reached before read buffer was filled");
        }
    } while (totalBytesRead < targetBytes);
}

From source file:FileCopy.java

public static void copy(String fromFileName, String toFileName) throws IOException {
    File fromFile = new File(fromFileName);
    File toFile = new File(toFileName);

    if (!fromFile.exists())
        throw new IOException("FileCopy: " + "no such source file: " + fromFileName);
    if (!fromFile.isFile())
        throw new IOException("FileCopy: " + "can't copy directory: " + fromFileName);
    if (!fromFile.canRead())
        throw new IOException("FileCopy: " + "source file is unreadable: " + fromFileName);

    if (toFile.isDirectory())
        toFile = new File(toFile, fromFile.getName());

    if (toFile.exists()) {
        if (!toFile.canWrite())
            throw new IOException("FileCopy: " + "destination file is unwriteable: " + toFileName);
        System.out.print("Overwrite existing file " + toFile.getName() + "? (Y/N): ");
        System.out.flush();//from  ww  w. jav  a  2  s. co m
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String response = in.readLine();
        if (!response.equals("Y") && !response.equals("y"))
            throw new IOException("FileCopy: " + "existing file was not overwritten.");
    } else {
        String parent = toFile.getParent();
        if (parent == null)
            parent = System.getProperty("user.dir");
        File dir = new File(parent);
        if (!dir.exists())
            throw new IOException("FileCopy: " + "destination directory doesn't exist: " + parent);
        if (dir.isFile())
            throw new IOException("FileCopy: " + "destination is not a directory: " + parent);
        if (!dir.canWrite())
            throw new IOException("FileCopy: " + "destination directory is unwriteable: " + parent);
    }

    FileInputStream from = null;
    FileOutputStream to = null;
    try {
        from = new FileInputStream(fromFile);
        to = new FileOutputStream(toFile);
        byte[] buffer = new byte[4096];
        int bytesRead;

        while ((bytesRead = from.read(buffer)) != -1)
            to.write(buffer, 0, bytesRead); // write
    } finally {
        if (from != null)
            try {
                from.close();
            } catch (IOException e) {
                ;
            }
        if (to != null)
            try {
                to.close();
            } catch (IOException e) {
                ;
            }
    }
}

From source file:Main.java

public static Document parseXML(InputStream in) throws IOException, SAXException {
    Document doc = null;//from  ww w.  j  av a2 s.  co  m
    try {
        DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        doc = builder.parse(in);
    } catch (ParserConfigurationException e) {
        throw new IOException(e.getMessage());
    } catch (FactoryConfigurationError e) {
        throw new IOException(e.getMessage());
    }
    return doc;
}

From source file:Main.java

private static void addToClassPath(String path) throws IOException, NoSuchMethodException, SecurityException,
        IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    File file = new File(path);
    if (file.exists()) {
        addtoClassLoader(file.toURI().toURL());
    } else {//from w w  w  . j a  va  2 s .  c  o  m
        throw new IOException("File Not Found");
    }
}

From source file:Main.java

public static void serialize(Document document, File targetFile) throws IOException {
    try {/*from  w w  w. j av  a  2 s . c om*/
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.transform(new DOMSource(document), new StreamResult(targetFile));
    } catch (TransformerException e) {
        throw new IOException(e);
    }
}