01: /*
02: * :tabSize=8:indentSize=8:noTabs=false:
03: * :folding=explicit:collapseFolds=1:
04: *
05: * Copyright (C) 2007 Kazutoshi Satoda
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or any later version.
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: */
20:
21: package org.gjt.sp.jedit.io;
22:
23: import java.io.InputStream;
24: import java.io.InputStreamReader;
25: import java.io.BufferedReader;
26: import java.io.IOException;
27:
28: /**
29: * An encoding detector which finds buffer-local-property syntax.
30: *
31: * This reads the sample in the system default encoding for first 10
32: * lines and look for ":encoding=..." syntax. This can fail if the
33: * stream cannot be read in the system default encoding or
34: * ":encoding=..." is not placed at near the top of the stream.
35: *
36: * @since 4.3pre10
37: * @author Kazutoshi Satoda
38: */
39: public class BufferLocalEncodingDetector implements EncodingDetector {
40: public String detectEncoding(InputStream sample) throws IOException {
41: BufferedReader reader = new BufferedReader(
42: new InputStreamReader(sample));
43: int i = 0;
44: while (i < 10) {
45: i++;
46: String line = reader.readLine();
47: if (line == null)
48: return null;
49: int pos = line.indexOf(":encoding=");
50: if (pos != -1) {
51: int p2 = line.indexOf(':', pos + 10);
52: String encoding = line.substring(pos + 10, p2);
53: return encoding;
54: }
55: }
56: return null;
57: }
58: }
|