Home » Android » android – ByteArrayBuffer missing in SDK23

android – ByteArrayBuffer missing in SDK23

Posted by: admin May 14, 2020 Leave a comment

Questions:

when updating android SDK 22->23 org.apache.http.util.ByteArrayBuffer is not there anymore – is there a replacement or is this a bug?

How to&Answers:

From javadoc:

This class was deprecated in API level 22.

Please use openConnection() instead. Please visit this webpage for further details.

Answer:

this answer can be a bit late, but an alternative is to replace ByteArrayBuffer with ByteArrayOutputStream and use an array of bytes as follows:

Example of code with ByteArraybuffer:

   BufferedInputStream bis = new BufferedInputStream(is);
   ByteArrayBuffer baf = new ByteArrayBuffer(50);
   while ((current = bis.read()) != -1) {
                baf.append((byte) current);
   }
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(buffer.toByteArray());

Now, using with ByteArrayOutputStream:

     BufferedInputStream bis = new BufferedInputStream(is);
     ByteArrayOutputStream buffer = new ByteArrayOutputStream();
     //We create an array of bytes
     byte[] data = new byte[50];
     int current = 0;

     while((current = bis.read(data,0,data.length)) != -1){
           buffer.write(data,0,current);
     }

     FileOutputStream fos = new FileOutputStream(file);
     fos.write(buffer.toByteArray());
     fos.close();

Well, I hope this be useful.