Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 78a41ce4abf8bf4e94e17b3e255c6a052c85a374 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.core.streams;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;

/**
 * Converts CR/LFs in the underlying input stream to LF.
 *
 * Supports resuming partially completed operations after an InterruptedIOException
 * if the underlying stream does.  Check the bytesTransferred field to determine how
 * much of the operation completed; conversely, at what point to resume.
 */
public class CRLFtoLFInputStream extends FilterInputStream {
	private boolean pendingByte = false;
	private int lastByte = -1;

	/**
	 * Creates a new filtered input stream.
	 * @param in the underlying input stream
	 */
	public CRLFtoLFInputStream(InputStream in) {
		super(in);
	}

	/**
	 * Wraps the underlying stream's method.
	 * Translates CR/LF sequences to LFs transparently.
	 * @throws InterruptedIOException if the operation was interrupted before all of the
	 *         bytes specified have been skipped, bytesTransferred will be zero
	 * @throws IOException if an i/o error occurs
	 */
	public int read() throws IOException {
		if (! pendingByte) {
			lastByte = in.read(); // ok if this throws
			pendingByte = true; // remember the byte in case we throw an exception later on
		}
		if (lastByte == '\r') {
			lastByte = in.read(); // ok if this throws
			if (lastByte != '\n') {
				if (lastByte == -1) pendingByte = false;
				return '\r'; // leaves the byte pending for later
			}
		}
		pendingByte = false;
		return lastByte;
	}

	/**
	 * Wraps the underlying stream's method.
	 * Translates CR/LF sequences to LFs transparently.
	 * @throws InterruptedIOException if the operation was interrupted before all of the
	 *         bytes specified have been skipped, bytesTransferred may be non-zero
	 * @throws IOException if an i/o error occurs
	 */
	public int read(byte[] buffer, int off, int len) throws IOException {
		// handle boundary cases cleanly
		if (len == 0) {
			return 0;
		} else if (len == 1) {
			int b = read();
			if (b == -1) return -1;
			buffer[off] = (byte) b;
			return 1;
		}
		// read some bytes from the stream
		// prefix with pending byte from last read if any
		int count = 0;
		if (pendingByte) {
			buffer[off] = (byte) lastByte;
			pendingByte = false;
			count = 1;
		}
		InterruptedIOException iioe = null;
		try {
			len = in.read(buffer, off + count, len - count);
			if (len == -1) {
				return (count == 0) ? -1 : count;
			}
		} catch (InterruptedIOException e) {
			len = e.bytesTransferred;
			iioe = e;
		}
		count += len;
		// strip out CR's in CR/LF pairs
		// pendingByte will be true iff previous byte was a CR
		int j = off;
		for (int i = off; i < off + count; ++i) { // invariant: j <= i
			lastByte = buffer[i];
			if (lastByte == '\r') {
				if (pendingByte) {
					buffer[j++] = '\r'; // write out orphan CR
				} else {
					pendingByte = true;
				}
			} else {
				if (pendingByte) {
					if (lastByte != '\n') buffer[j++] = '\r'; // if LF, don't write the CR
					pendingByte = false;
				}
				buffer[j++] = (byte) lastByte;
			}
		}
		if (iioe != null) {
			iioe.bytesTransferred = j - off;
			throw iioe;
		}
		return j - off;
	}

	/**
	 * Calls read() to skip the specified number of bytes
	 * @throws InterruptedIOException if the operation was interrupted before all of the
	 *         bytes specified have been skipped, bytesTransferred may be non-zero
	 * @throws IOException if an i/o error occurs
	 */
	public long skip(long count) throws IOException {
		int actualCount = 0; // assumes count < Integer.MAX_INT
		try {
			while (count-- > 0 && read() != -1) actualCount++; // skip the specified number of bytes
			return actualCount;
		} catch (InterruptedIOException e) {
			e.bytesTransferred = actualCount;
			throw e;
		}
	}

	/**
	 * Wraps the underlying stream's method.
	 * Returns the number of bytes that can be read without blocking; accounts for
	 * possible translation of CR/LF sequences to LFs in these bytes.
	 * @throws IOException if an i/o error occurs
	 */
	public int available() throws IOException {
		return in.available() / 2; // we can guarantee at least this amount after contraction
	}

	/**
	 * Mark is not supported by the wrapper even if the underlying stream does, returns false.
	 */
	public boolean markSupported() {
		return false;
	}
}

Back to the top