Class IOUtil
- java.lang.Object
-
- net.sourceforge.pmd.internal.util.IOUtil
-
public final class IOUtil extends Object
- Author:
- Brian Remedios
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
IOUtil.BomAwareInputStream
Input stream that skips an optional byte order mark at the beginning of the stream.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Exception
closeAll(Collection<? extends AutoCloseable> closeables)
Close all closeable resources in order.static void
closeQuietly(Closeable closeable)
static void
copy(InputStream from, OutputStream to)
static void
copy(Reader from, Writer to)
static Writer
createWriter()
Creates a writer that writes to stdout using the system default charset.static Writer
createWriter(String reportFile)
Creates a writer that writes to the given file or to stdout.static Writer
createWriter(Charset charset, String reportFile)
Creates a writer that writes to the given file or to stdout.static void
ensureClosed(List<? extends AutoCloseable> toClose, @Nullable Exception pendingException)
Ensure that the closeables are closed.static boolean
equalsNormalizedPaths(String path1, String path2)
static InputStream
fromReader(Reader reader)
static OutputStream
fromWriter(Writer writer, String encoding)
static String
getFilenameBase(String name)
static String
getFilenameExtension(String name)
static String
normalizePath(String path)
static String
readFileToString(File file)
static String
readFileToString(File file, Charset charset)
static String
readToString(InputStream stream, Charset charset)
static String
readToString(Reader reader)
static long
skipFully(InputStream stream, long n)
static byte[]
toByteArray(InputStream stream)
static void
tryCloseClassLoader(ClassLoader classLoader)
-
-
-
Field Detail
-
UTF_BOM
public static final char UTF_BOM
Unicode BOM character. Replaces commons io ByteOrderMark.- See Also:
- Constant Field Values
-
EOF
public static final int EOF
Conventional return value for readers.- See Also:
- Constant Field Values
-
-
Method Detail
-
createWriter
public static Writer createWriter()
Creates a writer that writes to stdout using the system default charset.- Returns:
- a writer, never null
- See Also:
createWriter(String)
,createWriter(Charset, String)
-
createWriter
public static Writer createWriter(String reportFile)
Creates a writer that writes to the given file or to stdout. The file is created if it does not exist.Warning: This writer always uses the system default charset.
- Parameters:
reportFile
- the file name (optional)- Returns:
- the writer, never null
-
createWriter
public static Writer createWriter(Charset charset, String reportFile)
Creates a writer that writes to the given file or to stdout. The file is created if it does not exist.Unlike
createWriter(String)
, this method always uses the given charset. Even for writing to stdout. It never falls back to the default charset.- Parameters:
charset
- the charset to be used (required)reportFile
- the file name (optional)- Returns:
-
tryCloseClassLoader
public static void tryCloseClassLoader(ClassLoader classLoader)
-
closeAll
public static Exception closeAll(Collection<? extends AutoCloseable> closeables)
Close all closeable resources in order. If any exception occurs, it is saved and returned. If more than one exception occurs, the following are accumulated as suppressed exceptions in the first.- Parameters:
closeables
- Resources to close- Returns:
- An exception, or null if no 'close' routine threw
-
ensureClosed
public static void ensureClosed(List<? extends AutoCloseable> toClose, @Nullable Exception pendingException) throws Exception
Ensure that the closeables are closed. In the end, throws the pending exception if not null, or the exception retuned bycloseAll(Collection)
if not null. If both are non-null, adds one of them to the suppress list of the other, and throws that one.- Throws:
Exception
-
closeQuietly
public static void closeQuietly(Closeable closeable)
-
toByteArray
public static byte[] toByteArray(InputStream stream) throws IOException
- Throws:
IOException
-
skipFully
public static long skipFully(InputStream stream, long n) throws IOException
- Throws:
IOException
-
copy
public static void copy(InputStream from, OutputStream to) throws IOException
- Throws:
IOException
-
copy
public static void copy(Reader from, Writer to) throws IOException
- Throws:
IOException
-
readFileToString
public static String readFileToString(File file) throws IOException
- Throws:
IOException
-
readFileToString
public static String readFileToString(File file, Charset charset) throws IOException
- Throws:
IOException
-
readToString
public static String readToString(Reader reader) throws IOException
- Throws:
IOException
-
readToString
public static String readToString(InputStream stream, Charset charset) throws IOException
- Throws:
IOException
-
fromReader
public static InputStream fromReader(Reader reader) throws IOException
- Throws:
IOException
-
fromWriter
public static OutputStream fromWriter(Writer writer, String encoding) throws UnsupportedCharsetException
- Throws:
UnsupportedCharsetException
-
-