< prev index next >

src/hotspot/share/utilities/ostream.cpp

Print this page




 295       if (with_ascii) {
 296         print("  ");
 297         for (size_t j = 0; j < 16; ++j) {
 298           size_t idx = i + j - 15;
 299           if (idx < len) {
 300             char c = ((char*)data)[idx];
 301             print("%c", c >= 32 && c <= 126 ? c : '.');
 302           }
 303         }
 304       }
 305       cr();
 306     }
 307   }
 308 }
 309 
 310 stringStream::stringStream(size_t initial_size) : outputStream() {
 311   buffer_length = initial_size;
 312   buffer        = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
 313   buffer_pos    = 0;
 314   buffer_fixed  = false;
 315   zero_terminate();
 316 }
 317 
 318 // useful for output to fixed chunks of memory, such as performance counters
 319 stringStream::stringStream(char* fixed_buffer, size_t fixed_buffer_size) : outputStream() {
 320   buffer_length = fixed_buffer_size;
 321   buffer        = fixed_buffer;
 322   buffer_pos    = 0;
 323   buffer_fixed  = true;
 324   zero_terminate();
 325 }
 326 
 327 void stringStream::write(const char* s, size_t len) {
 328   size_t write_len = len;               // number of non-null bytes to write
 329   size_t end = buffer_pos + len + 1;    // position after write and final '\0'
 330   if (end > buffer_length) {
 331     if (buffer_fixed) {
 332       // if buffer cannot resize, silently truncate
 333       end = buffer_length;
 334       write_len = end - buffer_pos - 1; // leave room for the final '\0'
 335     } else {
 336       // For small overruns, double the buffer.  For larger ones,
 337       // increase to the requested size.
 338       if (end < buffer_length * 2) {
 339         end = buffer_length * 2;
 340       }
 341       buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end, mtInternal);
 342       buffer_length = end;
 343     }
 344   }
 345   // invariant: buffer is always null-terminated
 346   guarantee(buffer_pos + write_len + 1 <= buffer_length, "stringStream oob");
 347   if (write_len > 0) {

 348     memcpy(buffer + buffer_pos, s, write_len);
 349     buffer_pos += write_len;
 350     zero_terminate();
 351   }
 352 
 353   // Note that the following does not depend on write_len.
 354   // This means that position and count get updated
 355   // even when overflow occurs.
 356   update_position(s, len);
 357 }
 358 
 359 void stringStream::zero_terminate() {
 360   assert(buffer != NULL &&
 361          buffer_pos < buffer_length, "sanity");
 362   buffer[buffer_pos] = '\0';
 363 }
 364 
 365 void stringStream::reset() {
 366   buffer_pos = 0; _precount = 0; _position = 0;
 367   zero_terminate();
 368 }
 369 
 370 char* stringStream::as_string() const {
 371   char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos + 1);
 372   strncpy(copy, buffer, buffer_pos);
 373   copy[buffer_pos] = 0;  // terminating null
 374   return copy;
 375 }
 376 
 377 stringStream::~stringStream() {
 378   if (buffer_fixed == false && buffer != NULL) {
 379     FREE_C_HEAP_ARRAY(char, buffer);
 380   }
 381 }
 382 
 383 xmlStream*   xtty;
 384 outputStream* tty;
 385 CDS_ONLY(fileStream* classlist_file;) // Only dump the classes that can be stored into the CDS archive
 386 extern Mutex* tty_lock;
 387 
 388 #define EXTRACHARLEN   32
 389 #define CURRENTAPPX    ".current"
 390 // convert YYYY-MM-DD HH:MM:SS to YYYY-MM-DD_HH-MM-SS




 295       if (with_ascii) {
 296         print("  ");
 297         for (size_t j = 0; j < 16; ++j) {
 298           size_t idx = i + j - 15;
 299           if (idx < len) {
 300             char c = ((char*)data)[idx];
 301             print("%c", c >= 32 && c <= 126 ? c : '.');
 302           }
 303         }
 304       }
 305       cr();
 306     }
 307   }
 308 }
 309 
 310 stringStream::stringStream(size_t initial_size) : outputStream() {
 311   buffer_length = initial_size;
 312   buffer        = NEW_C_HEAP_ARRAY(char, buffer_length, mtInternal);
 313   buffer_pos    = 0;
 314   buffer_fixed  = false;

 315 }
 316 
 317 // useful for output to fixed chunks of memory, such as performance counters
 318 stringStream::stringStream(char* fixed_buffer, size_t fixed_buffer_size) : outputStream() {
 319   buffer_length = fixed_buffer_size;
 320   buffer        = fixed_buffer;
 321   buffer_pos    = 0;
 322   buffer_fixed  = true;

 323 }
 324 
 325 void stringStream::write(const char* s, size_t len) {
 326   size_t write_len = len;               // number of non-null bytes to write
 327   size_t end = buffer_pos + len + 1;    // position after write and final '\0'
 328   if (end > buffer_length) {
 329     if (buffer_fixed) {
 330       // if buffer cannot resize, silently truncate
 331       end = buffer_length;
 332       write_len = end - buffer_pos - 1; // leave room for the final '\0'
 333     } else {
 334       // For small overruns, double the buffer.  For larger ones,
 335       // increase to the requested size.
 336       if (end < buffer_length * 2) {
 337         end = buffer_length * 2;
 338       }
 339       buffer = REALLOC_C_HEAP_ARRAY(char, buffer, end, mtInternal);
 340       buffer_length = end;
 341     }
 342   }
 343   // invariant: buffer is always null-terminated
 344   guarantee(buffer_pos + write_len + 1 <= buffer_length, "stringStream oob");
 345   if (write_len > 0) {
 346     buffer[buffer_pos + write_len] = 0;
 347     memcpy(buffer + buffer_pos, s, write_len);
 348     buffer_pos += write_len;

 349   }
 350 
 351   // Note that the following does not depend on write_len.
 352   // This means that position and count get updated
 353   // even when overflow occurs.
 354   update_position(s, len);
 355 }
 356 
 357 char* stringStream::as_string() {











 358   char* copy = NEW_RESOURCE_ARRAY(char, buffer_pos + 1);
 359   strncpy(copy, buffer, buffer_pos);
 360   copy[buffer_pos] = 0;  // terminating null
 361   return copy;
 362 }
 363 
 364 stringStream::~stringStream() {
 365   if (buffer_fixed == false && buffer != NULL) {
 366     FREE_C_HEAP_ARRAY(char, buffer);
 367   }
 368 }
 369 
 370 xmlStream*   xtty;
 371 outputStream* tty;
 372 CDS_ONLY(fileStream* classlist_file;) // Only dump the classes that can be stored into the CDS archive
 373 extern Mutex* tty_lock;
 374 
 375 #define EXTRACHARLEN   32
 376 #define CURRENTAPPX    ".current"
 377 // convert YYYY-MM-DD HH:MM:SS to YYYY-MM-DD_HH-MM-SS


< prev index next >