public void testInputStreamToByteArray() throws IOException {
byte[] expecteds = LOREM_IPSUM.getBytes("UTF-8");
byte[] actuals = org.apache.commons.io.IOUtils.toByteArray(new StringReader(LOREM_IPSUM), "UTF-8");
assertArrayEquals(expecteds, actuals);
}
public void givenUsingCommonsIO_whenConvertingAnInputStreamToAByteArray_thenCorrect()
throws IOException {
ByteArrayInputStream initialStream = new ByteArrayInputStream(new byte[] { 0, 1, 2 });
byte[] targetArray = IOUtils.toByteArray(initialStream);
}
public class StreamToByteArray2IOUtils {
public static void main(String args[]) throws IOException{
File file = new File("data");
FileInputStream fis = new FileInputStream(file);
byte [] byteArray = IOUtils.toByteArray(fis);
String s = new String(byteArray);
System.out.println("Contents of the byte stream are :: "+ s);
}
}
class Util
{
public static byte[] toByteArray(InputStream in) throws IOException {
byte[] bytes = IOUtils.toByteArray(in);
return bytes;
}
public static void main(String[] args) throws IOException
{
// input stream
InputStream in = new ByteArrayInputStream("Techie Delight"
.getBytes(StandardCharsets.UTF_8));
// byte array
byte[] bytes = toByteArray(in);
System.out.println(new String(bytes));
}
}
//Using FileUtils.readFileToByteArray()
byte[] org.apache.commons.io.FileUtils.readFileToByteArray(File file)
//Using IOUtils.toByteArray
byte[] org.apache.commons.io.IOUtils.toByteArray(InputStream input)
public static final byte[] get(String url, Map<String, String> headers) {
try {
URLConnection conn = new URL(url).openConnection();
if (headers != null) {
for (Map.Entry<String, String> entry : headers.entrySet()) {
conn.setRequestProperty(entry.getKey(), entry.getValue());
}
}
InputStream is = conn.getInputStream();
byte[] result = IOUtils.toByteArray(is);
is.close();
List<String> header = conn.getHeaderFields().get("Content-Disposition");
if (header != null && header.size() > 0) {
headers.put("Content-Disposition", header.get(0));
}
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public synchronized byte[] getFlowContent(final String bucketId, final String flowId, final int version) throws FlowPersistenceException {
final File snapshotFile = getSnapshotFile(bucketId, flowId, version);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Retrieving snapshot with filename {}", new Object[] {snapshotFile.getAbsolutePath()});
}
if (!snapshotFile.exists()) {
return null;
}
try (final InputStream in = new FileInputStream(snapshotFile)){
return IOUtils.toByteArray(in);
} catch (IOException e) {
throw new FlowPersistenceException("Error reading snapshot file: " + snapshotFile.getAbsolutePath(), e);
}
}
public Map<String, String> getReproguardMapping(String jarPath) {
Map<String, String> renameMap = new HashMap<>();
try {
JarFile file = new JarFile(new File(jarPath));
Enumeration<JarEntry> enumeration = file.entries();
while (enumeration.hasMoreElements()) {
JarEntry jarEntry = enumeration.nextElement();
InputStream inputStream = file.getInputStream(jarEntry);
String entryName = jarEntry.getName();
String className;
byte[] sourceClassBytes = IOUtils.toByteArray(inputStream);
if (entryName.endsWith(".class")) {
className = Utils.path2Classname(entryName);
String newClassname = getReproguardClassname(className);
TextFileWritter.getDefaultWritter().println(className + (newClassname != null ? " -> " + newClassname : ""));
//analyzeClassNames(className, sourceClassBytes);
}
}
} catch (IOException e) {
e.printStackTrace();
}
TextFileWritter.getDefaultWritter().close();
return renameMap;
}
public void openFile(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name=req.getParameter("name");
name=decode(name);
ProcessProvider targetProvider=ProcessProviderUtils.getProcessProvider(name);
if(targetProvider==null){
throw new RuntimeException("Unsupport file : "+name);
}
InputStream inputStream=targetProvider.loadProcess(name);
try{
byte[] bytes=IOUtils.toByteArray(inputStream);
ProcessDefinition process=ProcessParser.parseProcess(bytes, 0, true);
writeObjectToJson(resp, process);
}catch(Exception ex){
throw new RuntimeException(ex);
}finally{
IOUtils.closeQuietly(inputStream);
}
}
public ExtClassLoader() throws IOException {
super(Thread.currentThread().getContextClassLoader());
{
byte[] bytes;
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("kotlin/ResponseKotlin2.clazz");
bytes = IOUtils.toByteArray(is);
is.close();
super.defineClass("ResponseKotlin2", bytes, 0, bytes.length);
}
}