public class ClientServerIO extends Object
Modifier and Type | Field and Description |
---|---|
static String |
UTF_8
Constant for UTF-8 name.
|
Constructor and Description |
---|
ClientServerIO() |
Modifier and Type | Method and Description |
---|---|
static void |
copyFile(File sourceFile,
File destinationFile)
Copies data from
sourceFile to destinationFile . |
static void |
extractURLToFile(File file,
URL url)
Writes the content provided at a specified URL into a file.
|
static byte[] |
getBytes(InputStream inputStream)
This method returns a byte array containing the data in the specified stream.
|
static byte[] |
getFileBytes(File file)
Returns a byte array containing a File's content.
|
static String |
getFileContent(File file)
Returns the text content of a file.
|
static URLReturnData |
getURLBytes(String sURL)
Returns the content provided at a specified URL.
|
static URLReturnData |
getURLBytes(String sURL,
String cookieString)
Returns the content provided at a specified URL.
|
static DataInputStream |
getURLInputStream(String url)
Returns the content provided at a specified URL as a stream.
|
static Properties |
loadProperties(String propertiesPath)
Loads an instance of java.util.Properties from the specified file path.
|
static String |
makeXssSafe(String input)
Remove html entities from a string to prevent malicious Javascript and cross site scripting attacks.
|
static int |
readBlocking(InputStream inputStream,
byte[] byteArray,
int offset,
int length)
Reads exactly
length bytes from the input stream into the
byte array. |
static String |
readStringFromInputStream(InputStream inputStream)
Reads data from the specified inputStream and returns it as a String.
|
static void |
saveFileBytes(byte[] data,
File file)
Saves the specified byte array to the specified File.
|
static byte[] |
toBytes(Object object)
Serializes an object out to an ObjectOutputStream and then returns the serialized content as a byte array.
|
static boolean |
transferStreams(InputStream sourceStream,
OutputStream destinationStream)
Copy an InputStream to an OutputStream, until EOF.
|
static File |
writeInputStreamToTempFile(InputStream inputStream,
String prefix,
String suffix)
Writes the data from the specified InputStream to a temp file.
|
public static final String UTF_8
public static URLReturnData getURLBytes(String sURL) throws IOException
sURL
- The address of content to retrieveIOException
- IOExceptionpublic static URLReturnData getURLBytes(String sURL, String cookieString) throws IOException
sURL
- The address of content to retrievecookieString
- A cookie string to add to the requestIOException
- IOExceptionpublic static Properties loadProperties(String propertiesPath) throws IOException
Loads an instance of java.util.Properties from the specified file path. The specified file is expected to be a text file following the java.util.Properties format such as the following:
color=green city=Boston state=Massachusetts
propertiesPath
- path to properties fileIOException
- if properties file cannot be readpublic static String makeXssSafe(String input)
input
- String to be sanitizedpublic static boolean transferStreams(InputStream sourceStream, OutputStream destinationStream)
sourceStream
- Stream to be copied. The stream will not be closed by this method.destinationStream
- Stream to be copied to. The stream will not be closed by this method.public static final int readBlocking(InputStream inputStream, byte[] byteArray, int offset, int length) throws IOException
Reads exactly length
bytes from the input stream into the
byte array. This method reads repeatedly from the underlying stream until
all the bytes are read. InputStream.read is often documented to block
like this, but in actuality it does not always do so, and returns early
with just a few bytes. readBlocking blocks until all the bytes are read,
the end of the stream is detected, or an exception is thrown. You will
always get as many bytes as you asked for unless you get an EOF or other
exception. Unlike readFully, you find out how many bytes you did get.
inputStream
- Source streambyteArray
- the buffer into which the data is read.offset
- the start offset of the data.length
- the number of bytes to read.IOException
- if an I/O error occurs.public static byte[] getBytes(InputStream inputStream)
inputStream
- The input stream to readpublic static String readStringFromInputStream(InputStream inputStream)
inputStream
- Source streampublic static File writeInputStreamToTempFile(InputStream inputStream, String prefix, String suffix)
inputStream
- stream to copy data fromprefix
- prefix to the temp filenamesuffix
- suffix to the temp filenamepublic static void saveFileBytes(byte[] data, File file)
data
- data to savefile
- file reference to save data topublic static void copyFile(File sourceFile, File destinationFile) throws IOException
sourceFile
to destinationFile
.sourceFile
- Source filedestinationFile
- Destination fileIOException
- IOExceptionpublic static byte[] getFileBytes(File file) throws IOException
file
- File reference to read fromfile
IOException
- if data could not be read from filepublic static String getFileContent(File file) throws IOException
file
- File reference to read fromfile
IOException
- if data could not be read from filepublic static DataInputStream getURLInputStream(String url) throws IOException
url
- The address of content to retrieveIOException
- IOExceptionpublic static byte[] toBytes(Object object) throws IOException
object
- object to be serializedIOException
- if an I/O exception occurs while writing to streamObjectOutputStream.writeObject(java.lang.Object)
public static void extractURLToFile(File file, URL url) throws IOException
file
- Destination fileurl
- The address of content to retrieveIOException
- IOExceptionCopyright © 2020 Snowbound Software Corporation. All rights reserved.