As in BufferedReader returns the String value after readLine(), and StringBuilder is also return the same after append(line). Then why we use both together ?
HttpEntity he = res.getEntity();
InputStream is= he.getContent();
InputStreamReader ir= new InputStreamReader(is);
BufferedReader br= new BufferedReader(ir);
// StringBuilder sb = new StringBuilder();
String line=br.readLine();
// sb.append(line);
JSONObject jobj= new JSONObject(line);
It is used to concatenate all the line from buffered reader..
I am correcting your code
String line;
while((line=br.readLine())!= null){
sb.append(line);
}
Even now if you don't see any difference, It means your input stream has got a single line
They are unrelated classes.
BufferedReader buffers the IO operations making it more efficient and faster.
StringBuilder allows you to create a String in memory more efficient way (Strings do not have to be created on heap.
Related
I got it from a page
Android AsyncTask method that I dont know how to solve
but i am not sure how it work completly, if someone can explain me what is the while for and This part "iso-8859-1"
i understood that the 8 is for the number of characters but i could be wrong
static InputStream is = null;
static String json = "";
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
Your code basically reads from an inputstream obtained from the httpentity, puts that into a StringBuilder and converts that into a json finally.
For understanding the api codes, javadoc is your friend.
Here is what I found in BufferredReader javadoc
public BufferedReader(Reader in,
int sz)
Creates a buffering character-input stream that uses an input buffer of the specified size.
Parameters:** in - A Reader sz - Input-buffer size
Throws: IllegalArgumentException - If sz is <=0
http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html
As a reader, InputStreamReader is used in your code. Here is the relevant javadoc for the InputStreamReader
public InputStreamReader(InputStream in,Charset cs) Creates an
InputStreamReader that uses the given charset.
Parameters:
in - An
InputStream cs - A charset
http://docs.oracle.com/javase/7/docs/api/java/io/InputStreamReader.html#InputStreamReader(java.io.InputStream, java.nio.charset.Charset)
So "iso-8859-1" is the charset specified.
I have a ServerSocket in Java:
ServerSocket serverSocket = new ServerSocket(1000);
which accepts a clientSocket:
Socket clientSocket;
clientSocket = serverSocket.accept();
Up until now I was reading the input like this:
BufferedReader clientSocketInputStream = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
while ((inputLine = clientSocketInputStream.readLine()) != null){
String message = inputLine;
// Hack the computer connecting to this one after here
However if the text sent is something like
String stringToBeSent = "Hello\nHowareyou";
then I am in trouble. Because I need this text as it is. 2 different Strings do not help me.
How can I read it as it is?
Thanks.
Two options:
Put the multiple strings back together.
StringBuilder sb = new StringBuilder()
while ((inputLine = clientSocketInputStream.readLine()) != null)
{
String message = inputLine;
sb.append(message);
sb.append('\n');
}
String message = sb.toString();
Read an array of bytes instead of String, using a BufferedInputStream instead of a BufferedReader. Then smash the whole byte array into the String constructor with a valid charset. This will require you to know how many bytes the String will be.
You could simply read character by character. http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html#read()
instead of read line.
BufferedReader reads characters until it gets "\n", "\r" or "\r\n". You can read character by character but it does not change anything anyway as how would you determine which new line characater shows the real new line?
I have a String[] array and I need to convert it to InputStream.
I've seen Byte[] -> InputStream and String -> InputStream, but not this. Any tips?
You can construct a merged String with some separator and then to byte[] and then to ByteArrayInputStream.
Here's a way to convert a String to InputStream in Java.
Have a look at the following link: http://www.mkyong.com/java/how-to-convert-string-to-inputstream-in-java/
However, the difference in the code is that you should concatenate all the strings together in one string before converting.
String concatenatedString = ... convert your array
// convert String into InputStream
InputStream is = new ByteArrayInputStream(str.getBytes());
// read it with BufferedReader
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
There is still no single method call to do this but with Java 8 you can do it in a single line of code. Given an array of String objects:
String[] strings = {"string1", "string2", "string3"};
You can convert the String array to an InputStream using:
InputStream inputStream = new ByteArrayInputStream(String.join(System.lineSeparator(), Arrays.asList(strings)).getBytes(StandardCharsets.UTF_8));
I am reading from InputStreamReader but I only get the first 10,000 characters of the text that is supposed to come. Any idea what the problem may be? If there is no solution with this class, what are my alternatives?
I found this about InputStreamReader: "The buffer size is 8K." (http://developer.android.com/reference/java/io/InputStreamReader.html). Could this be the answer?
Any pointers really appreciated
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(
httpcon.getInputStream(),"utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
result = sb.toString();
8K buffer would mean 8000 bytes and since one character is one byte that would seem to make some sense as to your problem. But what is confusing is that you get 10,000 characters.
My current code needs to read foreign characters from the web, currently my solution works but it is very slow, since it read char by char using InputStreamReader. Is there anyway to speed it up and also get the job done?
// Pull content stream from response
HttpEntity entity = response.getEntity();
InputStream inputStream = entity.getContent();
StringBuilder contents = new StringBuilder();
int ch;
InputStreamReader isr = new InputStreamReader(inputStream, "gb2312");
// FileInputStream file = new InputStream(is);
while( (ch = isr.read()) != -1)
contents.append((char)ch);
String encode = isr.getEncoding();
return contents.toString();
Wrap your InputStreamReader with a BufferedReader
for the efficient reading of
characters, arrays, and lines.
InputStreamReader isr = new InputStreamReader(inputStream, "gb2312");
Reader reader = new BufferedReader(isr);