Skip to content
This repository has been archived by the owner on Sep 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request mongodb#31 from josephks/master
Browse files Browse the repository at this point in the history
Adding BasicOutputBuffer.pipe( DataOutput out )
  • Loading branch information
Ryan committed May 12, 2011
2 parents c495cfd + b275de4 commit 7aae216
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/main/org/bson/io/BasicOutputBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,69 @@

public class BasicOutputBuffer extends OutputBuffer {

@Override
public void write(byte[] b){
write( b , 0 , b.length );
}

@Override
public void write(byte[] b, int off, int len){
_ensure( len );
System.arraycopy( b , off , _buffer , _cur , len );
_cur += len;
_size = Math.max( _cur , _size );
}
@Override
public void write(int b){
_ensure(1);
_buffer[_cur++] = (byte)(0xFF&b);
_size = Math.max( _cur , _size );
}

@Override
public int getPosition(){
return _cur;
}
@Override
public void setPosition( int position ){
_cur = position;
}

@Override
public void seekEnd(){
_cur = _size;
}
@Override
public void seekStart(){
_cur = 0;
}

/**
* @return size of data so far
*/
@Override
public int size(){
return _size;
}

/**
* @return bytes written
*/
@Override
public int pipe( OutputStream out )
throws IOException {
out.write( _buffer , 0 , _size );
return _size;
}
/**
* @return bytes written
*/
public int pipe( DataOutput out )
throws IOException {
out.write( _buffer , 0 , _size );
return _size;
}


void _ensure( int more ){
final int need = _cur + more;
Expand All @@ -66,10 +84,12 @@ void _ensure( int more ){
_buffer = n;
}

@Override
public String asString(){
return new String( _buffer , 0 , _size );
}

@Override
public String asString( String encoding )
throws UnsupportedEncodingException {
return new String( _buffer , 0 , _size , encoding );
Expand Down

0 comments on commit 7aae216

Please sign in to comment.