< prev index next >

src/hotspot/share/utilities/ostream.cpp

Print this page
rev 54032 : 8220394: bufferedStream does not honor size limit


 921   }
 922   if (defaultStream::instance != NULL) {
 923     delete defaultStream::instance;
 924   }
 925   tty = NULL;
 926   xtty = NULL;
 927   defaultStream::instance = NULL;
 928 }
 929 
 930 // ostream_abort() is called by os::abort() when VM is about to die.
 931 void ostream_abort() {
 932   // Here we can't delete tty, just flush its output
 933   if (tty) tty->flush();
 934 
 935   if (defaultStream::instance != NULL) {
 936     static char buf[4096];
 937     defaultStream::instance->finish_log_on_error(buf, sizeof(buf));
 938   }
 939 }
 940 
 941 bufferedStream::bufferedStream(size_t initial_size, size_t bufmax) : outputStream() {
 942   buffer_length = initial_size;
 943   buffer        = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
 944   buffer_pos    = 0;
 945   buffer_fixed  = false;
 946   buffer_max    = bufmax;






 947 }
 948 
 949 bufferedStream::bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t bufmax) : outputStream() {
 950   buffer_length = fixed_buffer_size;
 951   buffer        = fixed_buffer;
 952   buffer_pos    = 0;
 953   buffer_fixed  = true;
 954   buffer_max    = bufmax;



 955 }
 956 
 957 void bufferedStream::write(const char* s, size_t len) {
 958 
 959   if(buffer_pos + len > buffer_max) {
 960     flush();
 961   }
 962 
 963   size_t end = buffer_pos + len;
 964   if (end >= buffer_length) {
 965     if (buffer_fixed) {
 966       // if buffer cannot resize, silently truncate
 967       len = buffer_length - buffer_pos - 1;
 968     } else {
 969       // For small overruns, double the buffer.  For larger ones,
 970       // increase to the requested size.
 971       if (end < buffer_length * 2) {
 972         end = buffer_length * 2;
 973       }
 974       buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end, mtInternal);
 975       buffer_length = end;

 976     }


 977   }
 978   memcpy(buffer + buffer_pos, s, len);
 979   buffer_pos += len;




 980   update_position(s, len);


 981 }
 982 
 983 char* bufferedStream::as_string() {
 984   char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos+1);
 985   strncpy(copy, buffer, buffer_pos);
 986   copy[buffer_pos] = 0;  // terminating null
 987   return copy;
 988 }
 989 
 990 bufferedStream::~bufferedStream() {
 991   if (!buffer_fixed) {
 992     FREE_C_HEAP_ARRAY(char, buffer);
 993   }
 994 }
 995 
 996 #ifndef PRODUCT
 997 
 998 #if defined(SOLARIS) || defined(LINUX) || defined(AIX) || defined(_ALLBSD_SOURCE)
 999 #include <sys/types.h>
1000 #include <sys/socket.h>
1001 #include <netinet/in.h>
1002 #include <arpa/inet.h>
1003 #elif defined(_WINDOWS)
1004 #include <winsock2.h>
1005 #endif
1006 
1007 // Network access
1008 networkStream::networkStream() : bufferedStream(1024*10, 1024*10) {
1009 
1010   _socket = -1;
1011 
1012   int result = os::socket(AF_INET, SOCK_STREAM, 0);




 921   }
 922   if (defaultStream::instance != NULL) {
 923     delete defaultStream::instance;
 924   }
 925   tty = NULL;
 926   xtty = NULL;
 927   defaultStream::instance = NULL;
 928 }
 929 
 930 // ostream_abort() is called by os::abort() when VM is about to die.
 931 void ostream_abort() {
 932   // Here we can't delete tty, just flush its output
 933   if (tty) tty->flush();
 934 
 935   if (defaultStream::instance != NULL) {
 936     static char buf[4096];
 937     defaultStream::instance->finish_log_on_error(buf, sizeof(buf));
 938   }
 939 }
 940 
 941 bufferedStream::bufferedStream(size_t initial_capacity, size_t max_capacity, size_t flush_threshold)
 942   : outputStream(),
 943     _buffer(NULL),
 944     _pos(0),
 945     _capacity(0),
 946     _owned(true),
 947     _max_capacity(max_capacity),
 948     _flush_threshold(flush_threshold)
 949 {
 950   assert(initial_capacity > 0 && max_capacity >= initial_capacity, "Sanity");
 951   _capacity = initial_capacity;
 952   _buffer = NEW_C_HEAP_ARRAY(char, _capacity, mtInternal);
 953 }
 954 
 955 bufferedStream::bufferedStream(char* fixed_buffer, size_t fixed_buffer_size, size_t flush_threshold)
 956 : outputStream(),
 957   _buffer(fixed_buffer),
 958   _pos(0),
 959   _capacity(fixed_buffer_size),
 960   _owned(false),
 961   _max_capacity(fixed_buffer_size),
 962   _flush_threshold(flush_threshold)
 963 {
 964 }
 965 
 966 void bufferedStream::write(const char* s, size_t len) {
 967 
 968   if(_pos + len > _flush_threshold) {
 969     flush(); // Noop for bufferedStream. Flushes and resets for networkStream.
 970   }
 971 
 972   size_t end = _pos + len;
 973   if (end >= _capacity && _owned && _capacity < _max_capacity) {




 974     // For small overruns, double the buffer.  For larger ones,
 975     // increase to the requested size.
 976     if (end < _capacity * 2) {
 977       end = _capacity * 2;
 978     }
 979     // Do not grow beyond max.
 980     if (end > _max_capacity) {
 981       end = _max_capacity;
 982     }
 983     _buffer = REALLOC_C_HEAP_ARRAY(char, _buffer, end, mtInternal);
 984     _capacity = end;
 985   }
 986 
 987   const size_t remaining = _capacity - _pos - 1;
 988   len = MIN2(len, remaining);
 989   if (len > 0) {
 990     memcpy(_buffer + _pos, s, len);
 991     _pos += len;
 992     update_position(s, len);
 993   }
 994 
 995 }
 996 
 997 char* bufferedStream::as_string() {
 998   char* copy = NEW_RESOURCE_ARRAY(char, _pos+1);
 999   strncpy(copy, _buffer, _pos);
1000   copy[_pos] = 0;  // terminating null
1001   return copy;
1002 }
1003 
1004 bufferedStream::~bufferedStream() {
1005   if (_owned) {
1006     FREE_C_HEAP_ARRAY(char, _buffer);
1007   }
1008 }
1009 
1010 #ifndef PRODUCT
1011 
1012 #if defined(SOLARIS) || defined(LINUX) || defined(AIX) || defined(_ALLBSD_SOURCE)
1013 #include <sys/types.h>
1014 #include <sys/socket.h>
1015 #include <netinet/in.h>
1016 #include <arpa/inet.h>
1017 #elif defined(_WINDOWS)
1018 #include <winsock2.h>
1019 #endif
1020 
1021 // Network access
1022 networkStream::networkStream() : bufferedStream(1024*10, 1024*10) {
1023 
1024   _socket = -1;
1025 
1026   int result = os::socket(AF_INET, SOCK_STREAM, 0);


< prev index next >