KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > Pager


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.cli.framework;
25
26 import java.io.BufferedReader JavaDoc;
27 import java.io.BufferedWriter JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.io.Reader JavaDoc;
30 import java.io.Writer JavaDoc;
31
32
33
34 /**
35  * Pager.java
36  *
37  * @author <a HREF="mailto:toby.h.ferguson@sun.com">Toby H Ferguson</a>
38  * @version $Revision: 1.3 $
39  */

40
41 class Pager {
42     private BufferedReader JavaDoc in;
43     private BufferedWriter JavaDoc out;
44     private int pageLength;
45     private String JavaDoc line;
46
47       /**
48        * Construct an object which will copy one pages worth of lines
49        * at a time from the input to the
50        * the output.
51        *
52        * No attempt is made under any circumstances to close the input
53        * or output.
54        *
55        * @param lines the number of lines in a page. A number less
56        * than 0 means copy all the input to the output.
57        * @param in the source of the copy operation
58        * @param out the destination of the copy operation
59        * @throws IOException if there's a problem reading from, or
60        * writing to, the source or destination
61        */

62     
63     Pager (int lines, Reader JavaDoc in, Writer JavaDoc out) throws IOException JavaDoc {
64         this.in = new BufferedReader JavaDoc(in);
65         this.out = new BufferedWriter JavaDoc(out);
66         pageLength = lines;
67         nextLine();
68     }
69
70       /**
71        * Copy the next page worth of lines from input to output
72        */

73     void nextPage() throws IOException JavaDoc {
74         for (int i = 0; (pageLength < 0 || i < pageLength) && hasNext(); i++){
75             out.write(line);
76             out.newLine();
77             nextLine();
78         }
79         out.flush();
80     }
81
82       /**
83        * Indicate if there are lines left to be copied
84        * @return true iff there is at least one line left to be copied
85        */

86     boolean hasNext() {
87         return line != null;
88     }
89
90       /**
91        * Get the next line and copy it inot the internal buffer so's
92        * we can answer the hasNext() question
93        */

94     private void nextLine() throws IOException JavaDoc {
95         line = in.readLine();
96     }
97     
98 }
99
100
Popular Tags