String from server is shorter than it needs to be - java

for some reason, when I take a string from my server to my app and Log it, the string is shorter than it needs to be.
I thought that it is happening because of the length of the string, but the whole string is 113137 characters long (and the limit is 10^32 -1).
The length of the string that returns to me is something like 4000.
Code:
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(IS));
StringBuilder stringBuilder = new StringBuilder();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
}
Log.d("Base64", stringBuilder.toString());

There is limit on log message length
#define LOGGER_ENTRY_MAX_LEN (4*1024)
#define LOGGER_ENTRY_MAX_PAYLOAD (LOGGER_ENTRY_MAX_LEN - sizeof(struct logger_entry))
Also look at following question to clarify things
Android - Set max length of logcat messages

Related

BufferedReader adds endlessly on readLine()

I'm trying to send test messages from Arduino via bluetooth by spamming "hello world!" every 100 ms.
Now, on android I've got an endless addition of "hello world!" to my StringBuilder and each debug iteration I have "hello world!\nhello world!\nhello world!\n..." etc.
What should I do?
As an idea I will spam a lot of Jsons from arduino to Android device. Do I have to make some special char sequences for dividers to break the while after each message from arduino? Or is there a simpler decision?
public static String toString(InputStream inputStream) throws IOException {
byte[] bs = new byte[inputStream.available()];
if (Utils.isZero(bs.length)) {
return "";
}
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while (r.ready()) {
line = r.readLine();
total.append(line).append('\n');
}
return total.toString();
}
You aren't checking for end of stream.
The correct way to write that read loop is:
while ((line = br.readLine()) != null)
{
total.append(line).append('\n');
}
But if the data is endless it doesn't make sense to even have a loop. Just return br.readLine(). It doesn't even make sense to have this method.
NB You don't need to allocate a byte array just to determine its length.

String comparison not breaking out of a while loop

I am trying these lines:
private String line;
private final String stopChr= "#";
BufferedReader in = new BufferedReader(new InputStreamReader(server.getInputStream()));
while ((line = in.readLine()) != null) {
tcpData = tcpData + line;
if(line.equals(stopChr)) break;
}
Why is the if statement not breaking out of the loop when # is present?
Most likely the line is not exactly "#" for example it might have a space after it. I suggest you look at what the line is in your debugger or in an editor to see exactly what characters the String has.
Try printing the following to help see what the string is actually.
System.out.println(Arrays.toString(line.toCharArray());
If you have trailing spaces you can drop these with trim
if (line.trim().equals(stopChar)) break;
If the string contains other characters, as in your example input $353323058181636,EV,D,T,567888.9,+12C,FFFFE000# (from your comment on #PeterLawrey's answer), use the following instead of String.equals:
if(line.contains(stopChr)) break;
If it specifically ends with the stop character, you can alternatively use:
if(line.endsWith(stopChr)) break;
The following code is working :
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = "";
String data = "";
while ((line = br.readLine()) != null) {
data += line;
if (line.contains("#"))
break;
}
Also, instead of contains() you can use endsWith() to check for end of file.
You make take help.
for getting everything before the #
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = "";
String data = "";
while (true)
{
line = br.readLine();
// break if terminated
if (line==null)
break;
// Check : for debugging only
System.err.println("LINE : "+line);
// break if #
if (line.contains("#"))
{
// Get first part, part after, we dont care
int first=line.indexOf('#');
data+=line.substring(0, first);
break;
}
else
data += line;
}
// See the result
System.out.println("DATA:"+data);
The problem solved. readLine() function need end of string character <CR>. Just replacing "#" to "\n" solved the problem. Thanks to all great team.
you will never get null if the inputstream is from socket. Instead, the readLine() method will block until you get new data.

How could a String contain all the StringBuilder result in Java

i'm writing a code that get a Json String from the google geocode api , i'm using a StringBuilder to build the String from the Input stream that i have , well the problem is when
the String that the StringBuilder Have is not the same that the Function StringBuilder.tostring(); returns ...
i have a code like this :
sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
Log.i("line",line);
}
this gonna show all the lines using Log.i("line",line); so well .. but when when i do :
sb.toString();
It only returns some of the String that i want ... any suggestions guys ?
Note : The Json Result of the API is so long . you can try with this one
https://maps.googleapis.com/maps/api/geocode/json?address=Hospital+Jijel
Writing to the log will truncate your string if it's too long, which in your case it probably is.
Write the contents of your StringBuilder to the screen or a file to see its full contents.

How to split a very long string

I have big file (about 30mb) and here the code I use to read data from the file
BufferedReader br = new BufferedReader(new FileReader(file));
try {
String line = br.readLine();
while (line != null) {
sb.append(line).append("\n");
line = br.readLine();
}
Then I need to split the content I read, so I use
String[] inst = sb.toString().split("GO");
The problem is that sometimes the sub-string is over the maximum String length and I can't get all the data inside the string. How can I get rid of this?
Thanks
Scanner s = new Scanner(input).useDelimiter("GO"); and use s.next()
WHY PART:- The erroneous result may be the outcome of non contiguous heap segment as the CMS collector doesn't de-fragment memory.
(It does not answer your how to solve part though).
You may opt for loading the whole string partwise, i.e using substring

Reading a file into a CharBuffer, then a StringBuilder or StringBuffer seems to leave out on parts of the file. Why?

The following code seems to only write a small part of the File in the StringBuilder - why?
Reader rdr = new BufferedReader(new InputStreamReader(new FileInputStream(...)));
StringBuilder buf = new StringBuilder();
CharBuffer cbuff = CharBuffer.allocate(1024);
while(rdr.read(cbuff) != -1){
buf.append(cbuff);
cbuff.clear();
}
rdr.close();
Some more information: The file is bigger than the CharBuffer, also i can see from the debugger that the charbuffer is indeed filled as expected. The only part that makes its way to the StringBuilder seems to be from somewhere in the middle of the file. I am using openJDK7.
I wonder why it would show such a behavior and how this can be fixed.
As Peter Lawrey mentioned, you need to call cbuff.flip() between the read and write. It seems that the append will read from the position of the buffer, which is at the end if we don't call cbuff.flip(). The reason why a part from somewhere in the middle is still written is because in the end, the buffer won't be completely filled, thus some "old" bytes will still be between the position in the buffer and the end of the buffer.
Mystery solved :-)
All those classes have been part of the JDK since 1.0. I doubt that any of them needs to be fixed.
Your code is a long way for the usual idiom. Was this intended as a learning exercise, one that's gone awry? Or did you really want to put this into an application?
Here's how I would expect to see those classes used:
public static final String NEWLINE = System.getProperty("line.separator");
public String readContents(File f) throws IOException {
StringBuilder builder = new StringBuilder(1024);
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(f));
String line;
while ((line = br.readLine()) != null) {
builder.append(line).append(NEWLINE);
}
} finally {
closeQuietly(br);
}
return builder.toString();
}

Categories