OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.base.test; | 5 package org.chromium.base.test; |
6 | 6 |
7 import java.io.File; | 7 import java.io.File; |
8 import java.io.FileInputStream; | 8 import java.io.FileInputStream; |
9 import java.io.FileNotFoundException; | 9 import java.io.FileNotFoundException; |
| 10 import java.io.FileOutputStream; |
10 import java.io.IOException; | 11 import java.io.IOException; |
11 import java.io.InputStreamReader; | 12 import java.io.InputStreamReader; |
| 13 import java.io.OutputStreamWriter; |
12 import java.io.Reader; | 14 import java.io.Reader; |
| 15 import java.io.Writer; |
13 import java.util.Arrays; | 16 import java.util.Arrays; |
14 | 17 |
15 /** | 18 /** |
16 * Utility class for dealing with files for test. | 19 * Utility class for dealing with files for test. |
17 */ | 20 */ |
18 public class TestFileUtil { | 21 public class TestFileUtil { |
| 22 public static void createNewHtmlFile(String name, String title, String body) |
| 23 throws IOException { |
| 24 File file = new File(name); |
| 25 if (!file.createNewFile()) { |
| 26 throw new IOException("File \"" + name + "\" already exists"); |
| 27 } |
| 28 |
| 29 Writer writer = null; |
| 30 try { |
| 31 writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8")
; |
| 32 writer.write("<html><meta charset=\"UTF-8\" />" + |
| 33 "<head><title>" + title + "</title></head>" + |
| 34 "<body>" + |
| 35 (body != null ? body : "") + |
| 36 "</body>" + |
| 37 "</html>"); |
| 38 } finally { |
| 39 if (writer != null) { |
| 40 writer.close(); |
| 41 } |
| 42 } |
| 43 } |
| 44 |
| 45 public static void deleteFile(String name) { |
| 46 File file = new File(name); |
| 47 file.delete(); |
| 48 } |
| 49 |
19 /** | 50 /** |
20 * @param fileName the file to read in. | 51 * @param fileName the file to read in. |
21 * @param sizeLimit cap on the file size: will throw an exception if exceede
d | 52 * @param sizeLimit cap on the file size: will throw an exception if exceede
d |
22 * @return Array of chars read from the file | 53 * @return Array of chars read from the file |
23 * @throws FileNotFoundException file does not exceed | 54 * @throws FileNotFoundException file does not exceed |
24 * @throws IOException error encountered accessing the file | 55 * @throws IOException error encountered accessing the file |
25 */ | 56 */ |
26 public static char[] readUtf8File(String fileName, int sizeLimit) throws | 57 public static char[] readUtf8File(String fileName, int sizeLimit) throws |
27 FileNotFoundException, IOException { | 58 FileNotFoundException, IOException { |
28 Reader reader = null; | 59 Reader reader = null; |
29 try { | 60 try { |
30 File f = new File(fileName); | 61 File f = new File(fileName); |
31 if (f.length() > sizeLimit) { | 62 if (f.length() > sizeLimit) { |
32 throw new IOException("File " + fileName + " length " + f.length
() + | 63 throw new IOException("File " + fileName + " length " + f.length
() + |
33 " exceeds limit " + sizeLimit); | 64 " exceeds limit " + sizeLimit); |
34 } | 65 } |
35 char[] buffer = new char[(int) f.length()]; | 66 char[] buffer = new char[(int) f.length()]; |
36 reader = new InputStreamReader(new FileInputStream(f), "UTF-8"); | 67 reader = new InputStreamReader(new FileInputStream(f), "UTF-8"); |
37 int charsRead = reader.read(buffer); | 68 int charsRead = reader.read(buffer); |
38 // Debug check that we've exhausted the input stream (will fail e.g.
if the | 69 // Debug check that we've exhausted the input stream (will fail e.g.
if the |
39 // file grew after we inspected its length). | 70 // file grew after we inspected its length). |
40 assert !reader.ready(); | 71 assert !reader.ready(); |
41 return charsRead < buffer.length ? Arrays.copyOfRange(buffer, 0, cha
rsRead) : buffer; | 72 return charsRead < buffer.length ? Arrays.copyOfRange(buffer, 0, cha
rsRead) : buffer; |
42 } finally { | 73 } finally { |
43 if (reader != null) reader.close(); | 74 if (reader != null) reader.close(); |
44 } | 75 } |
45 } | 76 } |
46 } | 77 } |
OLD | NEW |