Java IOException Create toIOException(Throwable inThrownException, boolean inCheckCauses)

Here you can find the source of toIOException(Throwable inThrownException, boolean inCheckCauses)

Description

to IO Exception

License

Open Source License

Declaration

public static IOException toIOException(Throwable inThrownException, boolean inCheckCauses) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;

import java.util.HashSet;

import java.util.Set;

public class Main {
    public static IOException toIOException(Throwable inThrownException, boolean inCheckCauses) {

        if (inThrownException == null) {
            return null;
        }//from   w ww .  ja v a2 s .c o  m
        if (inThrownException instanceof IOException) {
            return (IOException) inThrownException;
        }
        if (inCheckCauses) {

            final IOException ioe = findException(IOException.class, inThrownException);
            if (ioe != null) {
                return ioe;
            }
        }

        return new IOException(inThrownException);
    }

    @SuppressWarnings("unchecked")
    private static <T extends Throwable> T findException(Class<T> inThrowableClassToFind, Throwable inThrowable) {

        if (inThrowable == null || inThrowableClassToFind == null) {
            return null;
        }

        final Set<Throwable> checkedThrowables = new HashSet<>();

        while (inThrowable != null) {

            if (inThrowableClassToFind.isAssignableFrom(inThrowable.getClass())) {
                return (T) inThrowable;
            }

            if (checkedThrowables.contains(inThrowable)) {
                return null;
            }
            checkedThrowables.add(inThrowable);

            inThrowable = inThrowable.getCause();
        }

        return null;
    }
}

Related

  1. toIOException(final Throwable e)
  2. toIOException(String msg, Throwable cause)
  3. toIoException(Throwable cause)
  4. toIOException(Throwable cause)
  5. toIOException(Throwable e)
  6. toIOException(Throwable t)