protected JsonToken handleNewDocument ( boolean array ) throws IOException { if ( _in == null ) { //this means Feature.HONOR_DOCUMENT_LENGTH is enabled, and we //haven't yet started reading. Read the first int to find out the //length of the document. byte [ ] buf = new byte [ Integer . SIZE / Byte . SIZE ] ; int len = 0 ; while ( len < buf . length ) { int l = _rawInputStream . read ( buf , len , buf . length - len ) ; if ( l == - 1 ) { throw new IOException ( "Not enough bytes for length of document" ) ; } len += l ; } //wrap the input stream by a bounded stream, subtract buf.length from the //length because the size itself is included in the length int documentLength = ByteBuffer . wrap ( buf ) . order ( ByteOrder . LITTLE_ENDIAN ) . getInt ( ) ; InputStream in = new BoundedInputStream ( _rawInputStream , documentLength - buf . length ) ; //buffer if the raw input stream is not already buffered if ( ! ( _rawInputStream instanceof BufferedInputStream ) ) { in = new StaticBufferedInputStream ( in ) ; } _counter = new CountingInputStream ( in ) ; _in = new LittleEndianInputStream ( _counter ) ; } else { //read document header (skip size, we're not interested) _in . readInt ( ) ; } _currentContext = new Context ( _currentContext , array ) ; return array ? JsonToken . START_ARRAY : JsonToken . START_OBJECT ; }
Can be called when a new embedded document is found . Reads the document s header and creates a new context on the stack .