Created
October 2, 2008 15:54
-
-
Save ctran/14376 to your computer and use it in GitHub Desktop.
IOUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.FileInputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import java.io.FileOutputStream; | |
import java.util.zip.GZIPOutputStream; | |
import java.util.zip.GZIPInputStream; | |
public class IOUtils { | |
static final Logger logger = Logger.getLogger(IOUtils.class); | |
static final int BUFFER_SIZE = 512; | |
/** | |
* Read the content of the resource (from classpath) and return as string | |
* @param resource | |
* @throws IOException | |
*/ | |
public static String getResourceAsString(String resource) throws IOException { | |
return fgets(IOUtils.class.getResourceAsStream(resource)); | |
} | |
/** | |
* Read the content of the given input stream into a string | |
*/ | |
public static String fgets(InputStream in) throws IOException { | |
ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
copy(in, out); | |
return out.toString(); | |
} | |
/** | |
* Read the content of the given file into a string | |
*/ | |
public static String fgets(String fileName) throws IOException { | |
FileInputStream in = new FileInputStream(fileName); | |
try { | |
return fgets(in); | |
} finally { | |
in.close(); | |
} | |
} | |
/** | |
* Write the given data string to the output stream. | |
* @param out | |
* @param data | |
* @throws IOException | |
*/ | |
public static void fputs(OutputStream out, String data) throws IOException { | |
ByteArrayInputStream in = new ByteArrayInputStream(data.getBytes()); | |
copy(in, out); | |
} | |
/** | |
* Save the given data string as file | |
* @param fileName name of the dest file | |
* @param data the data to write | |
*/ | |
public static void fputs(String fileName, String data) throws IOException { | |
FileOutputStream out = new FileOutputStream(fileName); | |
try { | |
fputs(out, data); | |
} finally { | |
out.close(); | |
} | |
} | |
/** | |
* Copy the content of one stream to another. Both streams are not closed | |
* so it's the responsibility of the caller. | |
*/ | |
public static void copy(InputStream in, OutputStream out) throws IOException { | |
byte[] bytes = new byte[BUFFER_SIZE]; | |
for (int bytesRead; (bytesRead = in.read(bytes)) > 0;) { | |
out.write(bytes, 0, bytesRead); | |
} | |
} | |
/** | |
* Close the given stream and swallowed all exceptions. | |
*/ | |
public static void close(OutputStream output) { | |
if (output != null) { | |
try { | |
output.close(); | |
} catch (IOException ex) { | |
logger.error("Unable to close output stream", ex); | |
} | |
} | |
} | |
/** | |
* Close the given stream and swallowed all exceptions. | |
*/ | |
public static void close(InputStream input) { | |
if (input != null) { | |
try { | |
input.close(); | |
} catch (IOException ex) { | |
logger.error("Unable to close input stream", ex); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment