cn.apiclub.captcha.util.FileUtil Maven / Gradle / Ivy
The newest version!
package cn.apiclub.captcha.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import cn.apiclub.captcha.audio.Sample;
public class FileUtil {
/**
* Get a file resource and return it as an InputStream. Intended primarily
* to read in binary files which are contained in a jar.
*
* @param filename
* @return An @{link InputStream} to the file
*/
public static final InputStream readResource(String filename) {
InputStream jarIs = FileUtil.class.getResourceAsStream(filename);
if (jarIs == null) {
throw new RuntimeException(new FileNotFoundException("File '"
+ filename + "' not found."));
}
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
byte[] data = new byte[16384];
int nRead;
try {
while ((nRead = jarIs.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, nRead);
}
buffer.flush();
jarIs.close();
} catch (IOException e) {
e.printStackTrace();
}
return new ByteArrayInputStream(buffer.toByteArray());
}
public static final Sample readSample(String filename) {
InputStream is = readResource(filename);
return new Sample(is);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy