Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 9b38ec76b20b490955a396536db0aaee557a8d58 (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
/*******************************************************************************
 * Copyright (c) 2004, 2017 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.core.mapping;

import java.io.*;
import java.util.ArrayList;

import org.eclipse.compare.rangedifferencer.IRangeComparator;
import org.eclipse.core.resources.IEncodedStorage;
import org.eclipse.core.resources.IStorage;
import org.eclipse.core.runtime.CoreException;

/**
 * This implementation of IRangeComparator breaks an input stream into lines.
 * Copied from org.eclipse.compare.internal.merge.LineComparator 1.4 and
 * modified for {@link IStorage}.
 */
class LineComparator implements IRangeComparator {

	private String[] fLines;

	/*
	 * An input stream reader that detects a trailing LF in the wrapped stream.
	 */
	private static class TrailingLineFeedDetector extends FilterInputStream {

		boolean trailingLF = false;

		protected TrailingLineFeedDetector(InputStream in) {
			super(in);
		}

		@Override
		public int read() throws IOException {
			int c = super.read();
			trailingLF = isLineFeed(c);
			return c;
		}

		/*
		 * We don't need to override read(byte[] buffer) as the javadoc of
		 * FilterInputStream states that it will call read(byte[] buffer, int
		 * off, int len)
		 */
		@Override
		public int read(byte[] buffer, int off, int len) throws IOException {
			int length = super.read(buffer, off, len);
			if (length != -1) {
				int index = off + length - 1;
				if (index >= buffer.length)
					index = buffer.length - 1;
				trailingLF = isLineFeed(buffer[index]);
			}
			return length;
		}

		private boolean isLineFeed(int c) {
			return c != -1 && c == '\n';
		}

		public boolean hadTrailingLineFeed() {
			return trailingLF;
		}

	}

	public static LineComparator create(IStorage storage, String outputEncoding)
			throws CoreException, IOException {
		InputStream is = new BufferedInputStream(storage.getContents());
		try {
			String encoding = getEncoding(storage, outputEncoding);
			return new LineComparator(is, encoding);
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				// Ignore
			}
		}
	}

	private static String getEncoding(IStorage storage, String outputEncoding)
			throws CoreException {
		if (storage instanceof IEncodedStorage) {
			IEncodedStorage es = (IEncodedStorage) storage;
			String charset = es.getCharset();
			if (charset != null)
				return charset;
		}
		return outputEncoding;
	}

	public LineComparator(InputStream is, String encoding) throws IOException {

		TrailingLineFeedDetector trailingLineFeedDetector = new TrailingLineFeedDetector(
				is);
		try (BufferedReader br = new BufferedReader(new InputStreamReader(
				trailingLineFeedDetector, encoding))) {
			String line;
			ArrayList<String> ar = new ArrayList<>();
			while ((line = br.readLine()) != null) {
				ar.add(line);
			}
			// Add a trailing line if the last character in the file was a line
			// feed.
			// We do this because a BufferedReader doesn't distinguish the case
			// where the last line has or doesn't have a trailing line separator
			if (trailingLineFeedDetector.hadTrailingLineFeed()) {
				ar.add(""); //$NON-NLS-1$
			}
			fLines = ar.toArray(new String[ar.size()]);
		}
	}

	String getLine(int ix) {
		return fLines[ix];
	}

	@Override
	public int getRangeCount() {
		return fLines.length;
	}

	@Override
	public boolean rangesEqual(int thisIndex, IRangeComparator other,
			int otherIndex) {
		String s1 = fLines[thisIndex];
		String s2 = ((LineComparator) other).fLines[otherIndex];
		return s1.equals(s2);
	}

	@Override
	public boolean skipRangeComparison(int length, int maxLength,
			IRangeComparator other) {
		return false;
	}
}

Back to the top