00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include "../commun/buffer.h"
00036
00037 Buffer::Buffer( const Buffer& b) {
00038
00039 _size = b.getSize();
00040 _bufSize = _size+RESERVED;
00041 _buf = new char[_bufSize];
00042
00043 memcpy( _buf, b.getBuffer(), _size);
00044 }
00045
00046 Buffer::Buffer( const char* buf, jint size) {
00047
00048 _size = size;
00049 _bufSize = _size+RESERVED;
00050 _buf = new char[_bufSize];
00051
00052 memcpy( _buf, buf, _size);
00053 }
00054
00055 void Buffer::operator+=( const String& s) {
00056
00057 int len = s.length()+1;
00058
00059 reallocate( _size+len);
00060 memcpy( _buf+_size, s, len);
00061
00062 _size += len;
00063 }
00064
00065 void Buffer::operator+=( const jint c) {
00066
00067 int len = sizeof( c);
00068
00069 reallocate( _size+len);
00070
00071
00072 jint nc = htonl( c);
00073 memcpy( _buf+_size, &nc, len);
00074
00075 _size += len;
00076 }
00077
00078 void Buffer::operator+=( const jlong c) {
00079
00080 int len = sizeof( c);
00081
00082 reallocate( _size+len);
00083
00084
00085 jint hi = (jint)(c>>32);
00086 jint lo = (jint)(((jlong)hi<<32)^c);
00087
00088
00089 jint nhi = htonl( hi);
00090 jint nlo = htonl( lo);
00091
00092
00093 memcpy( _buf+_size, &nhi, sizeof( nhi));
00094 memcpy( _buf+_size+sizeof( nhi), &nlo, sizeof( nlo));
00095
00096 _size += len;
00097 }
00098
00099 void Buffer::operator=( const Buffer& b) {
00100
00101 reallocate( b.getSize());
00102
00103 _size = b.getSize();
00104 memcpy( _buf, b.getBuffer(), _size);
00105 }
00106
00107 void Buffer::reallocate( jint newBufSize) {
00108
00109 if( _bufSize >= newBufSize) return;
00110
00111 newBufSize += RESERVED;
00112 char* newBuf = new char[newBufSize];
00113
00114 if( _buf) {
00115
00116 memcpy( newBuf, _buf, _size);
00117 delete[] _buf;
00118 }
00119
00120 _buf = newBuf;
00121 _bufSize = newBufSize;
00122 }
00123
00124 void Buffer::clear( jint newBufSize) {
00125
00126 _size = 0;
00127 if( _bufSize >= newBufSize) return;
00128
00129 _bufSize = newBufSize+RESERVED;
00130
00131 if( _buf) delete[] _buf;
00132 _buf = new char[_bufSize];
00133 }