0

I have a case where I need to peek ahead in the stream for the existence of a certain regular expression and then read data from the stream.

mark and reset allow me to do this but I am facing an issue where mark becomes invalid if the readAheadLimit goes beyond the size of the current buffer.

For example: I have a BufferedReader with buffer size of 1k.

Lets say I am at position 1000 (mark=1000) in the buffer and I need to check for the regex in the next 100 chars (readAheadLimit=100).

So while reading, the moment I cross the current buffer size (1024), a new buffer is allocated and the mark becomes invalid (not able to reset) and the data is streamed into the new buffer in a normal way.

I think this is the intended behavior but is there a way to get around this?

Appreciate your help.

regards

4

2 に答える 2

2

現在のバッファ サイズ (1024) を超えた瞬間、新しいバッファが割り当てられます

いいえ、そうではありません。既存のバッファがクリアされ、別の使用に備えます。

マークが無効になる(リセットできない)

いいえ、先読み制限を超えていない限り、そうではありません。

API を読んでいないようです。reset() を呼び出す前にどれだけ先に進みたいかを示す引数 (この場合は 100 バイト) を指定して mark() を呼び出します。API は正確にそれを実行できるようにする必要があります。したがって、最大 100 文字進んだら、reset() を呼び出すと、mark() を呼び出したときの場所に戻ります。それが内部でどのように発生するかは問題ではありませんが、発生する必要があることは確かです。

また、1k バッファーの BufferedReader をどのように取得しましたか? デフォルトは 4096 です。

于 2013-02-20T11:43:03.543 に答える
1

少なくとも2つのオプションがあります。

  1. デフォルトのキャッシュサイズを1kよりはるかに大きく設定します。

    new BufferedReader(originalReader、1024 * 1024)//例:1Mb

  2. カスタムバッファリングを適用して、制限を超えたらすぐにキャッシュサイズを増やします。大量のデータを処理している場合、カスタムバッファリングにより、データをデータベースまたはファイルに保存できます。

于 2013-02-19T06:41:48.003 に答える