如何实现自定义 FilterReader?

How to implement a custom FilterReader?

我正在尝试实现自定义 FilterReader class。 class 应该转换它读取的内容(不管如何转换)。我遇到的问题是转换后的字符串比读入 char 缓冲区的原始字符串长,所以当我尝试将新字符串塞入缓冲区时得到 ArrayIndexOutOfBoundsException

这是我的自定义 FilterReader 的 read() 方法 class:

@Override
public int read(char[] cbuf, int off, int len) throws IOException {
    int result = in.read(cbuf, off, len);

    if( result != -1 ){
        String str = new String(cbuf, off, len);
        str = someStringTranformationMethod(str);

        //cbuf = new char[str.length()];

        str.getChars(0, str.length(), cbuf, 0);

        result = str.length();
    }

    return result;
}

我想我可以通过为 cbuf 重新分配一个新的 char 缓冲区来解决它,这就是我在注释行中尝试的方法。但是,这似乎根本不起作用,因为我从 reader 输出的是原始(未转换的)字符串。

我有一种感觉,我要在这里完全倒退,但要在网上找到任何关于这方面的好例子并不容易。我发现的所有自定义 FilterReader 都只是完成了一些基本的 upper/lowercasing 字符,其中新字符串的长度与原始字符串的长度相同。

那么我如何使用字符串转换函数来实现它,从而产生比原始字符串更长的字符串?

您需要使您的实现有状态并跟踪 "leftover" 个字符:

private String str = "";
private int pos = 0;

public int read(char[] cbuf, int off, int len) throws IOException {
  if (pos == str.length()) {
    // No leftovers from a previous call available, need to actully read more
    int result = in.read(cbuf, off, len);
    if( result <= 0 ){
      return -1;
    }
    str = new String(cbuf, off, result);
    str = someStringTranformationMethod(str);
    pos = 0;
  }

  // Return as much as we have available, but not more than len
  int available = Math.min(str.length() - pos, len);     
  str.getChars(pos, pos + available, cbuf, off);
  pos += available;
  return available;
}

请注意,如果尚未到达流的末尾,read() 只需读取 (at least) 一个字符。此实现利用了这一点。