aboutsummaryrefslogtreecommitdiffstats
path: root/trunk/infrastructure/net.appjet.common/util/BetterFile.java
blob: c67481083a8a24e35ae2cb23c42382c7577fec84 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.appjet.common.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.*;


/**
 * A bunch of stuff that should've been in the Java Standard Libraries.
 */
public class BetterFile {
    public static String getFileContents(File f, boolean t) throws FileNotFoundException {
        FileInputStream in;
        try {
            in = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            if (t) throw e;
            return null;
        }
        return getStreamContents(in);
    }

    public static String getFileContents(File f) {
        try {
            return getFileContents(f, false);
        } catch (FileNotFoundException e) {
            // won't ever get here.
        }  
        return null;
    }
    
    public static String getBinaryFileContents(File f) {
        FileInputStream in;
        try {
            in = new FileInputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        }

        return getBinaryStreamContents(in);
    }

    // Using the non-converting String contructor here. Yum.
    @SuppressWarnings({"deprecation"})
    public static String getBinaryStreamContents(InputStream in) {
        StringBuilder out = new StringBuilder();
        byte[] b = new byte[4096];
        try {
            for (int n; (n = in.read(b)) != -1 ;) {
                out.append(new String(b, 0, 0, n));
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return out.toString();
    }

    public static String getStreamContents(InputStream instream) {
        InputStreamReader in = new InputStreamReader(instream, java.nio.charset.Charset.forName("UTF-8"));
        StringBuilder out = new StringBuilder();

        char[] b = new char[4096];
        try {
            for (int n; (n = in.read(b)) != -1; ){
                out.append(b, 0, n);
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return out.toString();
    }        

    public static String getBinaryFileContents(String filename) {
        return getBinaryFileContents(new File(filename));
    }

    public static String getFileContents(String filename) {
        return getFileContents(new File(filename));
    }

    public static String getFileContents(String filename, boolean t) throws FileNotFoundException {
        return getFileContents(new File(filename), t);
    }

    public static byte[] getStreamBytes(InputStream instream) {
        byte[] b = new byte[8192];
        ByteArrayOutputStream baos = new ByteArrayOutputStream(16384);
        try {
            for (int n; (n = instream.read(b)) != -1;) {
                baos.write(b, 0, n);
            }
            instream.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        return baos.toByteArray();
    }
    
    public static String getReaderString(BufferedReader reader) {
      StringBuffer out = new StringBuffer();
      char[] c = new char[8192];
      try {
        for (int n; (n = reader.read(c, 0, c.length)) != -1;) {
          out.append(c, 0, n);
        }
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
        return null;
      }
      return out.toString();
    }

    public static byte[] getFileBytes(File f) {
        try {
            return getStreamBytes(new FileInputStream(f));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static byte[] getFileBytes(String filename) {
        return getFileBytes(new File(filename));
    }

    public static String getUnicodeFile(File f) throws FileNotFoundException {
            return getReaderString(new BufferedReader(new UnicodeReader(new FileInputStream(f), null)));
    }

    public static String getUnicodeFile(String filename) throws FileNotFoundException {
            return getUnicodeFile(new File(filename));
    }
    
    public static String stringFromUnicode(byte[] bytes) {
            return getReaderString(new BufferedReader(new UnicodeReader(new ByteArrayInputStream(bytes), null)));
    }

    // Ripped from: http://koti.mbnet.fi/akini/java/unicodereader/
    /**
     version: 1.1 / 2007-01-25
     - changed BOM recognition ordering (longer boms first)

     Original pseudocode   : Thomas Weidenfeller
     Implementation tweaked: Aki Nieminen

     http://www.unicode.org/unicode/faq/utf_bom.html
     BOMs:
       00 00 FE FF    = UTF-32, big-endian
       FF FE 00 00    = UTF-32, little-endian
       EF BB BF       = UTF-8,
       FE FF          = UTF-16, big-endian
       FF FE          = UTF-16, little-endian

     Win2k Notepad:
       Unicode format = UTF-16LE
    ***/

    /**
     * Generic unicode textreader, which will use BOM mark
     * to identify the encoding to be used. If BOM is not found
     * then use a given default or system encoding.
     */
    public static class UnicodeReader extends Reader {
       PushbackInputStream internalIn;
       InputStreamReader   internalIn2 = null;
       String              defaultEnc;

       private static final int BOM_SIZE = 4;

       /**
        *
        * @param in  inputstream to be read
        * @param defaultEnc default encoding if stream does not have 
        *                   BOM marker. Give NULL to use system-level default.
        */
       UnicodeReader(InputStream in, String defaultEnc) {
          internalIn = new PushbackInputStream(in, BOM_SIZE);
          this.defaultEnc = defaultEnc;
       }

       public String getDefaultEncoding() {
          return defaultEnc;
       }

       /**
        * Get stream encoding or NULL if stream is uninitialized.
        * Call init() or read() method to initialize it.
        */
       public String getEncoding() {
          if (internalIn2 == null) return null;
          return internalIn2.getEncoding();
       }

       /**
        * Read-ahead four bytes and check for BOM marks. Extra bytes are
        * unread back to the stream, only BOM bytes are skipped.
        */
       protected void init() throws IOException {
          if (internalIn2 != null) return;

          String encoding;
          byte bom[] = new byte[BOM_SIZE];
          int n, unread;
          n = internalIn.read(bom, 0, bom.length);

          if ( (bom[0] == (byte)0x00) && (bom[1] == (byte)0x00) &&
                      (bom[2] == (byte)0xFE) && (bom[3] == (byte)0xFF) ) {
             encoding = "UTF-32BE";
             unread = n - 4;
          } else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) &&
                      (bom[2] == (byte)0x00) && (bom[3] == (byte)0x00) ) {
             encoding = "UTF-32LE";
             unread = n - 4;
          } else if (  (bom[0] == (byte)0xEF) && (bom[1] == (byte)0xBB) &&
                (bom[2] == (byte)0xBF) ) {
             encoding = "UTF-8";
             unread = n - 3;
          } else if ( (bom[0] == (byte)0xFE) && (bom[1] == (byte)0xFF) ) {
             encoding = "UTF-16BE";
             unread = n - 2;
          } else if ( (bom[0] == (byte)0xFF) && (bom[1] == (byte)0xFE) ) {
             encoding = "UTF-16LE";
             unread = n - 2;
          } else {
             // Unicode BOM mark not found, unread all bytes
             encoding = defaultEnc;
             unread = n;
          }    
          //System.out.println("read=" + n + ", unread=" + unread);

          if (unread > 0) internalIn.unread(bom, (n - unread), unread);

          // Use given encoding
          if (encoding == null) {
             internalIn2 = new InputStreamReader(internalIn);
          } else {
             internalIn2 = new InputStreamReader(internalIn, encoding);
          }
       }

       public void close() throws IOException {
          init();
          internalIn2.close();
       }

       public int read(char[] cbuf, int off, int len) throws IOException {
          init();
          return internalIn2.read(cbuf, off, len);
       }

    }
}