Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2007 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.springframework.batch.item.file;
 
 import java.io.Reader;
 
A BufferedReaderFactory useful for reading simple binary (or text) files with no line endings, such as those produced by mainframe copy books. The reader splits a stream up across fixed line endings (rather than the usual convention based on plain text). The line endings are discarded, just as with the default plain text implementation.

Author(s):
Dave Syer
Since:
2.1
 
The default line ending value.
 
 	private static final String DEFAULT_LINE_ENDING = "\n";
 

Parameters:
lineEnding
 
 	public void setLineEnding(String lineEnding) {
 		this. = lineEnding;
 	}
 
 	public BufferedReader create(Resource resourceString encodingthrows UnsupportedEncodingExceptionIOException {
 		return new BinaryBufferedReader(new InputStreamReader(resource.getInputStream(), encoding), );
 	}

BufferedReader extension that splits lines based on a line ending, rather than the usual plain text conventions.

Author(s):
Dave Syer
 
 	private final class BinaryBufferedReader extends BufferedReader {
 
 		private final String ending;

Parameters:
in
 
 		private BinaryBufferedReader(Reader inString ending) {
 			super(in);
 			this. = ending;
 		}
 
 		public String readLine() throws IOException {
 
 			StringBuilder buffer = null;
 
 			synchronized () {
 
 				int next = read();
 				if (next == -1) {
 					return null;
 				}
 
 				buffer = new StringBuilder();
 				StringBuilder candidateEnding = new StringBuilder();
 
 				while (!isEndOfLine(buffercandidateEndingnext)) {
 					next = read();
 				}
 				buffer.append(candidateEnding);
 
 			}
 
 			if (buffer != null && buffer.length() > 0) {
 				return buffer.toString();
			}
			return null;
		}

Check for end of line and accumulate a buffer for next time.

Parameters:
buffer the current line excluding the candidate ending
candidate a buffer containing accumulated state
next the next character (or -1 for end of file)
Returns:
true if the values together signify the end of a file
		private boolean isEndOfLine(StringBuilder bufferStringBuilder candidateint next) {
			if (next == -1) {
				return true;
			}
			char c = (charnext;
			if (.charAt(0) == c || candidate.length() > 0) {
				candidate.append(c);
			}
			if (candidate.length() == 0) {
				buffer.append(c);
				return false;
			}
			boolean end = .equals(candidate.toString());
			if (end) {
				candidate.delete(0, candidate.length());
			}
			else if (candidate.length() >= .length()) {
				buffer.append(candidate);
				candidate.delete(0, candidate.length());
			}
			return end;
		}
	}
New to GrepCode? Check out our FAQ X