Path: blob/master/test/jdk/java/io/charStreams/LineGenerator.java
41152 views
/*1* Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**/242526public class LineGenerator {2728IntGenerator ig;29StringGenerator sg;30int limit;3132public LineGenerator(IntGenerator ig, StringGenerator sg, int limit) {33this.ig = ig;34this.sg = sg;35this.limit = limit;36}3738public LineGenerator(IntGenerator ig) {39this.ig = ig;40this.sg = new StringGenerator(ig);41this.limit = -1;42}4344private char prevTerminator = 0;45private int count = 0;46public String lineTerminator;4748public String next() {49if ((count >= limit) && (limit >= 0))50return null;5152String l = sg.next();5354/* Avoid "\r\n" sequences55in which the '\n' terminates a blank line */56int len = l.length();57int t;58do59t = ig.next(2);60while ((prevTerminator == '\r') && (len == 0) && (t == 0));6162String ts;63switch (t) {64case 0:65ts = "\n";66prevTerminator = '\n';67break;68case 1:69ts = "\r";70prevTerminator = '\r';71break;72case 2:73default:74ts = "\r\n";75prevTerminator = '\n';76break;77}7879count++;80lineTerminator = ts;81return l;82}83}848586