Package ucar.nc2.util
Class IO
- java.lang.Object
-
- ucar.nc2.util.IO
-
public class IO extends Object
Input/Output static utilities.
-
-
Field Summary
Fields Modifier and Type Field Description static int
default_file_buffersize
-
Constructor Summary
Constructors Constructor Description IO()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static long
appendToFile(InputStream in, String fileOutName)
static long
copy(InputStream in, OutputStream out)
Copy all bytes from in to out.static void
copy2File(byte[] src, String fileOut)
copy bytes to Filestatic long
copy2null(InputStream in, int buffersize)
copy all bytes from in and throw them away.static long
copy2null(FileChannel in, int buffersize)
copy all bytes from in and throw them away.static long
copyB(InputStream in, OutputStream out, int bufferSize)
copy all bytes from in to out, specify buffer sizestatic long
copyBuffered(InputStream in, OutputStream out, int buffer_size)
Copy all bytes from in to out, setting buffer size.static void
copyDirTree(String fromDirName, String toDirName)
Copy an entire directory tree.static void
copyFile(File fileIn, File fileOut)
copy one file to another.static void
copyFile(String fileInName, OutputStream out)
copy file to output streamstatic void
copyFile(String fileInName, String fileOutName)
copy one file to another.static void
copyFileB(File fileIn, OutputStream out, int bufferSize)
copy file to output stream, specify internal buffer sizestatic void
copyMaxBytes(InputStream in, OutputStream out, int maxBytes)
Copy up to maxBytes bytes from in to out.static long
copyRafB(RandomAccessFile raf, long offset, long length, OutputStream out, byte[] buffer)
Copy part of a RandomAccessFile to output stream, specify internal buffer sizestatic InputStream
getFileResource(String resourcePath)
Open a resource as a Stream.static String
readContents(InputStream is)
Read the contents from the inputStream and place into a String, with any error messages put in the return String.static String
readContents(InputStream is, Charset charset)
Read the contents from the inputStream and place into a String, with any error messages put in the return String.static byte[]
readContentsToByteArray(InputStream is)
Read the contents from the inputStream and place into a byte array.static String
readFile(String filename)
Read 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 void
writeContents(String contents, OutputStream os)
Wite the contents from the String to a Stream,static void
writeToFile(byte[] contents, File file)
Write byte[] contents to a file.static long
writeToFile(InputStream in, String fileOutName)
copy input stream to file.static void
writeToFile(String contents, File file)
Write String contents to a file, using UTF-8 encoding.static void
writeToFile(String contents, String fileOutName)
Write contents to a file, using UTF-8 encoding.
-
-
-
Field Detail
-
default_file_buffersize
public static final int default_file_buffersize
- See Also:
- Constant Field Values
-
-
Method Detail
-
getFileResource
public static InputStream getFileResource(String resourcePath) throws FileNotFoundException
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 FileNotFoundException on failure
- Throws:
FileNotFoundException
-
copy
public static long copy(InputStream in, OutputStream out) throws IOException
Copy all bytes from in to out.- Parameters:
in
- InputStreamout
- OutputStream- Returns:
- number of bytes copied
- Throws:
IOException
-
copyBuffered
public static long copyBuffered(InputStream in, OutputStream out, int buffer_size) throws IOException
Copy all bytes from in to out, setting buffer size.- Parameters:
in
- InputStreamout
- OutputStreambuffer_size
- size of buffer to read through.- Returns:
- number of bytes copied
- Throws:
IOException
-
copy2null
public static long copy2null(InputStream in, int buffersize) throws IOException
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
-
copy2null
public static long copy2null(FileChannel in, int buffersize) throws IOException
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
-
copyB
public static long copyB(InputStream in, OutputStream out, int bufferSize) throws IOException
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
-
copyMaxBytes
public static void copyMaxBytes(InputStream in, OutputStream out, int maxBytes) throws IOException
Copy up to maxBytes bytes from in to out.- Parameters:
in
- InputStreamout
- OutputStreammaxBytes
- number of bytes to copy- Throws:
IOException
- on io error
-
readContents
public static String readContents(InputStream is) throws IOException
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
public static String readContents(InputStream is, Charset charset) throws IOException
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
public static byte[] readContentsToByteArray(InputStream is) throws IOException
Read the contents from the inputStream and place into a byte array.- Parameters:
is
- the inputStream to read from.- Returns:
- byte[] holding the contents, or an error message.
- Throws:
IOException
- on io error
-
writeContents
public static void writeContents(String contents, OutputStream os) throws IOException
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
public static void copyFile(String fileInName, String fileOutName) throws IOException
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
public static void copyFile(File fileIn, File fileOut) throws IOException
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
public static void copy2File(byte[] src, String fileOut) throws IOException
copy bytes to File- Parameters:
src
- sourcefileOut
- copy to this file- Throws:
IOException
- on io error
-
copyFile
public static void copyFile(String fileInName, OutputStream out) throws IOException
copy file to output stream- Parameters:
fileInName
- open this fileout
- copy here- Throws:
IOException
- on io error
-
copyFileB
public static void copyFileB(File fileIn, OutputStream out, int bufferSize) throws IOException
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
-
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
public static void copyDirTree(String fromDirName, String toDirName) throws IOException
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
public static byte[] readFileToByteArray(String filename) throws IOException
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
public static String readFile(String filename) throws IOException
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
public static void writeToFile(String contents, File file) throws IOException
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
public static void writeToFile(byte[] contents, File file) throws IOException
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
public static void writeToFile(String contents, String fileOutName) throws IOException
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
public static long writeToFile(InputStream in, String fileOutName) throws IOException
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
public static long appendToFile(InputStream in, String fileOutName) throws IOException
- Throws:
IOException
-
-