什么是java.io.IOException异常的原因:底层输入流返回零字节 [英] What is the reason for java.io.IOException: Underlying input stream returned zero bytes

查看:1868
本文介绍了什么是java.io.IOException异常的原因:底层输入流返回零字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的code,镜像文件 PDF 文件,目的是要获得的Base64 连接图像文件codeD文件。我使用的Java6 并没有可能升级到 java7目录

Here is my code, imageFile is a pdf file, intent is to get Base64 encoded file for image file. I am using Java6 and no possibility to upgrade to Java7

Base64Inputstream 的类型 org.apache.commons。codec.binary.Base64InputStream的

private File toBase64(File imageFile) throws Exception {
         LOG.info(this.getClass().getName() + " toBase64 method is called");
         System. out.println("toBase64 is called" );
         Base64InputStream in = new Base64InputStream(new FileInputStream(imageFile), true );
         File f = new File("/root/temp/" + imageFile.getName().replaceFirst("[.][^.]+$" , "" ) + "_base64.txt" );
         Writer out = new FileWriter(f);
         copy(in, out);
         return f;
  }

 private void copy(InputStream input, Writer output)
            throws IOException {
        InputStreamReader in = new InputStreamReader(input);
        copy(in, output);
    }

 private int copy(Reader input, Writer output) throws IOException {
        long count = copyLarge(input, output);
        if (count > Integer.MAX_VALUE) {
            return -1;
        }
        return (int) count;
    }

 private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;

 private long copyLarge(Reader input, Writer output) {
        char[] buffer = new char[DEFAULT_BUFFER_SIZE];
        long count = 0;
        int n = 0;
        try {
            while (-1 != (n = input.read(buffer))) {
                output.write(buffer, 0, n);
                count += n;
                System.out.println("Count: " + count);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return count;
    }

我用 IOUtils.copy(输入的InputStream,作家输出)方法。但对于一些 PDF 文件(注意,不是所有的),它会抛出异常。因此,在调试我复制的过程中 IOUtils.copy code本地和异常后抛出数:2630388 。这是堆栈跟踪:

I was using IOUtils.copy(InputStream input, Writer output) method. But for some pdf files (note, not all) it throws exception. So, in the process of debugging I copied IOUtils.copy code locally and exception is thrown after Count: 2630388. This is the stack trace:

Root Exception stack trace:
java.io.IOException: Underlying input stream returned zero bytes
    at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:268)
    at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
    at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)

在什么情形下可以在此块上面说的抛出异常:

Under what situations can this block above said throw exception:

while (-1 != (n = input.read(buffer))) {
                    output.write(buffer, 0, n);
                    count += n;
                    System.out.println("Count: " + count);
                }

请帮我理解的原因,我怎么能解决这个问题。

Please help me understand the cause and how can I fix it

推荐答案

您不应该使用它们的文本的面向读/写,而不是的二进制的,至少在没有编码。他们使用的编码。和PDF是二进制的。无论是明确给出,或默认的操作系统编码(不可移植)。

You should not use Reader/Writer which are text oriented and not binary, at least without encoding. They use an encoding. And PDF is binary. Either explicitly given, or the default OS encoding (unportable).

有关的InputStream 使用的readFully

和然后做总是的close()。在复制方法,也许离开接近来电,至少可以调用刷新()在这种情况下。

And then do always a close(). The copy method, maybe leaving the close to the callers, could at least call flush() in that case.

在Java 7中已经有一个<一个href=\"http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#copy%28java.io.InputStream,%20java.nio.file.Path,%20java.nio.file.CopyOption...%29\"相对=nofollow>复制,但需要一个路径和一个额外的选项。

In Java 7 there already exists a copy, but needs a Path and an extra option.

private File toBase64(File imageFile) throws Exception {
    LOG.info(this.getClass().getName() + " toBase64 method is called");
    System.out.println("toBase64 is called");
    Base64InputStream in = new Base64InputStream(new FileInputStream(imageFile),
        true);
    File f = new File("/root/temp/" + imageFile.getName()
        .replaceFirst("[.][^.]+$", "") + "_base64.txt");

    Files.copy(in, f.toPath(), StandardCopyOption.REPLACE_EXISTING);
    in.close();

    return f;
}

这篇关于什么是java.io.IOException异常的原因:底层输入流返回零字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆