Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3 update branch 3 from very same issues and improvements fixed for #290 #291 #292 #299

Merged
merged 7 commits into from May 2, 2012
Expand Up @@ -115,9 +115,9 @@ public void setContent(ChannelBuffer buffer) throws IOException {
int written = 0;
while (written < size) {
written += localfileChannel.write(byteBuffer);
localfileChannel.force(false);
}
buffer.readerIndex(buffer.readerIndex() + written);
localfileChannel.force(false);
localfileChannel.close();
completed = true;
}
Expand All @@ -141,7 +141,6 @@ public void addContent(ChannelBuffer buffer, boolean last)
}
while (written < localsize) {
written += fileChannel.write(byteBuffer);
fileChannel.force(false);
}
size += localsize;
buffer.readerIndex(buffer.readerIndex() + written);
Expand All @@ -154,6 +153,7 @@ public void addContent(ChannelBuffer buffer, boolean last)
FileOutputStream outputStream = new FileOutputStream(file);
fileChannel = outputStream.getChannel();
}
fileChannel.force(false);
fileChannel.close();
fileChannel = null;
completed = true;
Expand Down Expand Up @@ -191,9 +191,10 @@ public void setContent(InputStream inputStream) throws IOException {
while (read > 0) {
byteBuffer.position(read).flip();
written += localfileChannel.write(byteBuffer);
localfileChannel.force(false);
read = inputStream.read(bytes);
}
localfileChannel.force(false);
localfileChannel.close());
size = written;
if (definedSize > 0 && definedSize < size) {
file.delete();
Expand Down Expand Up @@ -288,6 +289,8 @@ public boolean renameTo(File dest) throws IOException {
FileChannel in = inputStream.getChannel();
FileChannel out = outputStream.getChannel();
long destsize = in.transferTo(0, size, out);
in.close();
out.close();
if (destsize == size) {
file.delete();
file = dest;
Expand Down
Expand Up @@ -200,8 +200,8 @@ public boolean renameTo(File dest) throws IOException {
int written = 0;
while (written < length) {
written += fileChannel.write(byteBuffer);
fileChannel.force(false);
}
fileChannel.force(false);
fileChannel.close();
isRenamed = true;
return written == length;
Expand Down
Expand Up @@ -116,18 +116,59 @@ public String toString() {
private HttpPostBodyUtil() {
}

//Some commons methods between HttpPostRequestDecoder and HttpMessageDecoder

/**
* Skip control Characters
* @param buffer
*/
static void skipControlCharacters(ChannelBuffer buffer) {
for (;;) {
char c = (char) buffer.readUnsignedByte();
if (!Character.isISOControl(c) && !Character.isWhitespace(c)) {
buffer.readerIndex(buffer.readerIndex() - 1);
break;
* Exception when NO Backend Array is found
*/
static class SeekAheadNoBackArrayException extends Exception {
private static final long serialVersionUID = -630418804938699495L;
}

/**
* This class intends to decrease the CPU in seeking ahead some bytes in
* HttpPostRequestDecoder
*/
static class SeekAheadOptimize {
byte[] bytes;

int readerIndex;

int pos;

int limit;

ChannelBuffer buffer;

/**
* @param buffer
*/
SeekAheadOptimize(ChannelBuffer buffer) throws SeekAheadNoBackArrayException {
if (!buffer.hasArray()) {
throw new SeekAheadNoBackArrayException();
}
this.buffer = buffer;
this.bytes = buffer.array();
this.pos = this.readerIndex = buffer.readerIndex();
this.limit = buffer.writerIndex();
}

/**
*
* @param minus this value will be used as (currentPos - minus) to set
* the current readerIndex in the buffer.
*/
void setReadPosition(int minus) {
pos -= minus;
readerIndex = pos;
buffer.readerIndex(readerIndex);
}

void clear() {
this.buffer = null;
this.bytes = null;
this.limit = 0;
this.pos = 0;
this.readerIndex = 0;
}
}

Expand Down