|
| 1 | +package org.schabi.newpipe; |
| 2 | + |
| 3 | +import com.grack.nanojson.JsonArray; |
| 4 | +import com.grack.nanojson.JsonObject; |
| 5 | +import com.grack.nanojson.JsonWriter; |
| 6 | + |
| 7 | +import java.io.BufferedWriter; |
| 8 | +import java.io.File; |
| 9 | +import java.io.FileWriter; |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.file.Files; |
| 12 | +import java.nio.file.Paths; |
| 13 | + |
| 14 | +/** |
| 15 | + * Util class to write file to disk |
| 16 | + * <p> |
| 17 | + * Can be used to debug and test, for example writing a service's JSON response |
| 18 | + * (especially useful if the response provided by the service is not documented) |
| 19 | + */ |
| 20 | +public class FileUtils { |
| 21 | + |
| 22 | + public static void createFile(String path, JsonObject content) throws IOException { |
| 23 | + createFile(path, jsonObjToString(content)); |
| 24 | + } |
| 25 | + |
| 26 | + public static void createFile(String path, JsonArray array) throws IOException { |
| 27 | + createFile(path, jsonArrayToString(array)); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Create a file given a path and its content. Create subdirectories if needed |
| 32 | + * |
| 33 | + * @param path the path to write the file, including the filename (and its extension) |
| 34 | + * @param content the content to write |
| 35 | + * @throws IOException |
| 36 | + */ |
| 37 | + public static void createFile(final String path, final String content) throws IOException { |
| 38 | + final String[] dirs = path.split("/"); |
| 39 | + if (dirs.length > 1) { |
| 40 | + String pathWithoutFileName = path.replace(dirs[dirs.length - 1], ""); |
| 41 | + if (!Files.exists(Paths.get(pathWithoutFileName))) { //create dirs if they don't exist |
| 42 | + if (!new File(pathWithoutFileName).mkdirs()) { |
| 43 | + throw new IOException("An error occurred while creating directories"); |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + writeFile(path, content); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Write a file to disk |
| 52 | + * |
| 53 | + * @param filename the file name (and its extension if wanted) |
| 54 | + * @param content the content to write |
| 55 | + * @throws IOException |
| 56 | + */ |
| 57 | + private static void writeFile(final String filename, final String content) throws IOException { |
| 58 | + final BufferedWriter writer = new BufferedWriter(new FileWriter(filename)); |
| 59 | + writer.write(content); |
| 60 | + writer.flush(); |
| 61 | + writer.close(); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Convert a JSON object to String |
| 66 | + * toString() does not produce a valid JSON string |
| 67 | + */ |
| 68 | + public static String jsonObjToString(JsonObject object) { |
| 69 | + return JsonWriter.string(object); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Convert a JSON array to String |
| 74 | + * toString() does not produce a valid JSON string |
| 75 | + */ |
| 76 | + public static String jsonArrayToString(JsonArray array) { |
| 77 | + return JsonWriter.string(array); |
| 78 | + } |
| 79 | +} |
0 commit comments