如何在java中将Reader转换为InputStream

问题描述 投票:0回答:3

我需要将 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 对象?对此的任何帮助都非常感谢。

java memory-management inputstream reader
3个回答
5
投票

使用 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

阅读此类的文档

可能值得尝试一下,看看它是否也解决了内存问题。


1
投票

第一:一个罕见的需求,通常是相反的,或者有一个 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);
        }

0
投票

我不知道纯 JDK 方式有任何可能性。 StringBufferInputStream 已弃用,因为它无法正确地将字符转换为字节。
如果项目中有 Guava 库,则可以通过以下方式使用内部 ReaderInputStream

public InputStream asInputStream(Reader reader, Charset charset) throws IOException {
    return new CharSource() {
        @Override public Reader openStream() {
            return reader;
        }
    }.asByteSource(charset).openStream();
}

在底层,Reader 被包装到开头提到的 InputStream 中。

© www.soinside.com 2019 - 2024. All rights reserved.