# Class IOUtils

java.lang.Object
uk.ac.starlink.util.IOUtils

public class IOUtils extends Object
Provides static methods which do miscellaneous input/output tasks.

Author:
  Mark Taylor

## Method Summary

Modifier and Type
Method
Description
`static void`
`copy(InputStream in, OutputStream out)`
Copies all the bytes from a given input stream to a given output stream.
`static byte[]`
`getLineSeparatorBytes()`
Returns the platform's line separator as a byte array given the platform's default encoding.
`static String`
`getResourceContents(Class<?> clazz, String name, Level level)`
Reads a static resource and returns the contents as a string.
`static void`
`println(OutputStream out, String line)`
Writes a string to an output stream followed by a new line.
`static byte[]`
`readBytes(InputStream in, int maxLeng)`
Reads a number of bytes from a stream.
`static void`
`skip(InputStream strm, long nskip)`
Skips over a number of bytes in an `InputStream` This is implemented using `InputStream.skip(long)` but differs from it in that it guarantees to skip the bytes as specified, or to throw an exception.
`static void`
`skipBytes(DataInput strm, long nskip)`
Skips over a number of bytes in a `DataInput`.

### Methods inherited from class Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Method Details

### skipBytes

public static void skipBytes(DataInput strm, long nskip) throws IOException
Skips over a number of bytes in a `DataInput`. This is implemented using `DataInput.skipBytes(int)` but differs from it in that it guarantees to skip the bytes as specified, or to throw an exception.

Parameters:
  `strm` - the stream to skip through
  `nskip` - the number of bytes to skip
Throws:
  `EOFException` - if the end of file is reached
  `IOException` - if an I/O error occurs
  `IllegalArgumentException` - if `nskip<0`

### skip

public static void skip(InputStream strm, long nskip) throws IOException
Skips over a number of bytes in an `InputStream` This is implemented using `InputStream.skip(long)` but differs from it in that it guarantees to skip the bytes as specified, or to throw an exception.

Parameters:
  `strm` - the stream to skip through
  `nskip` - the number of bytes to skip
Throws:
  `EOFException` - if the end of file is reached
  `IOException` - if an I/O error occurs
  `IllegalArgumentException` - if `nskip<0`

### getResourceContents

public static String getResourceContents(Class<?> clazz, String name, Level level)
Reads a static resource and returns the contents as a string. The resource is read using `clazz.getResourceAsStream(name)` and is assumed to have ASCII content. The result is cached so that subsequent calls will return the same value. If it can't be read, "?" is returned, and a message is written through the logging system at the requested level. This is intended for short files such as version strings.

Parameters:
  `clazz` - class defining relative location of resource
  `name` - resource name relative to `clazz`
  `level` - logging level for failure; if null a default value is used (currently WARNING)
Returns:
  resource content string
See Also:
  - `Class.getResourceAsStream(String)`

### println

public static void println(OutputStream out, String line) throws IOException
Writes a string to an output stream followed by a new line. Unlike `PrintStream.println()`, an IOException may be thrown.

Parameters:
  `out` - destination stream
  `line` - line to write
Throws:
  `IOException`

### getLineSeparatorBytes

public static byte[] getLineSeparatorBytes()
Returns the platform's line separator as a byte array given the platform's default encoding. May or may not be equal to {'\n'}.

Returns:
  line separator byte sequence

### copy

public static void copy(InputStream in, OutputStream out) throws IOException
Copies all the bytes from a given input stream to a given output stream. Neither stream is closed following the copy.

Parameters:
  `in` - source
  `out` - destination
Throws:
  `IOException`

### readBytes

public static byte[] readBytes(InputStream in, int maxLeng) throws IOException
Reads a number of bytes from a stream. The specified number of bytes or the whole of the file is read, whichever is shorter.

Parameters:
  `in` - input stream
  `maxLeng` - maximum number of bytes to read
Returns:
  buffer of bytes containing `maxLeng` bytes read from `in`, or fewer if the stream ended early
Throws:
  `IOException`