OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 class _FileInputStream implements FileInputStream { | 5 class _FileInputStream implements InputStream { |
6 _FileInputStream(File file) { | 6 _FileInputStream(File file) { |
7 _file = file; | 7 _file = file; |
8 } | 8 } |
9 | 9 |
10 bool read(List<int> buffer, int offset, int len, void callback()) { | 10 List<int> read([int len]) { |
11 int bytesRead = _file.readList(buffer, offset, len); | 11 int bytesToRead = available(); |
12 | 12 if (bytesToRead == 0) { |
13 if (bytesRead == len) { | 13 return null; |
14 if (callback != null) { | 14 } |
15 callback(); | 15 if (len != null) { |
| 16 if (len <= 0) { |
| 17 throw new StreamException("Illegal length $len"); |
| 18 } else if (bytesToRead > len) { |
| 19 bytesToRead = len; |
16 } | 20 } |
17 return true; | 21 } |
| 22 List<int> buffer = new List<int>(bytesToRead); |
| 23 int bytesRead = _file.readList(buffer, 0, bytesToRead); |
| 24 if (bytesRead < bytesToRead) { |
| 25 List<int> newBuffer = new List<int>(bytesRead); |
| 26 newBuffer.copyFrom(buffer, 0, 0, bytesRead); |
| 27 return newBuffer; |
18 } else { | 28 } else { |
19 throw "FileInputStream: read error"; | 29 return buffer; |
20 } | 30 } |
21 } | 31 } |
22 | 32 |
23 // TODO(srdjan): Reading whole file at once does not scale. Implement partial | 33 int readInto(List<int> buffer, int offset, int len) { |
24 // file reading and pattern checks. | 34 if (offset == null) offset = 0; |
25 void readUntil(List<int> pattern, void callback(List<int> buffer)) { | 35 if (len == null) len = buffer.length; |
26 List<int> buffer = new List<int>(_file.length); | 36 if (offset < 0) throw new StreamException("Illegal offset $offset"); |
27 int result = _file.readList(buffer, 0, _file.length); | 37 if (len < 0) throw new StreamException("Illegal length $len"); |
28 if (result > 0) { | 38 return _file.readList(buffer, offset, len); |
29 int index = indexOf(buffer, pattern); | |
30 if (index != -1) { | |
31 int resultBufferSize = index + pattern.length; | |
32 List<int> resultBuffer = new List<int>(resultBufferSize); | |
33 resultBuffer.copyFrom(buffer, 0, 0, resultBufferSize); | |
34 callback(resultBuffer); | |
35 } | |
36 } | |
37 } | 39 } |
38 | 40 |
39 // TODO(srdjan: move this method to Lists.dart (helper method). | 41 int available() { |
40 static int indexOf(List<int> buffer, List<int> pattern) { | 42 return _file.length - _file.position; |
41 if (pattern.length == 0) { | 43 } |
42 return buffer.length; | 44 |
43 } | 45 bool closed() { |
44 int len = buffer.length - pattern.length + 1; | 46 _file.position == _file.length; |
45 for (int index = 0; index < len; index++) { | 47 } |
46 bool match = true; | 48 |
47 for (int k = 0; k < pattern.length; k++) { | 49 void set dataHandler(void callback()) { |
48 if (buffer[index + k] != pattern[k]) { | 50 // TODO(sgjesse): How to handle this? |
49 match = false; | 51 } |
50 break; | 52 |
51 } | 53 void set closeHandler(void callback()) { |
52 } | 54 // TODO(sgjesse): How to handle this? |
53 if (match) { | 55 } |
54 return index; | 56 |
55 } | 57 void set errorHandler(void callback()) { |
56 } | 58 // TODO(sgjesse): How to handle this? |
57 return -1; | |
58 } | 59 } |
59 | 60 |
60 File _file; | 61 File _file; |
61 } | 62 } |
62 | 63 |
63 class _FileOutputStream implements FileOutputStream { | 64 |
| 65 class _FileOutputStream implements OutputStream { |
64 _FileOutputStream(File file) { | 66 _FileOutputStream(File file) { |
65 _file = file; | 67 _file = file; |
66 } | 68 } |
67 | 69 |
68 bool write(List<int> buffer, int offset, int len, void callback()) { | 70 bool write(List<int> buffer, int offset, int len, void callback()) { |
69 int bytesWritten = _file.writeList(buffer, offset, len); | 71 int bytesWritten = _file.writeList(buffer, offset, len); |
70 | 72 |
71 if (bytesWritten == len) { | 73 if (bytesWritten == len) { |
72 return true; | 74 return true; |
73 } else { | 75 } else { |
74 throw "FileOutputStream: write error"; | 76 throw "FileOutputStream: write error"; |
75 } | 77 } |
76 } | 78 } |
77 | 79 |
78 File _file; | 80 File _file; |
79 } | 81 } |
80 | 82 |
| 83 |
81 // Class for encapsulating the native implementation of files. | 84 // Class for encapsulating the native implementation of files. |
82 class _File extends FileNativeWrapper implements File { | 85 class _File extends FileNativeWrapper implements File { |
83 // Constructor for file. | 86 // Constructor for file. |
84 factory _File(String name, bool writable) { | 87 factory _File(String name, bool writable) { |
85 _File file = new _File._internal(); | 88 _File file = new _File._internal(); |
86 if (!file._openFile(name, writable)) { | 89 if (!file._openFile(name, writable)) { |
87 return null; | 90 return null; |
88 } | 91 } |
89 file._writeable = writable; | 92 file._writeable = writable; |
90 return file; | 93 return file; |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 void flush() { | 193 void flush() { |
191 int result = _flush(); | 194 int result = _flush(); |
192 if (result == -1) { | 195 if (result == -1) { |
193 throw new FileIOException("Error: flush failed"); | 196 throw new FileIOException("Error: flush failed"); |
194 } | 197 } |
195 } | 198 } |
196 int _flush() native "File_Flush"; | 199 int _flush() native "File_Flush"; |
197 | 200 |
198 // Each file has an unique InputStream. | 201 // Each file has an unique InputStream. |
199 InputStream get inputStream() { | 202 InputStream get inputStream() { |
200 if (_inputStream === null) { | 203 if (_inputStream == null) { |
201 _inputStream = new FileInputStream(this); | 204 _inputStream = new _FileInputStream(this); |
202 } | 205 } |
203 return _inputStream; | 206 return _inputStream; |
204 } | 207 } |
205 | 208 |
206 // Each file has an unique OutputStream. | 209 // Each file has an unique OutputStream. |
207 OutputStream get outputStream() { | 210 OutputStream get outputStream() { |
208 if (!_writeable) { | 211 if (!_writeable) { |
209 throw "File is not writable"; | 212 throw "File is not writable"; |
210 } | 213 } |
211 if (_outputStream === null) { | 214 if (_outputStream == null) { |
212 _outputStream = new FileOutputStream(this); | 215 _outputStream = new _FileOutputStream(this); |
213 } | 216 } |
214 return _outputStream; | 217 return _outputStream; |
215 } | 218 } |
216 | 219 |
217 // Set of native methods used to provide file functionality. | 220 // Set of native methods used to provide file functionality. |
218 static bool fileExists(String name) native "File_Exists"; | 221 static bool fileExists(String name) native "File_Exists"; |
219 | 222 |
220 bool _writeable; | 223 bool _writeable; |
221 InputStream _inputStream; | 224 InputStream _inputStream; |
222 OutputStream _outputStream; | 225 OutputStream _outputStream; |
223 } | 226 } |
OLD | NEW |