Can I peek on a BufferedReader?

后端 未结 8 1787
别跟我提以往
别跟我提以往 2021-01-01 14:02

Is there a way to check if in BufferedReader object is something to read? Something like C++ cin.peek(). Thanks.

相关标签:
8条回答
  • 2021-01-01 14:34
    BufferedReader br = new BufferedReader(reader);
    br.mark(1);
    int firstByte = br.read();
    br.reset();
    
    0 讨论(0)
  • 2021-01-01 14:41

    my solution was.. extending BufferedReader and use queue as buf, then you can use peek method in queue.

    public class PeekBufferedReader extends BufferedReader{
    
        private Queue<String>       buf;
        private int                 bufSize;
    
        public PeekBufferedReader(Reader reader, int bufSize) throws IOException {
            super(reader);
            this.bufSize = bufSize;
            buf = Queues.newArrayBlockingQueue(bufSize);
        }
    
        /**
         * readAheadLimit is set to 1048576. Line which has length over readAheadLimit 
         * will cause IOException.
         * @throws IOException 
         **/
        //public String peekLine() throws IOException {
        //  super.mark(1048576);
        //  String peekedLine = super.readLine();
        //  super.reset();
        //  return peekedLine;
        //}
    
        /**
         * This method can be implemented by mark and reset methods. But performance of 
         * this implementation is better ( about 2times) than using mark and reset  
         **/
        public String peekLine() throws IOException {
            if (buf.isEmpty()) {
                while (buf.size() < bufSize) {
                    String readLine = super.readLine();
                    if (readLine == null) {
                        break;
                    } else {
                        buf.add(readLine);
                    }
                }
            } else {
                return buf.peek();
            }
            if (buf.isEmpty()) {
                return null;
            } else {
                return buf.peek();
            }
        }
    
        public String readLine() throws IOException {
            if (buf.isEmpty()) {
                while (buf.size() < bufSize) {
                    String readLine = super.readLine();
                    if (readLine == null) {
                        break;
                    } else {
                        buf.add(readLine);
                    }
                }
            } else {
                return buf.poll();
            }
            if (buf.isEmpty()) {
                return null;
            } else {
                return buf.poll();
            }
        }
        public boolean isEmpty() throws IOException {
            if (buf.isEmpty()) {
                while (buf.size() < bufSize) {
                    String readLine = super.readLine();
                    if (readLine == null) {
                        break;
                    } else {
                        buf.add(readLine);
                    }
                }
            } else {
                return false;
            }
            if (buf.isEmpty()) {
                return true;
            } else {
                return false;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题