Reading a binary input stream into a single byte array in Java

The simplest approach IMO is to use Guava and its ByteStreams class: byte[] bytes = ByteStreams.toByteArray(in); Or for a file: byte[] bytes = Files.toByteArray(file); Alternatively (if you didn’t want to use Guava), you could create a ByteArrayOutputStream, and repeatedly read into a byte array and write into the ByteArrayOutputStream (letting that handle resizing), then call … Read more

How to get information of an APK file in the file system (not just installed ones) without using File or file-path?

OK I think I found a way using the Android framework (someone on reddit gave me this solution), to use file-path and use it, but it’s not perfect at all. Some notes: Not as direct as before. Good thing is that it might also be possible to handle even files that are outside of the … Read more

Exception Handling and Opening a File?

http://en.cppreference.com/w/cpp/io/basic_ios/exceptions Also read this answer 11085151 which references this article // ios::exceptions #include <iostream> #include <fstream> using namespace std; void do_something_with(char ch) {} // Process the character int main () { ifstream file; file.exceptions ( ifstream::badbit ); // No need to check failbit try { file.open (“test.txt”); char ch; while (file.get(ch)) do_something_with(ch); // for line-oriented … Read more

Do I need to close a ByteArrayInputStream?

You don’t have to close ByteArrayInputStream, the moment it is not referenced by any variable, garbage collector will release the stream and somebytes (of course assuming they aren’t referenced somewhere else). However it is always a good practice to close every stream, in fact, maybe the implementation creating the stream will change in the future … Read more

Convert InputStream to JSONObject

Since you’re already using Google’s Json-Simple library, you can parse the json from an InputStream like this: InputStream inputStream = … //Read from a file, or a HttpRequest, or whatever. JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject)jsonParser.parse( new InputStreamReader(inputStream, “UTF-8”));

What is the difference between Java’s BufferedReader and InputStreamReader classes?

BufferedReader is a wrapper for both “InputStreamReader/FileReader”, which buffers the information each time a native I/O is called. You can imagine the efficiency difference with reading a character(or bytes) vis-a-vis reading a large no. of characters in one go(or bytes). With BufferedReader, if you wish to read single character, it will store the contents to … Read more

Wrapping a ByteBuffer with an InputStream

There seem to be some bugs with the implementation referred to by Thilo, and also copy and pasted on other sites verbatim: ByteBufferBackedInputStream.read() returns a sign extended int representation of the byte it reads, which is wrong (value should be in range [-1..255]) ByteBufferBackedInputStream.read(byte[], int, int) does not return -1 when there are no bytes … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)