Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Added data io streams for arbitrary data read/write. Added array io s…
…treams for array read/write. Deleted overarching thread code and reverted see.concurrent.Thread to its original state.
- Loading branch information
James King
committed
Aug 11, 2013
1 parent
2a94714
commit f1224cf
Showing
7 changed files
with
161 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --@import see.io.InputStream | ||
|
|
||
| --@extends see.io.InputStream | ||
|
|
||
| --[[ | ||
| Wraps around an array to read from. | ||
| @param Array:wrap The array to wrap around. | ||
| ]] | ||
| function ArrayInputStream:init(wrap) | ||
| self.wrap = wrap | ||
| self.position = 1 | ||
| end | ||
|
|
||
| function ArrayInputStream:read() | ||
| self.position = self.position + 1 | ||
| return self.wrap[self.position - 1] | ||
| end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --@import see.io.OutputStream | ||
|
|
||
| --@extends see.io.OutputStream | ||
|
|
||
| --[[ | ||
| Wraps around an array to write to. | ||
| @param Array:wrap The array to wrap around. | ||
| ]] | ||
| function ArrayOutputStream:init(wrap) | ||
| self.wrap = wrap | ||
| self.position = 1 | ||
| end | ||
|
|
||
| function ArrayOutputStream:write(b) | ||
| self.wrap[self.position] = b | ||
| self.position = self.position + 1 | ||
| end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| --@native bit | ||
|
|
||
| --@import see.io.InputStream | ||
|
|
||
| --@extends see.io.InputStream | ||
|
|
||
| --[[ | ||
| An InputStream for doing more advanced operations on other InputStreams. | ||
| ]] | ||
|
|
||
| --[[ | ||
| Wraps arounds a see.io.InputStream. | ||
| @param see.io.InputStream:wrap The InputStream to wrap around. | ||
| ]] | ||
| function DataInputStream:init(wrap) | ||
| self.wrap = wrap | ||
| end | ||
|
|
||
| function DataInputStream:read() | ||
| return self.wrap:read() | ||
| end | ||
|
|
||
| --[[ | ||
| Reads an integer with the given amount of bytes. | ||
| @param number:bytes The number of bytes to encode the number with. | ||
| @return number The integer that was read. | ||
| ]] | ||
| function DataInputStream:readInt(bytes) | ||
| local ret = 0 | ||
| local b | ||
| for i = bytes, 1, -1 do | ||
| b = self:read() | ||
| ret = bit.bor(ret, bit.blshift(b, (i - 1) * 8)) | ||
| end | ||
| return ret | ||
| end | ||
|
|
||
| --[[ | ||
| Reads a string of a specific length. | ||
| @param number:len The length of the string to read. | ||
| @return see.base.String The string that was read. | ||
| ]] | ||
| function DataInputStream:readString(len) | ||
| local ret = String.new() | ||
| for i = 1, len do | ||
| ret[i] = self:read() | ||
| end | ||
| return ret | ||
| end | ||
|
|
||
| function DataInputStream:flush() | ||
| self.wrap:flush() | ||
| end | ||
|
|
||
| function DataInputStream:close() | ||
| self.wrap:close() | ||
| end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| --@native bit | ||
|
|
||
| --@import see.io.OutputStream | ||
|
|
||
| --@extends see.io.OutputStream | ||
|
|
||
| --[[ | ||
| An OutputStream for doing more advanced operations on other OutputStreams. | ||
| ]] | ||
|
|
||
| --[[ | ||
| Wraps arounds a see.io.OutputStream. | ||
| @param see.io.OutputStream:wrap The OutputStream to wrap around. | ||
| ]] | ||
| function DataOutputStream:init(wrap) | ||
| self.wrap = wrap | ||
| end | ||
|
|
||
| function DataOutputStream:write(b) | ||
| self.wrap:write(b) | ||
| end | ||
|
|
||
| --[[ | ||
| Writes an integer with the given amount of bytes. | ||
| @param number:value The value to write. | ||
| @param number:bytes The number of bytes to encode the number with. | ||
| ]] | ||
| function DataOutputStream:writeInt(value, bytes) | ||
| for i = bytes, 1, -1 do | ||
| self:write(bit.band(0xff, bit.brshift(value, (i - 1) * 8))) | ||
| end | ||
| end | ||
|
|
||
| --[[ | ||
| Writes a string. | ||
| @param see.base.String:str The string to write. | ||
| ]] | ||
| function DataOutputStream:writeString(str) | ||
| str = cast(str, String) | ||
| for i = 1, str:length() do | ||
| self:write(str[i]) | ||
| end | ||
| end | ||
|
|
||
| function DataOutputStream:flush() | ||
| self.wrap:flush() | ||
| end | ||
|
|
||
| function DataOutputStream:close() | ||
| self.wrap:close() | ||
| end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,18 @@ | ||
| --@import see.io.Path | ||
| --@import see.io.FileOutputStream | ||
| --@import see.io.FileInputStream | ||
| --@import see.io.Path | ||
| --@import see.io.Files | ||
| --@import see.io.DataOutputStream | ||
| --@import see.io.DataInputStream | ||
| --@import see.io.ArrayOutputStream | ||
| --@import see.io.ArrayInputStream | ||
|
|
||
| function Test.main() | ||
| local path = Path.new("/fostest") | ||
|
|
||
| if not Files.exists(path) then | ||
| System.print("File does not exist!") | ||
| return | ||
| end | ||
| local path = Path.new("/filetest") | ||
| local array = Array.new() | ||
|
|
||
| local fos = FileOutputStream.new(path) | ||
| fos:write(133) | ||
| fos:close() | ||
| local dos = DataOutputStream.new(ArrayOutputStream.new(array)) | ||
| dos:writeString("Hello!\n") | ||
| local dis = DataInputStream.new(ArrayInputStream.new(array)) | ||
|
|
||
| local fis = FileInputStream.new(path) | ||
| System.print(fis:read()) | ||
| fis:close() | ||
| System.write(dis:readString(array:length())) | ||
| end |