Skip to content

Commit

Permalink
Fix for large websocket frames and better reduce memory #14
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirralev committed Oct 6, 2016
1 parent 7337cbc commit d435649
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/gov/nist/javax/sip/stack/WebSocketCodec.java
Expand Up @@ -94,29 +94,28 @@ private byte readNextByte() {
public byte[] decode(InputStream is)
throws Exception {
do {
// Attempt to resize the buffer when it's approaching a big chunk size
// Attempt to resize the decode buffer when it's approaching capacity
int bytesLeft = decodeBuffer.length - writeIndex;
int availToRead = is.available();
if(availToRead > bytesLeft - 1) {
int newSize = Math.max(2*decodeBuffer.length, 4*availToRead);
if(logger.isLoggingEnabled(LogLevels.TRACE_DEBUG)) {
logger.logDebug("Doubling buffer size from " + decodeBuffer.length + " avail " + availToRead + " newSize " + newSize);
logger.logDebug("Increasing buffer size from " + decodeBuffer.length +
" avail " + availToRead + " newSize " + newSize);
}
byte[] resizeBuffer = new byte[newSize];
for(int q=0; q<writeIndex; q++) resizeBuffer[q] = this.decodeBuffer[q];
this.decodeBuffer = resizeBuffer;
}

int bytesRead = is.read(decodeBuffer, writeIndex, bytesLeft);


if(bytesRead < 0) bytesRead = 0;

// Update the count in the buffer
writeIndex += bytesRead;
} while(is.available()>0);

// Start over from scratch. This is rare and doesn't affect performance
// Start over from scratch. If the frame is big this may be repeated a few times O(logN) and doesn't affect performance
readIndex = 0;

// All TCP slow-start algorithms will be cut off right here without further analysis
Expand Down

0 comments on commit d435649

Please sign in to comment.