1 | /* |
2 | * Copyright 2006-2007 the original author or authors. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | package org.springframework.batch.item.file; |
17 | |
18 | import java.io.BufferedReader; |
19 | import java.io.IOException; |
20 | import java.io.InputStreamReader; |
21 | import java.io.Reader; |
22 | import java.io.UnsupportedEncodingException; |
23 | |
24 | import org.springframework.core.io.Resource; |
25 | |
26 | /** |
27 | * A {@link BufferedReaderFactory} useful for reading simple binary (or text) |
28 | * files with no line endings, such as those produced by mainframe copy books. |
29 | * The reader splits a stream up across fixed line endings (rather than the |
30 | * usual convention based on plain text). The line endings are discarded, just |
31 | * as with the default plain text implementation. |
32 | * |
33 | * @author Dave Syer |
34 | * |
35 | * @since 2.1 |
36 | */ |
37 | public class SimpleBinaryBufferedReaderFactory implements BufferedReaderFactory { |
38 | |
39 | /** |
40 | * The default line ending value. |
41 | */ |
42 | private static final String DEFAULT_LINE_ENDING = "\n"; |
43 | |
44 | private String lineEnding = DEFAULT_LINE_ENDING; |
45 | |
46 | /** |
47 | * @param lineEnding |
48 | */ |
49 | public void setLineEnding(String lineEnding) { |
50 | this.lineEnding = lineEnding; |
51 | } |
52 | |
53 | public BufferedReader create(Resource resource, String encoding) throws UnsupportedEncodingException, IOException { |
54 | return new BinaryBufferedReader(new InputStreamReader(resource.getInputStream(), encoding), lineEnding); |
55 | } |
56 | |
57 | /** |
58 | * BufferedReader extension that splits lines based on a line ending, rather |
59 | * than the usual plain text conventions. |
60 | * |
61 | * @author Dave Syer |
62 | * |
63 | */ |
64 | private final class BinaryBufferedReader extends BufferedReader { |
65 | |
66 | private final String ending; |
67 | |
68 | /** |
69 | * @param in |
70 | */ |
71 | private BinaryBufferedReader(Reader in, String ending) { |
72 | super(in); |
73 | this.ending = ending; |
74 | } |
75 | |
76 | @Override |
77 | public String readLine() throws IOException { |
78 | |
79 | StringBuilder buffer = null; |
80 | |
81 | synchronized (lock) { |
82 | |
83 | int next = read(); |
84 | if (next == -1) { |
85 | return null; |
86 | } |
87 | |
88 | buffer = new StringBuilder(); |
89 | StringBuilder candidateEnding = new StringBuilder(); |
90 | |
91 | while (!isEndOfLine(buffer, candidateEnding, next)) { |
92 | next = read(); |
93 | } |
94 | buffer.append(candidateEnding); |
95 | |
96 | } |
97 | |
98 | if (buffer != null && buffer.length() > 0) { |
99 | return buffer.toString(); |
100 | } |
101 | return null; |
102 | |
103 | } |
104 | |
105 | /** |
106 | * Check for end of line and accumulate a buffer for next time. |
107 | * |
108 | * @param buffer the current line excluding the candidate ending |
109 | * @param candidate a buffer containing accumulated state |
110 | * @param next the next character (or -1 for end of file) |
111 | * @return true if the values together signify the end of a file |
112 | */ |
113 | private boolean isEndOfLine(StringBuilder buffer, StringBuilder candidate, int next) { |
114 | |
115 | if (next == -1) { |
116 | return true; |
117 | } |
118 | |
119 | char c = (char) next; |
120 | if (ending.charAt(0) == c || candidate.length() > 0) { |
121 | candidate.append(c); |
122 | } |
123 | |
124 | if (candidate.length() == 0) { |
125 | buffer.append(c); |
126 | return false; |
127 | } |
128 | |
129 | boolean end = ending.equals(candidate.toString()); |
130 | if (end) { |
131 | candidate.delete(0, candidate.length()); |
132 | } |
133 | else if (candidate.length() >= ending.length()) { |
134 | buffer.append(candidate); |
135 | candidate.delete(0, candidate.length()); |
136 | } |
137 | |
138 | return end; |
139 | |
140 | } |
141 | } |
142 | |
143 | } |