InputStreamReader buffering issue

后端 未结 6 893
感情败类
感情败类 2021-02-05 21:18

I am reading data from a file that has, unfortunately, two types of character encoding.

There is a header and a body. The header is always in ASCII and defines the char

6条回答
  •  一个人的身影
    2021-02-05 22:00

    If you wrap the InputStream and limit all reads to just 1 byte at a time, it seems to disable the buffering inside of InputStreamReader.

    This way we don't have to rewrite the InputStreamReader logic.

    public class OneByteReadInputStream extends InputStream
    {
        private final InputStream inputStream;
    
        public OneByteReadInputStream(InputStream inputStream)
        {
            this.inputStream = inputStream;
        }
    
        @Override
        public int read() throws IOException
        {
            return inputStream.read();
        }
    
        @Override
        public int read(byte[] b, int off, int len) throws IOException
        {
            return super.read(b, off, 1);
        }
    }
    

    To construct:

    new InputStreamReader(new OneByteReadInputStream(inputStream));
    

提交回复
热议问题