1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 package org.apache.geronimo.javamail.transport.nntp;
21
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.UnsupportedEncodingException;
25 import java.util.List;
26
27 /**
28 * @version $Rev: 437941 $ $Date: 2006-08-28 23:56:02 -0400 (Mon, 28 Aug 2006) $
29 */
30 public class StringListInputStream extends InputStream {
31 // the list of lines we're reading from
32 protected List lines;
33
34 // the next line to process.
35 protected int nextLine = 0;
36
37 // current buffer of bytes to read from
38 byte[] buffer;
39
40 // current offset within the buffer;
41 int offset;
42
43 // indicator that we've left off at a split between the CR and LF of a line
44 // break.
45 boolean atLineBreak = false;
46
47 public StringListInputStream(List lines) throws IOException {
48 this.lines = lines;
49 nextLine = 0;
50 buffer = null;
51 offset = 0;
52 atLineBreak = false;
53
54 // if we have at least one line in the list, get the bytes now.
55 if (lines.size() > 0) {
56 nextBuffer();
57 }
58 }
59
60 /**
61 * Just override the single byte read version, which handles all of the
62 * lineend markers correctly.
63 *
64 * @return The next byte from the stream or -1 if we've hit the EOF.
65 */
66 public int read() throws IOException {
67 // leave off at the split between a line?
68 if (atLineBreak) {
69 // flip this off and return the second line end character. Also step
70 // to the next line.
71 atLineBreak = false;
72 nextBuffer();
73 return '\n';
74 }
75 // gone past the end? Got an EOF
76 if (buffer == null) {
77 return -1;
78 }
79
80 // reach the end of the line?
81 if (offset >= buffer.length) {
82 // we're now working on a virtual linebreak
83 atLineBreak = true;
84 return '\r';
85 }
86 // just return the next byte
87 return buffer[offset++];
88
89 }
90
91 /**
92 * Step to the next buffer of string data.
93 *
94 * @exception IOException
95 */
96 protected void nextBuffer() throws IOException {
97 // give an eof check.
98 if (nextLine >= lines.size()) {
99 buffer = null;
100 } else {
101 try {
102 String next = (String) lines.get(nextLine++);
103 buffer = next.getBytes("US-ASCII");
104
105 } catch (UnsupportedEncodingException e) {
106 throw new IOException("Invalid string encoding");
107 }
108 }
109
110 offset = 0;
111 }
112 }