1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/core/src/com/panayotis/jubler/os/ByteOrderFactory.java Sun Oct 04 18:29:20 2009 +0300
1.3 @@ -0,0 +1,73 @@
1.4 +/*
1.5 + * To change this template, choose Tools | Templates
1.6 + * and open the template in the editor.
1.7 + */
1.8 +package com.panayotis.jubler.os;
1.9 +
1.10 +import java.io.File;
1.11 +import java.io.FileInputStream;
1.12 +import java.io.IOException;
1.13 +
1.14 +/**
1.15 + *
1.16 + * @author teras
1.17 + */
1.18 +public class ByteOrderFactory {
1.19 +
1.20 + private final static ByteOrder[] orders;
1.21 +
1.22 + static {
1.23 + orders = new ByteOrder[3];
1.24 + orders[0] = new ByteOrder("EFBBBF", "UTF-8");
1.25 + orders[1] = new ByteOrder("FEFF", "UTF-16");
1.26 + orders[2] = new ByteOrder("FFFE", "UTF-16");
1.27 + }
1.28 +
1.29 + private static class ByteOrder {
1.30 +
1.31 + private byte[] tag;
1.32 + private String encoding;
1.33 + private static int MaxSize = 0;
1.34 +
1.35 + private ByteOrder(String stringtag, String enc) {
1.36 + int length = stringtag.length() / 2;
1.37 + tag = new byte[length];
1.38 + if (MaxSize < length)
1.39 + MaxSize = length;
1.40 + for (int i = 0; i < length; i++)
1.41 + tag[i] = (byte) Integer.decode("0x" + stringtag.substring(i * 2, i * 2 + 2)).intValue();
1.42 + encoding = enc;
1.43 + }
1.44 +
1.45 + private boolean match(byte[] test) {
1.46 + for (int i = 0; i < tag.length; i++)
1.47 + if (tag[i] != test[i])
1.48 + return false;
1.49 + return true;
1.50 + }
1.51 + }
1.52 +
1.53 + public static String getEncoding(File f) {
1.54 + if (f.length() < ByteOrder.MaxSize)
1.55 + return null;
1.56 + byte[] buffer = new byte[ByteOrder.MaxSize];
1.57 + FileInputStream in = null;
1.58 + try {
1.59 + in = new FileInputStream(f);
1.60 + in.read(buffer);
1.61 + for (int i = 0; i < orders.length; i++) {
1.62 + if (orders[i].match(buffer)) {
1.63 + in.close();
1.64 + return orders[i].encoding;
1.65 + }
1.66 + }
1.67 + } catch (IOException ex) {
1.68 + } finally {
1.69 + try {
1.70 + in.close();
1.71 + } catch (IOException ex) {
1.72 + }
1.73 + }
1.74 + return null;
1.75 + }
1.76 +}
2.1 --- a/core/src/com/panayotis/jubler/os/FileCommunicator.java Sun Oct 04 18:29:01 2009 +0300
2.2 +++ b/core/src/com/panayotis/jubler/os/FileCommunicator.java Sun Oct 04 18:29:20 2009 +0300
2.3 @@ -59,13 +59,22 @@
2.4
2.5 public static String load(SubFile sfile) {
2.6 String res;
2.7 + String enc;
2.8
2.9 - String enc = sfile.getEncoding();
2.10 /* First chech already known data */
2.11 + enc = sfile.getEncoding();
2.12 res = load(sfile, enc, _("Found defined encoding {0}", enc), false);
2.13 if (res != null)
2.14 return res;
2.15
2.16 + /* Then check if encoding is tagged on the file */
2.17 + enc = ByteOrderFactory.getEncoding(sfile.getSaveFile());
2.18 + if (enc != null) {
2.19 + res = load(sfile, enc, _("Found tagged encoding {0}", enc), false);
2.20 + if (res != null)
2.21 + return res;
2.22 + }
2.23 +
2.24 /* Then guess and be strict */
2.25 for (int i = 0; i < SubFile.getDefaultEncodingSize(); i++) {
2.26 res = load(sfile, enc, _("Found strict encoding {0}", enc), true);