Question

I found the following code in a project. It works and can read more than 20MB big files. From how the code is set up it should fail after 5000 bytes. Why does it work? The docs for ByteArrayBuffer indicate nothing like that. I have verified that the read-loop iterates for all bytes of each file.

http://developer.android.com/reference/org/apache/http/util/ByteArrayBuffer.html

        URLConnection ucon = url.openConnection();
        ucon.setReadTimeout(10000); // enables throw SocketTimeoutException
        InputStream is = ucon.getInputStream();

        long expectedFileSize = ucon.getContentLength();
        Log.d("Downloader", "expected file size: " + expectedFileSize);

        BufferedInputStream bis = new BufferedInputStream(is);

        // Read bytes to the Buffer until there is nothing more to read(-1).
        // This code can read 20MB and bigger mp3-files, ... why?!?
        ByteArrayBuffer baf = new ByteArrayBuffer(5000);
        int current = 0;
        while ((current = bis.read()) != -1) {
            baf.append((byte) current);
        }

        FileOutputStream fos = new FileOutputStream(file);
        fos.write(baf.toByteArray());
        fos.flush();
        fos.close();
Était-ce utile?

La solution

5000 is just an initial capacity. It gets resized automatically once reaches its limit

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top