question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Buffer.truncate(size)

See original GitHub issue

I found myself in a situation today where it was more efficient to write extra data (in the form of SegmentedByteStrings) to a buffer and truncate the buffer. Currently, I’m doing it this way:

long oldSize = sink.size();
long toWrite = Math.min(byteCount, SEGMENT_SIZE);
// ... maybe other writes
sink.write(segmentTemplate); // might overfill the buffer
sink.readAndWriteUnsafe(cursor);
try {
    cursor.resizeBuffer(oldSize + toWrite);
} finally {
    cursor.close();
}

This would also be alleviated if there was better handling of ByteStrings substrings. Either make creating substrings of a SegementedByteString share the underlying segments or add a BufferedSink.write method which allows for a begin and end index when writing a ByteString. Probably all good things regardless?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
swankjessecommented, Sep 26, 2018

As a general rule I prefer all Buffer changes to be either reading from the front or writing to the end. You can cheat to truncate like so:

Buffer needsToTruncate = ...
long truncatedLength = ...;
Buffer temp = new Buffer();
temp.write(needsToTruncate, truncatedLength);
needsToTruncate.clear();
needsToTruncate.writeAll(temp);

I wonder which of the two variants will perform better. One of ’em might share segments better than the other.

0reactions
bnormcommented, Oct 2, 2018

No worries, I sort of jumped topics pretty quick there. Closing as the original request is not going to be implemented.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Truncate buffer from front - Stack Overflow
Buffer object has a Truncate(n int) method to discard all but the first n bytes. I'd need the exact inverse of that -...
Read more >
TRUNC—Truncate buffer (QSAM output—fixed or ... - IBM
On input, the system uses the block size specified in DCBBLKSI for reading the PDSE. When a variable-length spanned record is truncated and...
Read more >
Truncating buffers could come in handy · Issue #6975 - GitHub
Buffer.truncate would be trivial to implement in O(1) with access to the ... if Buffer.length buf > max_len then Buffer.truncate buf max_len.
Read more >
How we can truncate a file at a given size using Python?
The truncate() function has no return value. To examine the file's increased size after appropriately applying truncate(), open the file's ...
Read more >
How do I automatically truncate a growing buffer?
By default, the buffer will be truncated at 1024 lines. To change it to 1000 you will need to change the variable comint-buffer-maximum-size...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found