Java方法在Eclipse中可用,但在Android中不可用

时间:2019-01-29 13:46:54

标签: java android android-studio

我在Eclipse的库中使用readAllBytes()类中的CipherInputStream方法,但是,当我在Android中使用该库时,该方法不可用。对于Android和Eclipse项目,我的源兼容性都设置为JAVA_1_8。

为什么readAllBytes()方法在Android中不可用?

2 个答案:

答案 0 :(得分:3)

readAllBytes是java10 +引入的,android还不算什么。源代码兼容性是关于哪些Java语言功能可用的。您可以配置要单独使用的JVM。安装一个JDK8并指向Eclipse。然后getAllBytes应该消失了。

答案 1 :(得分:1)

我相信这是您要寻找的功能:

    /**
         * Copies all available data from in to out without closing any stream.
         *
         * @return number of bytes copied
         */
        private static final int BUFFER_SIZE = 8192;
        public static int copyAllBytes(InputStream in, OutputStream out) throws IOException {
            int byteCount = 0;
            byte[] buffer = new byte[BUFFER_SIZE];
            while (true) {
                int read = in.read(buffer);
                if (read == -1) {
                    break;
                }
                out.write(buffer, 0, read);
                byteCount += read;
            }
            return byteCount;
        }