public class FileHelpers extends Object
Note: SocketTimeoutException
is a subclass of
IOException
.
Note: Using the URLConnection.setReadTimeout(int)
and
URLConnection.setConnectTimeout(int)
methods should
prevent the methods in this class from hanging.
It looks like the best approach is to rerun the process if an error occurs. Start with small timeout values and increase them on each iteration.
Modifier and Type | Field and Description |
---|---|
protected static long |
combinedTime
Timeout for entire transfer in milliseconds.
|
protected static int |
connectTimeout
Length of time allowed for connect operations.
|
protected static int |
debugLevel
Determines quantity of diagnostic output to be generated.
|
protected static int |
readTimeout
Length of time allowed for read operations.
|
protected static int |
timeout
Deprecated.
|
Modifier | Constructor and Description |
---|---|
protected |
FileHelpers()
Since all of the methods and fields are static, the constructor
should never be used.
|
Modifier and Type | Method and Description |
---|---|
static void |
copyFile(File input,
File output)
Copy a file.
|
static void |
copyFile(String inputFileName,
String outputFileName)
Copy a file.
|
static void |
fixDirs(File newFile)
Checks to see if directories for a file exist, and then
creates directories as appropriate.
|
static void |
fixDirs(String name)
Checks to see if directories for a file exist, and then
creates directories as appropriate.
|
static void |
getBytes(File input,
OutputStream output)
Read the contents of a a file and then sends
the data to a OutputStream object.
|
static void |
getBytes(URL url,
File file)
Read the contents of a URL into a file, passing the
data as byte (binary) stream.
|
static void |
getBytes(URL url,
OutputStream output)
Read the contents of a a URL and then sends
the data to a OutputStream object.
|
static long |
getCombinedTime()
Getter for combinedTime
|
static int |
getConnectTimeout()
Getter for connectTimeout.
|
static int |
getDebugLevel()
Getter for debugLevel
|
static int |
getReadTimeout()
Getter for readTimeout.
|
int |
getTimeout()
Deprecated.
|
static String[] |
lineSplitterCommas(String input)
Split a line into tokens using commas as the field separators.
|
static String[] |
lineSplitterSpaces(String input)
Split a line into tokens using spaces as the field separators.
|
static void |
main(String[] args)
This is a test driver to check out the copyFile method.
|
protected static int |
power(int i)
Calculates 2 to the power i as a helper in determining timings
for some of the operations.
|
static String |
readTextFile(String name)
Reads the contents of a text file into a String object
given the name of the file.
|
static String |
readTextFile(URL address)
Read the contents referenced by a URL into a String
object.
|
static void |
sendBytes(File output,
InputStream input)
Send the contents of an InputStream to a File.
|
static void |
sendBytes(URL address,
byte[] data)
Write the contents of a byte array to a URL as a binary data stream.
|
static void |
sendBytes(URL address,
File data)
Write the contents of a file to a URL, passing the data as
a byte stream (binary data).
|
static void |
setCombinedTime(long value)
Setter for combinedTimeout
|
static void |
setConnectTimeout(int value)
Setter for connectTimeout.
|
static void |
setDebugLevel(int value)
Setter for debugLevel
|
static void |
setReadTimeout(int value)
Setter for readTimeout.
|
void |
setTimeout(int value)
Deprecated.
|
static void |
showProperties(URLConnection connection)
Send properties for connection to System.out
|
static void |
thumbnail(File input,
File output,
int sizeX,
int sizeY)
Create a file containing a thumbnail image from another file.
|
static void |
thumbnail(String input,
String output)
Makes file with thumbnail image
|
static void |
thumbnail(String input,
String output,
int size)
Makes file with thumbnail image
|
static void |
thumbnail(String inputFile,
String outputFile,
int sizeX,
int sizeY)
Create a file containing a thumbnail image from another file.
|
protected static int timeout
protected static int connectTimeout
protected static int readTimeout
protected static long combinedTime
getCombinedTime()
,
setConnectTimeout(int)
protected static int debugLevel
getDebugLevel()
,
setDebugLevel(int)
protected FileHelpers()
public static int getConnectTimeout()
connectTimeout
public static void setConnectTimeout(int value)
value
- Value to be used for timeout for socket operations.connectTimeout
public static int getReadTimeout()
readTimeout
public static void setReadTimeout(int value)
value
- Value to be used for timeout for read operationsreadTimeout
public static long getCombinedTime()
combinedTime
public static void setCombinedTime(long value)
value
- Number of milliseconds to be allowed for the entire file transfercombinedTime
public void setTimeout(int value)
value
- Milliseconds to wait for read or socket operationtimeout
public int getTimeout()
timeout
public static int getDebugLevel()
debugLevel
public static void setDebugLevel(int value)
value
- to be used for debugLeveldebugLevel
protected static int power(int i)
i
- Power to which 2 is raisedpublic static String readTextFile(String name) throws IOException
name
- File nameIOException
public static String readTextFile(URL address) throws IOException
The java.net.URL object required as a parameter is
created by a call of the type
java.net.URL url =
new java.net.URL(urlString)
where urlString
is a String object containing the URL identifier.
This should only be used for text files. Results are unpredictable and probably undesirable when working with binary files.
ftp: ftp://userid:password@host:port/file
file: file:// (file name starting with / indicating root)
address
- URL of desired contentsIOException
public static void sendBytes(URL address, byte[] data) throws IOException
It appears that you can write to protocol ftp but not to protocol file.
address
- URL of destinationdata
- Byte array to be sentIOException
SocketTimeoutException
URLConnection
public static void sendBytes(URL address, File data) throws IOException
address
- URL of destinationdata
- File object containing data to be sentIOException
public static void sendBytes(File output, InputStream input)
output
- File object for output.input
- InputStream for input.public static void getBytes(URL url, File file) throws IOException
In case of failure, the algorithm retries with increased values of connectTimeout and readTimeout.
This modification may eventually be added to the other methods.
url
- Location of data to be readfile
- File where data is to be writtenIOException
URLConnection.setReadTimeout(int)
,
URLConnection.setConnectTimeout(int)
public static void getBytes(File input, OutputStream output) throws IOException
input
- File containing data to be writtenoutput
- OutputStream object where data is to be writtenIOException
File
public static void getBytes(URL url, OutputStream output) throws IOException
url
- URL of item to be copiedoutput
- OutputStream object where data is to be writtenIOException
public static void copyFile(File input, File output) throws IOException
input
- File object for file to be copiedoutput
- File object for copy of file to be createdIOException
FileOutputStream
public static void copyFile(String inputFileName, String outputFileName) throws IOException
inputFileName
- Name of file to be copiedoutputFileName
- Name of copy of file to be createdIOException
TestCopyFile
public static void fixDirs(File newFile) throws IOException
newFile
- File object to be createdIOException
File
public static void fixDirs(String name) throws IOException
name
- Name of file or directory to be createdIOException
File
public static void showProperties(URLConnection connection)
This is intended for diagnostic purposes.
connection
- Connection objectpublic static void main(String[] args)
args
- Names of original file and copy to be createdpublic static String[] lineSplitterSpaces(String input)
This method can be used as an example of processing regular expressions in Java.
input
- Line to be split into tokenspublic static String[] lineSplitterCommas(String input)
The space is suffixed to the input parameter because the system doesn't seem to recognize a comma as the last character in the string as signifying a new token.
input
- Line to be split into tokenspublic static void thumbnail(String inputFile, String outputFile, int sizeX, int sizeY) throws IOException
inputFile
- Name of file to be readoutputFile
- Name of file to be generatedsizeX
- Width of generated image in pixelssizeY
- Height of generated image in pixelsIOException
BufferedImage
,
ImageIO
,
RenderedImage
public static void thumbnail(File input, File output, int sizeX, int sizeY) throws IOException
input
- file to be readoutput
- file to be generatedsizeX
- Width of generated image in pixelssizeY
- Height of generated image in pixelsIOException
BufferedImage
,
ImageIO
,
RenderedImage
public static void thumbnail(String input, String output, int size) throws IOException
input
- Input fileoutput
- Output filesize
- Height and width of output image in pixelsIOException
public static void thumbnail(String input, String output) throws IOException
input
- Input fileoutput
- Output fileIOException