Permalink
Browse files

New version of ceylon.io

  • Loading branch information...
1 parent 53ce5f3 commit 9acd9956ab9bb273085058a3d15dba876ea44118 @FroMage committed Aug 24, 2012
View
@@ -1,8 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="source"/>
- <classpathentry kind="con" path="com.redhat.ceylon.eclipse.ui.cpcontainer.CEYLON_CONTAINER/default"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
- <classpathentry kind="lib" path="/home/stephane/.ceylon/repo/ceylon/collection/0.3.1/ceylon.collection-0.3.1.car"/>
+ <classpathentry kind="con" path="com.redhat.ceylon.eclipse.ui.cpcontainer.CEYLON_CONTAINER/default"/>
<classpathentry kind="output" path="modules"/>
</classpath>
@@ -1,8 +1,13 @@
import ceylon.io.buffer.impl { ByteBufferImpl }
-shared abstract class Buffer() satisfies Iterable<Integer> {
+
+shared abstract class Buffer<T>() satisfies Iterable<T> {
shared formal Integer position;
shared formal Integer capacity;
shared formal Integer limit;
+
+ shared formal void resize(Integer newSize);
+
+ shared default Boolean writable = true;
shared Boolean hasAvailable {
return available > 0;
@@ -12,15 +17,14 @@ shared abstract class Buffer() satisfies Iterable<Integer> {
return limit - position;
}
- shared formal Integer get();
- shared formal void put(Integer byte);
- shared formal void reset();
+ shared formal T get();
+ shared formal void put(T element);
shared formal void clear();
shared formal void flip();
- shared actual Iterator<Integer> iterator {
- object it satisfies Iterator<Integer> {
- shared actual Integer|Finished next() {
+ shared actual Iterator<T> iterator {
+ object it satisfies Iterator<T> {
+ shared actual T|Finished next() {
if(hasAvailable){
return get();
}
@@ -31,11 +35,54 @@ shared abstract class Buffer() satisfies Iterable<Integer> {
}
}
-shared Buffer newByteBuffer(Integer capacity){
+shared abstract class ByteBuffer() extends Buffer<Integer>(){
+}
+
+shared class CharacterBuffer(String str) extends Buffer<Character>(){
+ shared actual Integer capacity = str.size;
+ shared variable actual Integer limit := str.size;
+ shared variable actual Integer position := 0;
+
+ shared actual Boolean writable = false;
+
+ shared actual void clear() {
+ position := 0;
+ limit := capacity;
+ }
+
+ shared actual void flip() {
+ limit := position;
+ position := 0;
+ }
+
+ shared actual Character get() {
+ if(is Character c = str[position++]){
+ return c;
+ }
+ // FIXME: type
+ throw Exception("Buffer depleted");
+ }
+
+ shared actual void put(Character element) {
+ // FIXME: type
+ throw Exception("Buffer is read-only");
+ }
+
+ shared actual void resize(Integer integer) {
+ // FIXME: type
+ throw Exception("Buffer is read-only");
+ }
+}
+
+shared CharacterBuffer newCharacterBufferWithData(String data){
+ return CharacterBuffer(data);
+}
+
+shared ByteBuffer newByteBuffer(Integer capacity){
return ByteBufferImpl(capacity);
}
-shared Buffer newByteBufferWithData(Integer... bytes){
+shared ByteBuffer newByteBufferWithData(Integer... bytes){
value seq = bytes.sequence;
value buf = newByteBuffer(seq.size);
for(Integer byte in seq){
@@ -1,12 +1,17 @@
import java.nio { JavaByteBuffer = ByteBuffer { allocateJavaByteBuffer = allocate }}
-import ceylon.io.buffer { Buffer }
+import ceylon.io.buffer { ByteBuffer }
Boolean needsWorkarounds = true;
-shared class ByteBufferImpl(capacity) extends Buffer(){
- shared JavaByteBuffer buf = allocateJavaByteBuffer(capacity);
+shared class ByteBufferImpl(Integer initialCapacity) extends ByteBuffer(){
+ variable JavaByteBuffer buf := allocateJavaByteBuffer(initialCapacity);
+ shared JavaByteBuffer underlyingBuffer {
+ return buf;
+ }
- shared actual Integer capacity;
+ shared actual Integer capacity {
+ return buf.capacity();
+ }
shared actual Integer limit {
return buf.limit();
}
@@ -19,9 +24,6 @@ shared class ByteBufferImpl(capacity) extends Buffer(){
shared actual void put(Integer byte) {
buf.put(unsignedByteToSigned(byte));
}
- shared actual void reset() {
- buf.reset();
- }
shared actual void clear() {
buf.clear();
}
@@ -41,5 +43,31 @@ shared class ByteBufferImpl(capacity) extends Buffer(){
}
return b;
}
+
+ shared actual void resize(Integer newSize) {
+ if(newSize == capacity){
+ return;
+ }
+ if(newSize < 0){
+ // FIXME: type
+ throw;
+ }
+ JavaByteBuffer dest = allocateJavaByteBuffer(newSize);
+ // save our position and limit
+ value position = min({this.position, newSize});
+ value limit = min({this.limit, newSize});
+ // copy everything unless we shrink
+ value copyUntil = min({this.capacity, newSize});
+ // prepare our limits for copying
+ buf.position(0);
+ buf.limit(copyUntil);
+ // copy
+ dest.put(buf);
+ // change buffer
+ buf := dest;
+ // now restore positions
+ buf.limit(limit);
+ buf.position(position);
+ }
}
Oops, something went wrong.

0 comments on commit 9acd995

Please sign in to comment.