| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 // | 4 // |
| 5 // Dart test program for testing file I/O. | 5 // Dart test program for testing file I/O. |
| 6 | 6 |
| 7 #import("dart:io"); | 7 #import("dart:io"); |
| 8 #import("dart:isolate"); | 8 #import("dart:isolate"); |
| 9 | 9 |
| 10 class MyListOfOneElement implements List { | 10 class MyListOfOneElement implements List { |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 static void testReadWriteStream() { | 71 static void testReadWriteStream() { |
| 72 asyncTestStarted(); | 72 asyncTestStarted(); |
| 73 | 73 |
| 74 // Read a file. | 74 // Read a file. |
| 75 String inFilename = getFilename("tests/vm/data/fixed_length_file"); | 75 String inFilename = getFilename("tests/vm/data/fixed_length_file"); |
| 76 File file; | 76 File file; |
| 77 InputStream input; | 77 InputStream input; |
| 78 int bytesRead; | 78 int bytesRead; |
| 79 | 79 |
| 80 // Test reading all using readInto. | 80 // Test reading all using readInto. |
| 81 var file1 = new File(inFilename); | 81 file = new File(inFilename); |
| 82 var input1 = file1.openInputStream(); | 82 input = file.openInputStream(); |
| 83 List<int> buffer1; | 83 input.onData = () { |
| 84 input1.onData = () { | 84 List<int> buffer1 = new List<int>(42); |
| 85 buffer1 = new List<int>(42); | 85 bytesRead = input.readInto(buffer1, 0, 42); |
| 86 bytesRead = input1.readInto(buffer1, 0, 42); | |
| 87 Expect.equals(42, bytesRead); | 86 Expect.equals(42, bytesRead); |
| 88 }; | 87 Expect.isTrue(input.closed); |
| 89 input1.onError = (e) { throw e; }; | |
| 90 input1.onClosed = () { | |
| 91 Expect.isTrue(input1.closed); | |
| 92 | 88 |
| 93 // Test reading all using readInto and read. | 89 // Test reading all using readInto and read. |
| 94 var file2 = new File(inFilename); | 90 file = new File(inFilename); |
| 95 var input2 = file2.openInputStream(); | 91 input = file.openInputStream(); |
| 96 input2.onData = () { | 92 input.onData = () { |
| 97 bytesRead = input2.readInto(buffer1, 0, 21); | 93 bytesRead = input.readInto(buffer1, 0, 21); |
| 98 Expect.equals(21, bytesRead); | 94 Expect.equals(21, bytesRead); |
| 99 buffer1 = input2.read(); | 95 buffer1 = input.read(); |
| 100 Expect.equals(21, buffer1.length); | 96 Expect.equals(21, buffer1.length); |
| 101 }; | 97 Expect.isTrue(input.closed); |
| 102 input2.onError = (e) { throw e; }; | |
| 103 input2.onClosed = () { | |
| 104 Expect.isTrue(input2.closed); | |
| 105 | 98 |
| 106 // Test reading all using read and readInto. | 99 // Test reading all using read and readInto. |
| 107 var file3 = new File(inFilename); | 100 file = new File(inFilename); |
| 108 var input3 = file3.openInputStream(); | 101 input = file.openInputStream(); |
| 109 input3.onData = () { | 102 input.onData = () { |
| 110 buffer1 = input3.read(21); | 103 buffer1 = input.read(21); |
| 111 Expect.equals(21, buffer1.length); | 104 Expect.equals(21, buffer1.length); |
| 112 bytesRead = input3.readInto(buffer1, 0, 21); | 105 bytesRead = input.readInto(buffer1, 0, 21); |
| 113 Expect.equals(21, bytesRead); | 106 Expect.equals(21, bytesRead); |
| 114 }; | 107 Expect.isTrue(input.closed); |
| 115 input3.onError = (e) { throw e; }; | |
| 116 input3.onClosed = () { | |
| 117 Expect.isTrue(input3.closed); | |
| 118 | 108 |
| 119 // Test reading all using read. | 109 // Test reading all using read. |
| 120 var file4 = new File(inFilename); | 110 file = new File(inFilename); |
| 121 var input4 = file4.openInputStream(); | 111 input = file.openInputStream(); |
| 122 input4.onData = () { | 112 input.onData = () { |
| 123 buffer1 = input4.read(); | 113 buffer1 = input.read(); |
| 124 Expect.equals(42, buffer1.length); | 114 Expect.equals(42, buffer1.length); |
| 125 }; | 115 Expect.isTrue(input.closed); |
| 126 input4.onError = (e) { throw e; }; | |
| 127 input4.onClosed = () { | |
| 128 Expect.isTrue(input4.closed); | |
| 129 | 116 |
| 130 // Write the contents of the file just read into another file. | 117 // Write the contents of the file just read into another file. |
| 131 String outFilename = | 118 String outFilename = |
| 132 tempDirectory.path.concat("/out_read_write_stream"); | 119 tempDirectory.path.concat("/out_read_write_stream"); |
| 133 file = new File(outFilename); | 120 file = new File(outFilename); |
| 134 OutputStream output = file.openOutputStream(); | 121 OutputStream output = file.openOutputStream(); |
| 135 bool writeDone = output.writeFrom(buffer1, 0, 42); | 122 bool writeDone = output.writeFrom(buffer1, 0, 42); |
| 136 Expect.equals(false, writeDone); | 123 Expect.equals(false, writeDone); |
| 137 output.onNoPendingWrites = () { | 124 output.onNoPendingWrites = () { |
| 138 output.close(); | 125 output.close(); |
| 139 output.onClosed = () { | 126 output.onClosed = () { |
| 140 // Now read the contents of the file just written. | 127 // Now read the contents of the file just written. |
| 141 List<int> buffer2 = new List<int>(42); | 128 List<int> buffer2 = new List<int>(42); |
| 142 var file6 = new File(outFilename); | 129 file = new File(outFilename); |
| 143 var input6 = file6.openInputStream(); | 130 input = file.openInputStream(); |
| 144 input6.onData = () { | 131 input.onData = () { |
| 145 bytesRead = input6.readInto(buffer2, 0, 42); | 132 bytesRead = input.readInto(buffer2, 0, 42); |
| 146 Expect.equals(42, bytesRead); | 133 Expect.equals(42, bytesRead); |
| 147 // Now compare the two buffers to check if they are identical. | 134 // Now compare the two buffers to check if they are identical. |
| 148 for (int i = 0; i < buffer1.length; i++) { | 135 for (int i = 0; i < buffer1.length; i++) { |
| 149 Expect.equals(buffer1[i], buffer2[i]); | 136 Expect.equals(buffer1[i], buffer2[i]); |
| 150 } | 137 } |
| 151 }; | 138 }; |
| 152 input6.onError = (e) { throw e; }; | 139 input.onClosed = () { |
| 153 input6.onClosed = () { | |
| 154 // Delete the output file. | 140 // Delete the output file. |
| 155 file6.deleteSync(); | 141 file.deleteSync(); |
| 156 Expect.isFalse(file6.existsSync()); | 142 Expect.isFalse(file.existsSync()); |
| 157 asyncTestDone("testReadWriteStream"); | 143 asyncTestDone("testReadWriteStream"); |
| 158 }; | 144 }; |
| 159 }; | 145 }; |
| 160 }; | 146 }; |
| 161 }; | 147 }; |
| 162 }; | 148 }; |
| 163 }; | 149 }; |
| 164 }; | 150 }; |
| 165 } | 151 } |
| 166 | 152 |
| (...skipping 928 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1095 testWriteVariousLists(); | 1081 testWriteVariousLists(); |
| 1096 testDirectory(); | 1082 testDirectory(); |
| 1097 testDirectorySync(); | 1083 testDirectorySync(); |
| 1098 }); | 1084 }); |
| 1099 } | 1085 } |
| 1100 } | 1086 } |
| 1101 | 1087 |
| 1102 main() { | 1088 main() { |
| 1103 FileTest.testMain(); | 1089 FileTest.testMain(); |
| 1104 } | 1090 } |
| OLD | NEW |