All pastes #756802 Raw Edit

Anonym

public java v1 · immutable
#756802 ·published 2007-10-31 19:17 UTC
rendered paste body
import java.io.*;public class XMLEncoding {	private final String[] UTF = { "3C", "0" };	private File file = null;	public XMLEncoding(File file) {		this.file = file;	}	public String encode() {		String coding = "Das Dokument ist codiert in: ";		coding += testDeclaration();		return coding;	}	private String testDeclaration() {		String coding = null;		try {			LineNumberReader reader = new LineNumberReader(new FileReader(file));			coding = reader.readLine();		} catch (FileNotFoundException e) {			e.printStackTrace();		} catch (IOException e) {			e.printStackTrace();		}		int start = coding.lastIndexOf("encoding=\"");		if (start == -1) {			return testBytes(coding);		} else {			int end = start;			while (coding.charAt(end) != '"') {				end++;			}			coding = coding.substring(start, end);		}		return coding;	}	private String testBytes(String coding) {		int charFormat = -1;		byte[] b = coding.getBytes();		if (String.format("%X", b[0]).equals(UTF[0])				&& String.format("%X", b[1]).equals(UTF[1])) {			charFormat = 0;		} else if (String.format("%X", b[0]).equals(UTF[1])				&& String.format("%X", b[1]).equals(UTF[0])) {			charFormat = 1;		}		switch (charFormat) {		case -1:			coding = "UTF 8";			break;		case 0:			coding = "UTF 16 LE";			break;		case 1:			coding = "UTF 16 BE";			break;		}		return coding;	}	public static void main(String args[]) {		if (args.length == 0) {			System.err.println("Bitte Dateipfad als Parameter übergeben");		} else {			XMLEncoding encoder = new XMLEncoding(new File(args[0]));			System.out.println(encoder.encode());		}	}}