|
1 | 1 | package com.zhekasmirnov.horizon.util; |
2 | 2 |
|
| 3 | +import org.json.JSONArray; |
3 | 4 | import org.json.JSONException; |
4 | 5 | import org.json.JSONObject; |
5 | 6 |
|
6 | 7 | import java.io.*; |
| 8 | +import java.nio.ByteBuffer; |
| 9 | +import java.nio.channels.Channels; |
| 10 | +import java.nio.channels.ReadableByteChannel; |
| 11 | +import java.nio.channels.WritableByteChannel; |
7 | 12 | import java.util.ArrayList; |
8 | 13 | import java.util.List; |
9 | 14 |
|
@@ -56,6 +61,11 @@ public static void writeJSON(File file, JSONObject jsonObject) throws IOExceptio |
56 | 61 | FileUtils.writeFileText(file, result); |
57 | 62 | } |
58 | 63 |
|
| 64 | + public static void writeJSON(File file, JSONArray jsonObject) throws IOException { |
| 65 | + String result = jsonObject.toString(); |
| 66 | + FileUtils.writeFileText(file, result); |
| 67 | + } |
| 68 | + |
59 | 69 | public static String cleanupPath(String path) { |
60 | 70 | path = path.replaceAll("\\\\", "/"); |
61 | 71 | while (path.startsWith("/")) { |
@@ -101,4 +111,42 @@ public static void setFileFlag(File directory, String name, boolean exists) { |
101 | 111 | flag.delete(); |
102 | 112 | } |
103 | 113 | } |
| 114 | + |
| 115 | + public static void copy(File src, File dst) throws IOException { |
| 116 | + FileInputStream inputStream = new FileInputStream(src); |
| 117 | + FileOutputStream outputStream = new FileOutputStream(dst); |
| 118 | + |
| 119 | + ReadableByteChannel inputChannel = Channels.newChannel(inputStream); |
| 120 | + WritableByteChannel outputChannel = Channels.newChannel(outputStream); |
| 121 | + |
| 122 | + final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); |
| 123 | + while (inputChannel.read(buffer) != -1) { |
| 124 | + buffer.flip(); |
| 125 | + outputChannel.write(buffer); |
| 126 | + buffer.compact(); |
| 127 | + } |
| 128 | + buffer.flip(); |
| 129 | + while (buffer.hasRemaining()) { |
| 130 | + outputChannel.write(buffer); |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + |
| 135 | + public static void copyFileTree(File srcDir, File dstDir, Object dialog, String prefix) throws IOException { |
| 136 | + if (!dstDir.isDirectory() && !dstDir.mkdirs()) { |
| 137 | + throw new IOException("mkdirs failed: " + dstDir); |
| 138 | + } |
| 139 | + for (String name : srcDir.list()) { |
| 140 | + if (dialog != null) { |
| 141 | + return; |
| 142 | + } |
| 143 | + File srcFile = new File(srcDir, name); |
| 144 | + File dstFile = new File(dstDir, name); |
| 145 | + if (srcFile.isDirectory()) { |
| 146 | + copyFileTree(srcFile, dstFile, dialog, prefix + name + "/"); |
| 147 | + } else { |
| 148 | + copy(srcFile, dstFile); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
104 | 152 | } |
0 commit comments