Package com.attivio.util
Class IOUtils
- java.lang.Object
-
- com.attivio.util.IOUtils
-
public class IOUtils extends java.lang.ObjectContains utilities for reading/writing streams.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceIOUtils.OutputStreamConsumerUsed for copying an OutputStream into an InputStream.
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringDEFAULT_ENCODINGThe default encoding value (UTF-8).static java.lang.StringEXE_SUFFIXstatic java.lang.StringFILE_SEPstatic java.lang.Stringi386Deprecated.UseI386instead.static java.lang.StringI38632 bit OS architecture identifier.static java.lang.StringLINUXThe 'Linux' OS family identifier.static java.lang.StringMACThe 'Mac' OS family identifier.static java.lang.StringSOLARISThe 'Solaris' OS family identifier.static java.lang.StringWINDOWSThe 'Windows' OS family identifier.static java.lang.Stringx32Deprecated.UseX32instead.static java.lang.StringX3232 bit OS architecture identifier.static java.lang.Stringx64Deprecated.UseX64instead.static java.lang.StringX6464 bit OS architecture identifier.static java.lang.StringX86_64x86/64 bit OS architecture identifier (mac).
-
Constructor Summary
Constructors Modifier Constructor Description protectedIOUtils()The default constructor (hidden).
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleancheckIfPortInUse(int port)Checks to see if a specified port is in use on the local host.static java.lang.Exceptionclose(java.io.Closeable closeable)Safely close anyCloseable(Input/OutputStreams, Readers, Writers), ignoring Null streams..static java.lang.Exceptionclose(java.io.Closeable closeable, StatusListener statusListener, java.lang.String streamId)Safely close anyCloseable(Input/OutputStreams, Readers, Writers), ignoring Null streams and logging a message if failure occurs.static voidcopyStream(java.io.InputStream in, java.io.OutputStream out)Copies the contents from an OutputStream into an InputStream.static longcopyStream(java.io.InputStream in, java.io.OutputStream out, boolean closeStreams)Copies the contents from an OutputStream into an InputStream.static java.lang.StringfindIpAddress(java.lang.String startsWith)Returns the 1st IP address that starts with the given string.static java.lang.StringfixPathSeparators(java.lang.String path)Make path separators consistent and correct for the local platform.static java.lang.StringgetBinaryExtension()Get the binary filename extension for the current platformstatic byte[]getBytes(java.lang.String s)Gets bytes from a String in the proper encoding.static java.lang.LonggetChecksumFromString(java.lang.String contents)Return theCRC32checksum for the contents of this string.static java.lang.StringgetIpAddress()static java.lang.StringgetIpAddress(java.lang.String host)static java.lang.StringgetOSArchitecture()Gets a nice clean OS cpu architecture name using system propertiesstatic java.lang.StringgetOSFamily()Gets a nice clean OS name using system properties.static intgetPID()Gets the ProcessID of the JVM from java.lang.management.ManagementFactory.getRuntimeMXBean().getName()static java.lang.StringgetString(byte[] bytes)Gets String from a byte array in proper encoding.static java.io.OutputStreamgetZipOutputStream(java.util.zip.ZipOutputStream zip, java.lang.String dir, java.lang.String path)Get output stream for a zip file.static java.io.WritergetZipWriter(java.util.zip.ZipOutputStream zip, java.lang.String dir, java.lang.String path)Get a writer object for writing a file within a zip filestatic voidinitIpAddress()Get the IP address of this node as a String.static booleanisLocalHost(java.lang.String host)Note, this method will not return true ifhostis the real hostname of this system.static booleanisPortInUse(int port, int numRetries)CallsisPortInUse(int, int, int)with a retry wait time of 0static booleanisPortInUse(int port, int numRetries, int retryWaitTimeInMilliseconds)Checks to see if a port is in use with a given number of loops (numRetries) waiting retryWaitTimeInMilliseconds for each loop.static booleanisSameHost(java.lang.String otherHost)Determines is the local host is the same as theotherHostspecified.static java.lang.StringlocalizedHost(java.lang.String host)static java.io.ReaderopenReader(java.io.File file, java.lang.String encoding)Open a reader for a file with a specified encoding.static java.io.ReaderopenReader(java.net.URI uri, java.lang.String encoding)Open a reader for a file with a specified encoding.static intrandomPort()Taken from org.apache.curator.test.InstanceSpecstatic byte[]readAsBytes(java.io.InputStream is)Reads the contents of an InputStream.static byte[]readAsBytes(java.io.InputStream input, int limit)Reads up tolimitbytes frominput.static java.lang.StringreadAsText(java.io.InputStream in)Reads InputStream using the default encoding.static java.lang.StringreadAsText(java.io.InputStream in, java.lang.String encoding)Reads InputStream using the default encoding.static java.lang.StringreadAsText(java.io.InputStream in, java.nio.charset.Charset charset)Reads InputStream using the default encoding.static java.lang.StringreadAsText(java.io.Reader r)Gets the contents of a reader as text.static java.lang.StringreadAsTextSafely(java.io.InputStream in)Reads InputStream using the default encoding, trapping IOException and returning null.static voidregisterURLProtocolHandler(java.lang.Class<? extends java.net.URLStreamHandler> clazz)Register a protocol handler for a specific class.static java.io.InputStreamtoInputStream(IOUtils.OutputStreamConsumer ss)Creates an InputStream which reads data written by theIOUtils.OutputStreamConsumer.static java.io.InputStreamtoInputStream(IOUtils.OutputStreamConsumer ss, int pipeSize)Creates an InputStream which reads data written by theIOUtils.OutputStreamConsumer.static java.io.IOExceptiontoIOException(AttivioException ex)If the parameter was caused by an IOException, the cause is returned, otherwise a new IOException with the parameter is constructed and returned.static java.net.URItoUri(java.lang.String file)Returns a URI representing the file.static voidwrite(java.lang.String s, java.io.OutputStream out)Writes out a String.static voidzipDir(java.util.zip.ZipOutputStream zip, java.lang.String path, java.io.File dir, java.io.FileFilter ff)Take the contents of the directory passed in and add it to the ZIP output stream at the location pointed to by path.
-
-
-
Field Detail
-
MAC
public static final java.lang.String MAC
The 'Mac' OS family identifier.- See Also:
- Constant Field Values
-
LINUX
public static final java.lang.String LINUX
The 'Linux' OS family identifier.- See Also:
- Constant Field Values
-
WINDOWS
public static final java.lang.String WINDOWS
The 'Windows' OS family identifier.- See Also:
- Constant Field Values
-
SOLARIS
public static final java.lang.String SOLARIS
The 'Solaris' OS family identifier.- See Also:
- Constant Field Values
-
X64
public static final java.lang.String X64
64 bit OS architecture identifier.- See Also:
- Constant Field Values
-
X86_64
public static final java.lang.String X86_64
x86/64 bit OS architecture identifier (mac).- See Also:
- Constant Field Values
-
X32
public static final java.lang.String X32
32 bit OS architecture identifier.- See Also:
- Constant Field Values
-
I386
public static final java.lang.String I386
32 bit OS architecture identifier.- See Also:
- Constant Field Values
-
x64
@Deprecated public static final java.lang.String x64
Deprecated.UseX64instead.64 bit OS architecture identifier.- See Also:
- Constant Field Values
-
x32
@Deprecated public static final java.lang.String x32
Deprecated.UseX32instead.32 bit OS architecture identifier.- See Also:
- Constant Field Values
-
i386
@Deprecated public static final java.lang.String i386
Deprecated.UseI386instead.32 bit OS architecture identifier.- See Also:
- Constant Field Values
-
DEFAULT_ENCODING
public static final java.lang.String DEFAULT_ENCODING
The default encoding value (UTF-8).- See Also:
- Constant Field Values
-
EXE_SUFFIX
public static final java.lang.String EXE_SUFFIX
-
FILE_SEP
public static final java.lang.String FILE_SEP
-
-
Method Detail
-
readAsText
public static java.lang.String readAsText(java.io.Reader r) throws java.io.IOExceptionGets the contents of a reader as text. WARNING: Does not close stream- Parameters:
r- the reader to read- Returns:
- String from Reader
- Throws:
java.io.IOException- if an error occurs reading
-
readAsText
public static java.lang.String readAsText(java.io.InputStream in, java.lang.String encoding) throws java.io.IOExceptionReads InputStream using the default encoding. WARNING: Does not close stream- Parameters:
in- stream to readencoding- encoding to use- Returns:
- String from InputStream
- Throws:
java.io.IOException- if an error occurs
-
readAsText
public static java.lang.String readAsText(java.io.InputStream in, java.nio.charset.Charset charset) throws java.io.IOExceptionReads InputStream using the default encoding. WARNING: Does not close stream- Parameters:
in- stream to readcharset- encoding to use- Returns:
- String from InputStream
- Throws:
java.io.IOException- if an error occurs
-
readAsText
public static java.lang.String readAsText(java.io.InputStream in) throws java.io.IOExceptionReads InputStream using the default encoding. WARNING: Does not close stream- Parameters:
in- stream to read in default encoding- Returns:
- String from InputStream
- Throws:
java.io.IOException- if an error occurs
-
readAsTextSafely
public static java.lang.String readAsTextSafely(java.io.InputStream in)
Reads InputStream using the default encoding, trapping IOException and returning null. WARNING: Does not close stream- Parameters:
in-- Returns:
- the stream as a string or null if error was encountered reading the stream.
-
readAsBytes
public static byte[] readAsBytes(java.io.InputStream is) throws java.io.IOExceptionReads the contents of an InputStream. WARNING: Does not close stream- Parameters:
is- stream to read- Returns:
- byte array from InputStream
- Throws:
java.io.IOException- if an error occurs
-
readAsBytes
public static byte[] readAsBytes(java.io.InputStream input, int limit) throws java.io.IOExceptionReads up tolimitbytes frominput.WARNING: does not close stream.
- Throws:
java.io.IOException
-
copyStream
public static void copyStream(java.io.InputStream in, java.io.OutputStream out) throws java.io.IOExceptionCopies the contents from an OutputStream into an InputStream. Both streams will be closed after the copy operation completes.- Parameters:
in- input streamout- output stream- Throws:
java.io.IOException- if error- See Also:
- Java I/O Performance
-
copyStream
public static long copyStream(java.io.InputStream in, java.io.OutputStream out, boolean closeStreams) throws java.io.IOExceptionCopies the contents from an OutputStream into an InputStream.- Parameters:
in- input streamout- output streamcloseStreams- if true, both streams will be closed after the copy operation completes successfully- Throws:
java.io.IOException- if error- See Also:
- Java I/O Performance
-
getBytes
public static byte[] getBytes(java.lang.String s)
Gets bytes from a String in the proper encoding.- Parameters:
s- string to get bytes of- Returns:
- byte array encoded in UTF-8, if string is null return an empty byte array
-
getString
public static java.lang.String getString(byte[] bytes)
Gets String from a byte array in proper encoding.- Parameters:
bytes- byte array to get String from. if bytes is null, returns null
-
getZipWriter
public static java.io.Writer getZipWriter(java.util.zip.ZipOutputStream zip, java.lang.String dir, java.lang.String path) throws java.io.IOExceptionGet a writer object for writing a file within a zip file- Parameters:
zip- output stream for the zip filedir- directory in the zip file to house filepath- the path of the file within the zip file- Returns:
- writer
- Throws:
java.io.IOException
-
zipDir
public static void zipDir(java.util.zip.ZipOutputStream zip, java.lang.String path, java.io.File dir, java.io.FileFilter ff) throws java.io.IOExceptionTake the contents of the directory passed in and add it to the ZIP output stream at the location pointed to by path.- Parameters:
zip- theZipOutputStreamto add topath- the location within the output stream where the contents of the directory should go, ornullto put them at the root of the ZIPdir- the directory on the local file system of the directory whose contents you want to zip upff- an optional filter to use when looking at the contents of the directory (passnullto include all files)- Throws:
java.io.IOException
-
getZipOutputStream
public static java.io.OutputStream getZipOutputStream(java.util.zip.ZipOutputStream zip, java.lang.String dir, java.lang.String path) throws java.io.IOExceptionGet output stream for a zip file.- Parameters:
zip- the output streamdir- the directory in the zip file to house filepath- the path of the file within the zip file- Returns:
- output stream for the zip file
- Throws:
java.io.IOException
-
write
public static void write(java.lang.String s, java.io.OutputStream out) throws java.io.IOExceptionWrites out a String.- Parameters:
s- String to writeout- Stream to write to in default encoding- Throws:
java.io.IOException- if an error occors while writing
-
getPID
public static int getPID()
Gets the ProcessID of the JVM from java.lang.management.ManagementFactory.getRuntimeMXBean().getName()- Returns:
- the process ID of the JVM
-
initIpAddress
public static void initIpAddress()
Get the IP address of this node as a String.NOTE: in the event that the default local host address is a loopback address, all non-local network interfaces will be searched for the first ipv4 address. This may still result in an address that is not externally available on the network. To avoid this, you must configure your host to use a static ip address.
-
getIpAddress
public static java.lang.String getIpAddress()
-
findIpAddress
public static java.lang.String findIpAddress(java.lang.String startsWith) throws java.net.UnknownHostException, java.net.SocketExceptionReturns the 1st IP address that starts with the given string.- Parameters:
startsWith-- Returns:
- Throws:
java.net.UnknownHostExceptionjava.net.SocketException
-
isLocalHost
public static boolean isLocalHost(java.lang.String host)
Note, this method will not return true ifhostis the real hostname of this system.- Parameters:
host-- Returns:
trueifhostisnullor one of 127.0.0.1, ::1, or localhost
-
localizedHost
public static java.lang.String localizedHost(java.lang.String host)
- Parameters:
host-- Returns:
localhostifisLocalHost(String)istrue, otherwise returnshost.
-
isSameHost
public static boolean isSameHost(java.lang.String otherHost) throws AttivioExceptionDetermines is the local host is the same as theotherHostspecified. Checks to see if any of the IP addresses of theotherHostmatch any of the IPs of any of this machine's interfaces.- Parameters:
otherHost- the host to compare to. ifnull, the localhost is assumed, thus resulting in atrueresult.- Throws:
AttivioException
-
getIpAddress
public static java.lang.String getIpAddress(java.lang.String host)
- Parameters:
host-- Returns:
- the IP address of
hostas a String or null if it could not be found.
-
openReader
public static java.io.Reader openReader(java.io.File file, java.lang.String encoding) throws java.io.IOExceptionOpen a reader for a file with a specified encoding.- Throws:
java.io.IOException
-
openReader
public static java.io.Reader openReader(java.net.URI uri, java.lang.String encoding) throws java.io.IOExceptionOpen a reader for a file with a specified encoding.- Throws:
java.io.IOException
-
getOSFamily
public static java.lang.String getOSFamily()
Gets a nice clean OS name using system properties.
-
getOSArchitecture
public static java.lang.String getOSArchitecture()
Gets a nice clean OS cpu architecture name using system properties- Returns:
- the CPU Architecture name, either 32 or 64
-
getBinaryExtension
public static java.lang.String getBinaryExtension()
Get the binary filename extension for the current platform- Returns:
- the binary extension (ie .exe for windows)
-
isPortInUse
public static boolean isPortInUse(int port, int numRetries)CallsisPortInUse(int, int, int)with a retry wait time of 0- Parameters:
port- socket port numbernumRetries- number of attempts to use that socket- Returns:
- true if port is in use, false otherwise
-
isPortInUse
public static boolean isPortInUse(int port, int numRetries, int retryWaitTimeInMilliseconds)Checks to see if a port is in use with a given number of loops (numRetries) waiting retryWaitTimeInMilliseconds for each loop.- Parameters:
port- socket port numbernumRetries- number of attempts to use that socketretryWaitTimeInMilliseconds- time in milliseconds to sleep between retries- Returns:
- true if port is in use, false otherwise
-
fixPathSeparators
public static java.lang.String fixPathSeparators(java.lang.String path)
Make path separators consistent and correct for the local platform.
-
toInputStream
public static java.io.InputStream toInputStream(IOUtils.OutputStreamConsumer ss) throws java.io.IOException
Creates an InputStream which reads data written by theIOUtils.OutputStreamConsumer. An OutputStreamConsumer is an interface which provides a single write method that takes an OutputStream.Sample use
class1.read(IOUtils.toInputStream(new OutputStreamConsumer() public void write(OutputStream os) throws IOException class2.write(os); )); }
- Parameters:
ss- the output stream consumer to write to.- Returns:
- an InputStream which reads data written by the OutputStreamConsumer.
- Throws:
java.io.IOException
-
toInputStream
public static java.io.InputStream toInputStream(IOUtils.OutputStreamConsumer ss, int pipeSize) throws java.io.IOException
Creates an InputStream which reads data written by theIOUtils.OutputStreamConsumer. An OutputStreamConsumer is an interface which provides a single write method that takes an OutputStream.Sample use
class1.read(IOUtils.toInputStream(new OutputStreamConsumer() public void write(OutputStream os) throws IOException class2.write(os); )); }
- Parameters:
ss- the output stream consumer to write to.pipeSize- the size of the buffer between the output and input streams.- Returns:
- an InputStream which reads data written by the OutputStreamConsumer.
- Throws:
java.io.IOException
-
toIOException
public static final java.io.IOException toIOException(AttivioException ex)
If the parameter was caused by an IOException, the cause is returned, otherwise a new IOException with the parameter is constructed and returned. This is particularly useful when implementing close() methods which must throw IOExceptions.- Parameters:
ex-- Returns:
- the IOException
-
close
public static final java.lang.Exception close(java.io.Closeable closeable)
Safely close anyCloseable(Input/OutputStreams, Readers, Writers), ignoring Null streams..
-
close
public static final java.lang.Exception close(java.io.Closeable closeable, StatusListener statusListener, java.lang.String streamId)Safely close anyCloseable(Input/OutputStreams, Readers, Writers), ignoring Null streams and logging a message if failure occurs.
-
checkIfPortInUse
public static boolean checkIfPortInUse(int port)
Checks to see if a specified port is in use on the local host.- Returns:
- true if the port is in use or there was some other exception testing the port, false if port is not in use.
-
randomPort
public static int randomPort()
Taken from org.apache.curator.test.InstanceSpec- Returns:
- a random unused port
-
toUri
public static java.net.URI toUri(java.lang.String file)
Returns a URI representing the file. Iffileis already a URI, it is returned as a URI- Parameters:
file-- Returns:
- a corresponding URI
-
registerURLProtocolHandler
public static void registerURLProtocolHandler(java.lang.Class<? extends java.net.URLStreamHandler> clazz)
Register a protocol handler for a specific class.
-
getChecksumFromString
public static java.lang.Long getChecksumFromString(java.lang.String contents) throws java.io.IOExceptionReturn theCRC32checksum for the contents of this string.- Parameters:
contents-- Returns:
- checksum
- Throws:
java.io.IOException
-
-