1

I had a method like this in an Android app reading a raw file:

public String inputStreamToString(InputStream isTwo) throws IOException {
        StringBuffer sBuffer = new StringBuffer();
        DataInputStream dataIO = new DataInputStream(isTwo);
        String strLineTwo = null;
        while ((strLineTwo = dataIO.readLine()) != null) {
            sBuffer.append(strLineTwo + "\n");
        }
        dataIO.close();
        isTwo.close();
        return sBuffer.toString();

    }

However, the DataInputStream object appears to be deprecated now. I researched it and heard it is better to wrap the readline() with a BufferedInputStream. Can someone help me finish his (fill in the missing line)? I am not sure how to declare the br var. This is what I have so far:

public String inputStreamToString(InputStream isTwo) throws IOException {
        String strLineTwo = null;
        BufferedReader br = null;

        StringBuffer sBuffer = new StringBuffer();
        InputStreamReader dataIO = new InputStreamReader(isTwo);


        while ((strLineTwo = br.readLine()) != null) {
            sBuffer.append(strLineTwo + "\n");
        }

        dataIO.close();
        isTwo.close();
        return sBuffer.toString();

Here is the preceding code I have not touched yet that calls this method:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tech);
        InputStream iFileTwo = getResources().openRawResource(R.raw.testing);
        try {
            TextView helpText = (TextView) findViewById(R.id.tvStream);
            String strFileTwo = inputStreamToString(iFileTwo);
            helpText.setText(strFileTwo);

        } catch (Exception e) {
            Log.e(DEBUG_TAG_THREE, "InputStreamToString failure", e);
        }
    }

Also, I want to make sure it works from Android 2.3 to 4.2 (current). Thanks for any help.

TheLettuceMaster
  • 15,594
  • 48
  • 153
  • 259
  • Please don't use StringBuffer when you can use StringBuilder. Don't mix text and binary. If you want to use text, use BufferedReader not DataInputStream. – Peter Lawrey Dec 19 '12 at 16:37
  • 2
    what's wrong with `br = new BufferedReader(dataIo);` ? – njzk2 Dec 19 '12 at 16:37
  • OP: We're not code writers for you based on your comment above in the question 'Can someone help me finish this?' which implies, write code for you.. Heck, we're not paid to do that! All we can do is *guide* you and that sort of comment are frowned upon here! – t0mm13b Dec 19 '12 at 16:42
  • @t0mm13b Except when the OP is so far off it's easier to re-write, ;) – Peter Lawrey Dec 19 '12 at 16:44
  • @t0mm13b I admit, that does sound lazy. I figured it was easy as writing a single line such as `br = new BufferedReader(dataIo);` and that actually is all that I needed. However, Peter choose to rewrite it to make it more optimal (My code was far outdated which was lost on me since I haven't touched it in two years and got it off a tutorial). More power to him. – TheLettuceMaster Dec 19 '12 at 16:46
  • Possible duplicate: [Read/convert an InputStream to a String](http://stackoverflow.com/q/309424/1073063) – Pablo Dec 19 '12 at 17:08

2 Answers2

4

This is how I would write it. This has much less overhead and preserves the newlines as they were originally.

public String inputStreamToString(InputStream in) throws IOException {
    StringBuilder out = new StringBuilder();
    char[] chars = new char[1024];
    Reader reader = new InputStreamReader(in /*, CHARSET_TO_USE */);

    try {
        for (int len; (len = reader.read(chars)) > 0; )
            out.append(chars, 0, len);
    } finally {
        try {
            in.close();
        } catch (IOException ignored) {
        }
    }
    return out.toString();
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • 1
    nota : this assumes the stream uses the same encoding as the system default. This is not guaranteed to be the case, and may result in conflicts. I would recommend explicitly setting the encoding. – njzk2 Dec 19 '12 at 17:07
  • Incidentally, this is pretty much the way used by apache in EntityUtils (except they use a CharArrayBuffer instead of a StringWriter. Not sure if one is better that the other – njzk2 Dec 19 '12 at 17:09
  • If Android has a built in function that would be better to use. – Peter Lawrey Dec 19 '12 at 17:12
  • Just looked at android StringWriter code. Though you may be interested in knowing it uses a StringBuffer as data container. ^^ – njzk2 Dec 19 '12 at 17:14
  • (android has a built-in function from apache for entities (http responses mainly). Not directly usable with inputstream) – njzk2 Dec 19 '12 at 17:15
  • Good point. I really don't like StringBuffer, replaced it with StringBuilder. – Peter Lawrey Dec 19 '12 at 17:28
  • 1
    I tested this on an old and new version of Android as well and ran into no problems. – TheLettuceMaster Dec 19 '12 at 18:50
0

Just a suggestion, if you are migrating, then why not use the IOUtils from libraries like apache commons etc which actually take care of managing your streams and also save you with lot of errorneous conditions

rock_win
  • 755
  • 1
  • 5
  • 14