Skip to content

Commit

Permalink
[JENKINS-36871] Remove intermediary ByteBuffer.wrap for simple case
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenc committed Aug 3, 2016
1 parent 38cd6e4 commit bbb5865
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 47 deletions.
46 changes: 46 additions & 0 deletions src/main/java/org/jenkinsci/remoting/util/ByteBufferQueue.java
Expand Up @@ -348,6 +348,52 @@ public void get(ByteBuffer dst) {
}
}

/**
* This method transfers bytes from the head of this buffer queue into the given destination {@link byte[]}.
* The number of bytes transferred will be the smaller of the number of requested bytes and the remaining capacity
* of the destination buffer.
* @param dst the destination byte array into which the bytes are to be written.
* @param offset the offset in the byte array at which to write the bytes.
* @param len the number of bytes to transfer.
* @return the actual number of bytes transferred.
*/
public int get(byte[] dst, int offset, int len) {
int read = 0;
while (len > 0) {
if (readIndex >= writeIndex && buffers[readIndex].position() == 0) {
if (writeIndex > 0) {
// this is a cheap compact
buffers[0] = buffers[writeIndex];
buffers[writeIndex] = null;
readIndex = writeIndex = 0;
}
break;
}
buffers[readIndex].flip();
int count = buffers[readIndex].remaining();
if (count > len) {
buffers[readIndex].get(dst, offset, len);
offset += len;
read += len;
len = 0;
buffers[readIndex].compact();
break;
} else {
buffers[readIndex].get(dst, offset, count);
offset += count;
read += count;
len -= count;
if (readIndex < writeIndex) {
buffers[readIndex++] = null;
} else {
assert readIndex == writeIndex;
buffers[readIndex].clear();
}
}
}
return read;
}

/**
* Reads the next byte from this queue.
*
Expand Down
Expand Up @@ -94,45 +94,6 @@ public int read() throws IOException {
}
}

/**
* {@inheritDoc}
*/
@Override
public int read(byte[] b) throws IOException {
ByteBuffer buffer;
if (length != -1 && pos >= length) {
int rem = length - pos;
if (rem <= 0) {
return -1;
} else if (b.length > rem) {
buffer = ByteBuffer.wrap(b, 0, rem);
} else {
buffer = ByteBuffer.wrap(b);
}
} else {
buffer = ByteBuffer.wrap(b);
}
queue.get(buffer);
int read = buffer.position();
if (read <= 0) {
return -1;
}
pos += read;
if (mark != null) {
if (mark.remaining() > read) {
int oldLimit = buffer.limit();
buffer.limit(buffer.position());
buffer.position(0);
mark.put(buffer);
buffer.limit(oldLimit);
} else {
// mark was invalidated as there was more data than reserved
mark = null;
}
}
return read;
}

/**
* {@inheritDoc}
*/
Expand All @@ -146,20 +107,14 @@ public int read(byte[] b, int off, int len) throws IOException {
len = rem;
}
}
ByteBuffer buffer = ByteBuffer.wrap(b, off, len);
queue.get(buffer);
int read = buffer.position() - off;
int read = queue.get(b, off, len);
if (read <= 0) {
return -1;
}
pos += read;
if (mark != null) {
if (mark.remaining() > read) {
int oldLimit = buffer.limit();
buffer.limit(buffer.position());
buffer.position(off);
mark.put(buffer);
buffer.limit(oldLimit);
mark.put(b, off, read);
} else {
// mark was invalidated as there was more data than reserved
mark = null;
Expand Down

0 comments on commit bbb5865

Please sign in to comment.