如何在java中将读取器转换为inputstream

hrirmatl  于 2021-06-29  发布在  Java
关注(0)|答案(2)|浏览(467)

我需要将reader对象转换为inputstream。我现在的解决方案如下。但我担心的是,由于这将处理大量数据,它将大幅增加内存使用。

private static InputStream getInputStream(final Reader reader) {
   char[] buffer = new char[10240];
   StringBuilder builder = new StringBuilder();
   int charCount;
   try {
      while ((charCount = reader.read(buffer, 0, buffer.length)) != -1) {
         builder.append(buffer, 0, charCount);
      }
      reader.close();
   } catch (final IOException e) {
      e.printStackTrace();
   }
   return new ByteArrayInputStream(builder.toString().getBytes(StandardCharsets.UTF_8));
}

因为我使用的是stringbuilder,所以它会将reader对象的全部内容保留在内存中。我想避免这样。有没有方法可以通过管道读取对象?非常感谢您的帮助。

6l7fqoea

6l7fqoea1#

第一:一个罕见的需求,通常是相反的,或者有一个filechannel,所以可以使用bytebuffer。
pipedinputstream是可能的,在第二个线程中启动pipedoutputstream。然而,这是不必要的。
读书人给人以魅力。unicode代码点是从一个或两个字符(后者是代理项对)派生的。

/**
 * Reader for an InputSteam of UTF-8 text bytes.
 */
public class ReaderInputStream extends InputStream {

    private final Reader reader;
    private boolean eof;
    private int byteCount;
    private byte[] bytes = new byte[6];

    public ReaderInputStream(Reader reader) {
        this.reader = reader;
    }

    @Override
    public int read() throws IOException {
        if (byteCount > 0) {
            int c = bytes[0];
            --byteCount;
            for (int i = 0; i < byteCount; ++i) {
                bytes[i] = bytes[i + 1];
            }
            return c;
        }
        if (eof) {
            return -1;
        }

        int c = reader.read();
        if (c == -1) {
            eof = true;
            return -1;
        }
        char ch = (char) c;
        String s;
        if (Character.isHighSurrogate(ch)) {
            c = reader.read();
            if (c == -1) {
                // Error, low surrogate expected.
                eof = true;
                //return -1;
                throw new IOException("Expected a low surrogate char i.o. EOF");
            }
            char ch2 = (char) c;
            if (!Character.isLowSurrogate(ch2)) {
                throw new IOException("Expected a low surrogate char");
            }
            s = new String(new char [] {ch, ch2});
        } else {
            s = Character.toString(ch);
        }
        byte[] bs = s.getBytes(StandardCharsets.UTF_8);
        byteCount = bs.length;
        System.arraycopy(bs, 0, bytes, 0, byteCount);
        return read();
    }
}

        Path source = Paths.get("...");
        Path target = Paths.get("...");
        try (Reader reader = Files.newBufferedReader(source, StandardCharsets.UTF_8);
                InputStream in = new ReaderInputStream(reader)) {
            Files.copy(in, target);
        }
xzlaal3s

xzlaal3s2#

使用apache commons io库,您可以在一行中完成此转换:

//import org.apache.commons.io.input.ReaderInputStream;

    InputStream inputStream = new ReaderInputStream(reader, StandardCharsets.UTF_8);

你可以在网上阅读这节课的文件https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/input/readerinputstream.html
也许值得一试,看看它是否也解决了内存问题。

相关问题