Speed up getting string from inputstream?

11 views
Skip to first unread message

Gareth Murfin

unread,
Apr 6, 2018, 7:31:46 AM4/6/18
to CodenameOne Discussions
I have a lot of info coming down from a server, im using the following method to convert from inputstream to string, but it can take many minutes, we have 52,000 records coming down (about 20 meg of text!).. Is there any way to optimise this method ?

private String readResponseX(InputStream input, int state)
    {
        StringBuffer sb = new StringBuffer();     
        try
        {
            int chars, i = 0;
            _("converting response to string");
            while ((chars = input.read()) != -1)
            {
                sb.append((char) chars);
            }
            _("done.");
            input.close();//////
        }
        catch (Exception e)
        {
             _("!!!!!!! WARNING !!!!!!!!!! readResponseX() warning error reading input steam!!"+e.getMessage());
             
        }   
        return sb.toString();
    }   

Steve Hannah

unread,
Apr 6, 2018, 7:57:20 AM4/6/18
to codenameone...@googlegroups.com
Use Util.readToString(input) instead,
or use InputStream.read(byte[]) instead of InputStream.read() so that you read a whole buffer at a time rather than one byte at a time.

Steve

--
You received this message because you are subscribed to the Google Groups "CodenameOne Discussions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to codenameone-discussions+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/codenameone-discussions.
To view this discussion on the web visit https://groups.google.com/d/msgid/codenameone-discussions/b01596da-b9ef-4ec5-8540-80d175a706e1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Steve Hannah
Software Developer
Codename One

Gareth Murfin

unread,
Apr 6, 2018, 11:37:05 PM4/6/18
to CodenameOne Discussions
thanks man, knew there was something, I did try that but I must have got it wrong cause my json got messed up, but will try it again, cheers. 1024 is ok for byte[] size?

Shai Almog

unread,
Apr 7, 2018, 12:31:06 AM4/7/18
to CodenameOne Discussions
8192 is common in Java.
Reply all
Reply to author
Forward
0 new messages