Package ucar.nc2.util
Class IO
java.lang.Object
ucar.nc2.util.IO
Input/Output static utilities.
- See Also:
-
- "http://stackoverflow.com/questions/12552863/correct-idiom-for-managing-multiple-chained-resources-in-try-with-resources-bloc"
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intstatic final int -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic longappendToFile(InputStream in, String fileOutName) static longcopy(InputStream in, OutputStream out) copy all bytes from in to out.static voidcopy(InputStream in, OutputStream out, int n) Deprecated.use copyMaxBytes()static voidcopy file to output streamstatic longcopy2null(InputStream in, int buffersize) copy all bytes from in and throw them away.static longcopy2null(FileChannel in, int buffersize) copy all bytes from in and throw them away.static longcopyB(InputStream in, OutputStream out, int bufferSize) copy all bytes from in to out, specify buffer sizestatic voidcopyDirTree(String fromDirName, String toDirName) Copy an entire directory tree.static voidcopy one file to another.static voidcopyFile(File fileIn, OutputStream out) copy file to output streamstatic voidcopyFile(String fileInName, OutputStream out) copy file to output streamstatic voidcopy one file to another.static voidcopyFileB(File fileIn, OutputStream out, int bufferSize) copy file to output stream, specify internal buffer sizestatic voidcopyFileWithChannels(File fileIn, WritableByteChannel out) static voidcopyMaxBytes(InputStream in, OutputStream out, int maxBytes) Copy up to maxBytes bytes from in to out.static longcopyRafB(RandomAccessFile raf, long offset, long length, OutputStream out) Copy part of a RandomAccessFile to output streamstatic longcopyRafB(RandomAccessFile raf, long offset, long length, OutputStream out, byte[] buffer) Copy part of a RandomAccessFile to output stream, specify internal buffer sizestatic longcopyUrlB(String urlString, OutputStream out, int bufferSize) copy contents of URL to output stream, specify internal buffer size.static InputStreamgetFileResource(String resourcePath) Open a resource as a Stream.static InputStreamgetInputStreamFromUrl(String urlString) get input stream from URLstatic IO.HttpResultuse HTTP PUT to send the contents to the named URL.static StringRead the contents from the inputStream and place into a String, with any error messages put in the return String.static StringreadContents(InputStream is, String charset) Read the contents from the inputStream and place into a String, with any error messages put in the return String.static byte[]Read the contents from the inputStream and place into a byte array, with any error messages put in the return String.static StringRead the contents from the named file and place into a String, assuming UTF-8 encoding.static byte[]readFileToByteArray(String filename) Read the file and place contents into a byte array, with any error messages put in the return String.static StringreadURLcontents(String urlString) Read the contents from the named URL and place into a String, with any error messages put in the return String.static byte[]readURLContentsToByteArray(String urlString) Read the contents from the given URL and place into a byte array, with any error messages put in the return String.static StringreadURLcontentsWithException(String urlString) Read the contents from the named URL and place into a String.static StringreadURLtoFile(String urlString, File file) read the contents from the named URL, write to a file.static StringreadURLtoFileWithExceptions(String urlString, File file) read the contents from the named URL, write to a file.static StringreadURLtoFileWithExceptions(String urlString, File file, int buffer_size) read the contents from the named URL, write to a file.static longtouch(InputStream in, int buffersize) static longtouch(FileChannel in, int buffersize) static voidwriteContents(String contents, OutputStream os) Wite the contents from the String to a Stream,static voidwriteToFile(byte[] contents, File file) Write byte[] contents to a file.static longwriteToFile(InputStream in, String fileOutName) copy input stream to file.static voidwriteToFile(String contents, File file) Write String contents to a file, using UTF-8 encoding.static voidwriteToFile(String contents, String fileOutName) Write contents to a file, using UTF-8 encoding.
-
Field Details
-
default_file_buffersize
public static final int default_file_buffersize- See Also:
-
default_socket_buffersize
public static final int default_socket_buffersize- See Also:
-
-
Constructor Details
-
IO
public IO()
-
-
Method Details
-
getFileResource
Open a resource as a Stream. First try ClassLoader.getResourceAsStream(). If that fails, try a plain old FileInputStream().- Parameters:
resourcePath- name of file path (use forward slashes!)- Returns:
- InputStream or null on failure
-
copy
copy all bytes from in to out.- Parameters:
in- InputStreamout- OutputStream- Returns:
- number of bytes copied
- Throws:
IOException- on io error
-
copy2null
copy all bytes from in and throw them away.- Parameters:
in- InputStreambuffersize- size of buffer to use, if -1 uses default value (9200)- Returns:
- number of bytes copied
- Throws:
IOException- on io error
-
touch
- Throws:
IOException
-
copy2null
copy all bytes from in and throw them away.- Parameters:
in- FileChannelbuffersize- size of buffer to use, if -1 uses default value (9200)- Returns:
- number of bytes copied
- Throws:
IOException- on io error
-
touch
- Throws:
IOException
-
copyB
copy all bytes from in to out, specify buffer size- Parameters:
in- InputStreamout- OutputStreambufferSize- : internal buffer size.- Returns:
- number of bytes copied
- Throws:
IOException- on io error
-
copy
Deprecated.use copyMaxBytes()- Throws:
IOException
-
copyMaxBytes
Copy up to maxBytes bytes from in to out.- Parameters:
in- InputStreamout- OutputStreammaxBytes- number of bytes to copy- Throws:
IOException- on io error
-
readContents
Read the contents from the inputStream and place into a String, with any error messages put in the return String. Assume UTF-8 encoding.- Parameters:
is- the inputStream to read from.- Returns:
- String holding the contents, or an error message.
- Throws:
IOException- on io error
-
readContents
Read the contents from the inputStream and place into a String, with any error messages put in the return String.- Parameters:
is- the inputStream to read from.- Returns:
- String holding the contents, or an error message.
- Throws:
IOException- on io error
-
readContentsToByteArray
Read the contents from the inputStream and place into a byte array, with any error messages put in the return String.- Parameters:
is- the inputStream to read from.- Returns:
- byte[] holding the contents, or an error message.
- Throws:
IOException- on io error
-
writeContents
Wite the contents from the String to a Stream,- Parameters:
contents- String holding the contents.os- write to this OutputStream- Throws:
IOException- on io error
-
copyFile
copy one file to another.- Parameters:
fileInName- copy from this file, which must exist.fileOutName- copy to this file, which is overrwritten if already exists.- Throws:
IOException- on io error
-
copyFile
copy one file to another.- Parameters:
fileIn- copy from this file, which must exist.fileOut- copy to this file, which is overrwritten if already exists.- Throws:
IOException- on io error
-
copy2File
copy file to output stream- Parameters:
src- sourcefileOut- copy to this file- Throws:
IOException- on io error
-
copyFile
copy file to output stream- Parameters:
fileInName- open this fileout- copy here- Throws:
IOException- on io error
-
copyFile
copy file to output stream- Parameters:
fileIn- copy this fileout- copy here- Throws:
IOException- on io error
-
copyFileB
copy file to output stream, specify internal buffer size- Parameters:
fileIn- copy this fileout- copy to this streambufferSize- internal buffer size.- Throws:
IOException- on io error
-
copyFileWithChannels
- Throws:
IOException
-
copyRafB
public static long copyRafB(RandomAccessFile raf, long offset, long length, OutputStream out) throws IOException Copy part of a RandomAccessFile to output stream- Parameters:
raf- copy this fileoffset- start here (byte offset)length- number of bytes to copyout- copy to this stream- Returns:
- number of bytes copied
- Throws:
IOException- on io error
-
copyRafB
public static long copyRafB(RandomAccessFile raf, long offset, long length, OutputStream out, byte[] buffer) throws IOException Copy part of a RandomAccessFile to output stream, specify internal buffer size- Parameters:
raf- copy this fileoffset- start here (byte offset)length- number of bytes to copyout- copy to this streambuffer- use this buffer.- Returns:
- number of bytes copied
- Throws:
IOException- on io error
-
copyDirTree
Copy an entire directory tree.- Parameters:
fromDirName- from this directory (do nothing if not exist)toDirName- to this directory (will create if not exist)- Throws:
IOException- on io error
-
readFileToByteArray
Read the file and place contents into a byte array, with any error messages put in the return String.- Parameters:
filename- the file to read from.- Returns:
- byte[] holding the contents, or an error message.
- Throws:
IOException- on io error
-
readFile
Read the contents from the named file and place into a String, assuming UTF-8 encoding.- Parameters:
filename- the URL to read from.- Returns:
- String holding the file contents
- Throws:
IOException- on io error
-
writeToFile
Write String contents to a file, using UTF-8 encoding.- Parameters:
contents- String holding the contentsfile- write to this file (overwrite if exists)- Throws:
IOException- on io error
-
writeToFile
Write byte[] contents to a file.- Parameters:
contents- String holding the contentsfile- write to this file (overwrite if exists)- Throws:
IOException- on io error
-
writeToFile
Write contents to a file, using UTF-8 encoding.- Parameters:
contents- String holding the contentsfileOutName- write to this file (overwrite if exists)- Throws:
IOException- on io error
-
writeToFile
copy input stream to file. close input stream when done.- Parameters:
in- copy from herefileOutName- open this file (overwrite) and copy to it.- Returns:
- number of bytes copied
- Throws:
IOException- on io error
-
appendToFile
- Throws:
IOException
-
copyUrlB
copy contents of URL to output stream, specify internal buffer size. request gzip encoding- Parameters:
urlString- copy the contents of this URLout- copy to this stream. If null, throw bytes awaybufferSize- internal buffer size.- Returns:
- number of bytes copied
- Throws:
IOException- on io error
-
getInputStreamFromUrl
get input stream from URL- Parameters:
urlString- URL- Returns:
- input stream, unzipped if needed
- Throws:
IOException- on io error
-
readURLtoFile
read the contents from the named URL, write to a file.- Parameters:
urlString- the URL to read from.file- write to this file- Returns:
- status or error message.
-
readURLContentsToByteArray
Read the contents from the given URL and place into a byte array, with any error messages put in the return String.- Parameters:
urlString- read from this URL.- Returns:
- byte[] holding the contents, or an error message.
- Throws:
IOException- on io error
-
readURLtoFileWithExceptions
read the contents from the named URL, write to a file.- Parameters:
urlString- the URL to read from.file- write to this file- Returns:
- status or error message.
- Throws:
IOException- if failure
-
readURLtoFileWithExceptions
public static String readURLtoFileWithExceptions(String urlString, File file, int buffer_size) throws IOException read the contents from the named URL, write to a file.- Parameters:
urlString- the URL to read from.file- write to this filebuffer_size- read/write in this size chunks- Returns:
- status or error message.
- Throws:
IOException- if failure
-
readURLcontentsWithException
Read the contents from the named URL and place into a String.- Parameters:
urlString- the URL to read from.- Returns:
- String holding the contents.
- Throws:
IOException- if fails
-
readURLcontents
Read the contents from the named URL and place into a String, with any error messages put in the return String.- Parameters:
urlString- the URL to read from.- Returns:
- String holding the contents, or an error message.
-
putToURL
use HTTP PUT to send the contents to the named URL.- Parameters:
urlString- the URL to read from. must be http:contents- String holding the contents- Returns:
- a Result object; generally 0 <= code <=400 is ok
-