summaryrefslogtreecommitdiff
path: root/base/test/android/javatests/src/org/chromium/base/test/util/TestFileUtil.java
blob: 8765def89525d0a9f35aacedd7b40d77f9cb93d4 (plain)
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
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package org.chromium.base.test.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Arrays;

/**
 * Utility class for dealing with files for test.
 */
public class TestFileUtil {
    public static void createNewHtmlFile(String name, String title, String body)
            throws IOException {
        File file = new File(name);
        if (!file.createNewFile()) {
            throw new IOException("File \"" + name + "\" already exists");
        }

        Writer writer = null;
        try {
            writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8");
            writer.write("<html><meta charset=\"UTF-8\" />"
                    + "     <head><title>" + title + "</title></head>"
                    + "     <body>"
                    + (body != null ? body : "")
                    + "     </body>"
                    + "   </html>");
        } finally {
            if (writer != null) {
                writer.close();
            }
        }
    }

    public static void deleteFile(String name) {
        File file = new File(name);
        boolean deleted = file.delete();
        assert (deleted || !file.exists());
    }

    /**
     * @param fileName the file to read in.
     * @param sizeLimit cap on the file size: will throw an exception if exceeded
     * @return Array of chars read from the file
     * @throws FileNotFoundException file does not exceed
     * @throws IOException error encountered accessing the file
     */
    public static char[] readUtf8File(String fileName, int sizeLimit) throws
            FileNotFoundException, IOException {
        Reader reader = null;
        try {
            File f = new File(fileName);
            if (f.length() > sizeLimit) {
                throw new IOException("File " + fileName + " length " + f.length()
                        + " exceeds limit " + sizeLimit);
            }
            char[] buffer = new char[(int) f.length()];
            reader = new InputStreamReader(new FileInputStream(f), "UTF-8");
            int charsRead = reader.read(buffer);
            // Debug check that we've exhausted the input stream (will fail e.g. if the
            // file grew after we inspected its length).
            assert !reader.ready();
            return charsRead < buffer.length ? Arrays.copyOfRange(buffer, 0, charsRead) : buffer;
        } finally {
            if (reader != null) reader.close();
        }
    }
}