Android: compile error Util.toByteArray (taken from an example)

前端 未结 1 969
你的背包
你的背包 2021-01-24 17:46

I am trying to compile this sample of code in my Android app to have crypt/decrypt feature. I found that code here http://apachejava.blogspot.it/2012/04/androidencryption-made-e

相关标签:
1条回答
  • 2021-01-24 18:01

    Part of the needed code is missing in the page you link : the author forgot to show his Util class which obviously contains a toByteArray function.

    Solution 1 : use commons IO

    Replace

    Util.toByteArray(cis);   
    

    by

    IOUtils.toByteArray(cis);
    

    IOUtils is a Apache commons IO utility class.

    You'll need

    • to download the commons IO jar (see link) and set your classpath accordingly
    • this import at the start of your class : import org.apache.commons.io.IOUtils;

    Solution 2 : write a toByteArray function

    Define this function :

    public byte[] toByteArray(InputStream is) throws IOException {
        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        int l;
        byte[] data = new byte[1024];
        while ((l = is.read(data, 0, data.length)) != -1) {
          buffer.write(data, 0, l);
        }
        buffer.flush();
        return buffer.toByteArray();
    }
    

    And replace Util.toByteArray(cis); by toByteArray(cis);.

    0 讨论(0)
提交回复
热议问题