3

最初に文字列に変換せずに、BufferedReaderを使用してByteBufferを読み取る方法はありますか?かなり大きなByteBufferをテキスト行として読みたいのですが、パフォーマンス上の理由から、ディスクへの書き込みは避けたいと思います。結果の文字列が大きすぎるため、ByteBufferでtoStringを呼び出すことはできません(java.lang.OutOfMemoryError:Javaヒープスペースがスローされます)。APIに適切なリーダーでByteBufferをラップする何かがあると思っていたのですが、適切なものが見つからないようです。

これが私がしていることを示す省略されたコードサンプルです):

// input stream is from Process getInputStream()
public String read(InputStream istream)
{
  ReadableByteChannel source = Channels.newChannel(istream);
  ByteArrayOutputStream ostream = new ByteArrayOutputStream(bufferSize);
  WritableByteChannel destination = Channels.newChannel(ostream);
  ByteBuffer buffer = ByteBuffer.allocateDirect(writeBufferSize);

  while (source.read(buffer) != -1)
  {
    buffer.flip();
    while (buffer.hasRemaining())
    {
      destination.write(buffer);
    }
    buffer.clear();
  }

  // this data can be up to 150 MB.. won't fit in a String.
  result = ostream.toString();
  source.close();
  destination.close();
  return result;
}

// after the process is run, we call this method with the String
public void readLines(String text)
{
  BufferedReader reader = new BufferedReader(new StringReader(text));
  String line;

  while ((line = reader.readLine()) != null)
  {
    // do stuff with line
  }
}
4

3 に答える 3

5

そもそもなぜバイトバッファを使用しているのかは明らかではありません。あなたが持っていInputStreamて、それのために行を読みたいのなら、なぜあなたはただInputStreamReader包まれたものを使ってみませんBufferedReaderか?NIOを関与させることの利点は何ですか?

スペースがあったとしても、私には悪い考えのように聞こえます。バイト配列として取得し、それをaでラップしてtoString()から、本当に必要な場合は、でラップする方がよいでしょう。本当に呼び出したい場合は、少なくとも、使用する文字エンコードの名前をとるオーバーロードを使用してください。そうでない場合は、システムのデフォルトが使用されますが、これはおそらくあなたが望むものではありません。ByteArrayOutputStreamByteArrayInputStreamInputStreamReaderByteArrayOutputStreamtoString()

編集:さて、あなたは本当にNIOを使いたいのです。最終的にはまだ書き込みを行っているByteArrayOutputStreamため、データを含むBAOSになります。そのデータのコピーを作成したくない場合はByteArrayOutputStream、たとえば次のように、から派生する必要があります。

public class ReadableByteArrayOutputStream extends ByteArrayOutputStream
{
    /**
     * Converts the data in the current stream into a ByteArrayInputStream.
     * The resulting stream wraps the existing byte array directly;
     * further writes to this output stream will result in unpredictable
     * behavior.
     */
    public InputStream toInputStream()
    {
        return new ByteArrayInputStream(array, 0, count);
    }
}

次に、入力ストリームを作成し、それをでラップし、それをでラップすることがInputStreamReaderできBufferedReaderます。

于 2009-06-25T19:04:32.120 に答える
4

NIOを使用することはできますが、ここでは実際の必要はありません。ジョンスキートが提案したように:

public byte[] read(InputStream istream)
{
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  byte[] buffer = new byte[1024]; // Experiment with this value
  int bytesRead;

  while ((bytesRead = istream.read(buffer)) != -1)
  {
    baos.write(buffer, 0, bytesRead);
  }

  return baos.toByteArray();
}


// after the process is run, we call this method with the String
public void readLines(byte[] data)
{
  BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(data)));
  String line;

  while ((line = reader.readLine()) != null)
  {
    // do stuff with line
  }
}
于 2009-06-25T19:42:45.023 に答える
1

これはサンプルです:

public class ByteBufferBackedInputStream extends InputStream {

    ByteBuffer buf;

    public ByteBufferBackedInputStream(ByteBuffer buf) {
        this.buf = buf;
    }

    public synchronized int read() throws IOException {
        if (!buf.hasRemaining()) {
            return -1;
        }
        return buf.get() & 0xFF;
    }

    @Override
    public int available() throws IOException {
        return buf.remaining();
    }

    public synchronized int read(byte[] bytes, int off, int len) throws IOException {
        if (!buf.hasRemaining()) {
            return -1;
        }

        len = Math.min(len, buf.remaining());
        buf.get(bytes, off, len);
        return len;
    }
}

そして、あなたはそれをこのように使うことができます:

    String text = "this is text";   // It can be Unicode text
    ByteBuffer buffer = ByteBuffer.wrap(text.getBytes("UTF-8"));

    InputStream is = new ByteBufferBackedInputStream(buffer);
    InputStreamReader r = new InputStreamReader(is, "UTF-8");
    BufferedReader br = new BufferedReader(r);
于 2012-11-11T08:37:55.323 に答える