Skip to content

Instantly share code, notes, and snippets.

@Crydust
Last active November 15, 2016 10:14
Show Gist options
  • Save Crydust/fa8483545c900557dde2bcc9ecfd68e7 to your computer and use it in GitHub Desktop.
Save Crydust/fa8483545c900557dde2bcc9ecfd68e7 to your computer and use it in GitHub Desktop.
read Resource as String
package be.crydust.findweirdchars;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
/** @see http://stackoverflow.com/questions/309424/read-convert-an-inputstream-to-a-string */
public class ResoureAsStringHelper {
public static String readResourceAsString8a(Class<?> aClass, String name) {
try (Reader in = new BufferedReader(new InputStreamReader(aClass.getResourceAsStream(name), StandardCharsets.UTF_8));) {
StringWriter out = new StringWriter();
char[] buffer = new char[1024 * 4];
int n = 0;
while (-1 != (n = in.read(buffer))) {
out.write(buffer, 0, n);
}
return out.toString();
} catch (IOException ex) {
throw new RuntimeException("unexpected: read failed", ex);
}
}
public static String readResourceAsString8b(Class<?> aClass, String name) {
try (InputStream in = new BufferedInputStream(aClass.getResourceAsStream(name));) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024 * 4];
int length;
while ((length = in.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
return out.toString("UTF-8");
} catch (IOException ex) {
throw new RuntimeException("unexpected: read failed", ex);
}
}
public static String readResourceAsString6(Class<?> aClass, String name) {
StringWriter out = new StringWriter();
Reader in = null;
try {
in = new BufferedReader(new InputStreamReader(aClass.getResourceAsStream(name), "UTF-8"));
char[] buffer = new char[1024 * 4];
int n = 0;
while (-1 != (n = in.read(buffer))) {
out.write(buffer, 0, n);
}
} catch (UnsupportedEncodingException ex) {
throw new RuntimeException("impossible: UTF-8 is suported", ex);
} catch (IOException ex) {
throw new RuntimeException("unexpected: read failed", ex);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
throw new RuntimeException("unlikely: close failed", ex);
}
}
}
return out.toString();
}
public static String readResourceAsString6b(Class<?> aClass, String name) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = null;
try {
in = new BufferedInputStream(aClass.getResourceAsStream(name));
byte[] buffer = new byte[1024 * 4];
int length;
while ((length = in.read(buffer)) != -1) {
out.write(buffer, 0, length);
}
return out.toString("UTF-8");
} catch (IOException ex) {
throw new RuntimeException("unexpected: read failed", ex);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ex) {
throw new RuntimeException("unlikely: close failed", ex);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment