Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members  

buffer.cpp

00001 /*
00002  *                         Sun Public License Notice
00003  *
00004  * The contents of this file are subject to the Sun Public License
00005  * Version 1.0 (the "License"); you may not use this file except
00006  * in compliance with the License. A copy of the License is available
00007  * at http://www.sun.com/
00008  *
00009  * The Original Code is the Java Profiler module.  The Initial Developers
00010  * of the Original Code are Jan Stola, Pavel Vacha, Michal Pise, Petr Luner,
00011  * Lukas Petru and Marek Przeczek.
00012  *
00013  * Portions created by Jan Stola are Copyright (C) 2000-2001.
00014  * All Rights Reserved.
00015  *
00016  * Portions created by Pavel Vacha are Copyright (C) 2000-2001.
00017  * All Rights Reserved.
00018  *
00019  * Portions created by Michal Pise are Copyright (C) 2000-2001.
00020  * All Rights Reserved.
00021  *
00022  * Portions created by Petr Luner are Copyright (C) 2000-2001.
00023  * All Rights Reserved.
00024  *
00025  * Portions created by Lukas Petru are Copyright (C) 2000-2001.
00026  * All Rights Reserved.
00027  *
00028  * Portions created by Marek Przeczek are Copyright (C) 2000-2001.
00029  * All Rights Reserved.
00030  *
00031  * Contributors: Jan Stola, Pavel Vacha, Michal Pise, Petr Luner,
00032  *               Lukas Petru and Marek Przeczek.
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         // store number in network-byte-order
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         // 64-bit number must be divided to two 32-bit numbers
00085         jint hi = (jint)(c>>32);
00086         jint lo = (jint)(((jlong)hi<<32)^c);
00087 
00088         // each of them is stored in network-byte-order
00089         jint nhi = htonl( hi);
00090         jint nlo = htonl( lo);
00091 
00092         // HI dword goes first, LO dword goes second
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 }

Generated on Mon Jan 28 14:53:26 2002 for Java Profiler Dynamic Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001