Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/netty/netty
Browse files Browse the repository at this point in the history
* 'master' of git://github.com/netty/netty: (45 commits)
  Allow easier use of AIO transport via bootstrap. Related to [netty#725]
  Fix a compilation error
  Fix a failing test
  Fix a compilation error (sorry!)
  More robust localhost resolution
  Use 'x' over "x" wherever possible / String.equals("") -> isEmpty()
  Add 'static' modifier to the methods that don't need to be member methods
  Replace keySet() + unnecessary map lookup with entrySet()
  Make classes static wherever possible
  Fix unchecked warnings
  Use foreach loop wherever possible / Prefer String.contains() to indexOf() >= 0 / Prefer StringUtil.split() to String.split()
  DefaultHttpDataFactory.MINSIZE must be final
  Remove methods overridden but identical with the super implementation / Make constructors of abstract classes protected rather than non-sense public
  Remove unnecessary throws clauses for unchecked exceptions
  Remove unused imports
  Remove various unnecessary qualifiers
  Remove redundant field initialization
  Suppress false positives related with utility class inspections.
  Remove redundant throws clauses / Suppress inspections for some false positives
  [netty#719] Handle http requests without an absolute path the right way when encoding them, which is adding / to it
  ...
  • Loading branch information
Ankur Chauhan committed Nov 11, 2012
2 parents c6aa295 + fa805c4 commit cf87dec
Show file tree
Hide file tree
Showing 163 changed files with 8,230 additions and 576 deletions.
2 changes: 1 addition & 1 deletion all/pom.xml
Expand Up @@ -20,7 +20,7 @@
<parent>
<groupId>io.netty</groupId>
<artifactId>netty-parent</artifactId>
<version>4.0.0.Alpha7-SNAPSHOT</version>
<version>4.0.0.Alpha8-SNAPSHOT</version>
</parent>

<artifactId>netty</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion buffer/pom.xml
Expand Up @@ -20,7 +20,7 @@
<parent>
<groupId>io.netty</groupId>
<artifactId>netty-parent</artifactId>
<version>4.0.0.Alpha7-SNAPSHOT</version>
<version>4.0.0.Alpha8-SNAPSHOT</version>
</parent>

<artifactId>netty-buffer</artifactId>
Expand Down
Expand Up @@ -32,7 +32,7 @@ protected AbstractWrappedByteBuf(ByteOrder endianness, int maxCapacity) {

@Override
public WrappedByteBuf capacity(int newCapacity) {
throw new ReadOnlyBufferException();
throw new UnsupportedOperationException();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
Expand Up @@ -33,8 +33,8 @@ public final class ByteBufUtil {
static {
final char[] DIGITS = "0123456789abcdef".toCharArray();
for (int i = 0; i < 256; i ++) {
HEXDUMP_TABLE[(i << 1) + 0] = DIGITS[i >>> 4 & 0x0F];
HEXDUMP_TABLE[(i << 1) + 1] = DIGITS[i >>> 0 & 0x0F];
HEXDUMP_TABLE[ i << 1 ] = DIGITS[i >>> 4 & 0x0F];
HEXDUMP_TABLE[(i << 1) + 1] = DIGITS[i & 0x0F];
}
}

Expand Down
158 changes: 66 additions & 92 deletions buffer/src/main/java/io/netty/buffer/DefaultCompositeByteBuf.java
Expand Up @@ -61,7 +61,8 @@ public DefaultCompositeByteBuf(int maxNumComponents, ByteBuf... buffers) {

this.maxNumComponents = maxNumComponents;

addComponents(0, buffers);
addComponents0(0, buffers);
consolidateIfNeeded();
setIndex(0, capacity());
}

Expand All @@ -74,30 +75,41 @@ public DefaultCompositeByteBuf(int maxNumComponents, Iterable<ByteBuf> buffers)
}

this.maxNumComponents = maxNumComponents;
addComponents(0, buffers);
addComponents0(0, buffers);
consolidateIfNeeded();
setIndex(0, capacity());

}

@Override
public CompositeByteBuf addComponent(ByteBuf buffer) {
addComponent(components.size(), buffer);
addComponent0(components.size(), buffer);
consolidateIfNeeded();
return this;
}

@Override
public CompositeByteBuf addComponents(ByteBuf... buffers) {
addComponents(components.size(), buffers);
addComponents0(components.size(), buffers);
consolidateIfNeeded();
return this;
}

@Override
public CompositeByteBuf addComponents(Iterable<ByteBuf> buffers) {
addComponents(components.size(), buffers);
addComponents0(components.size(), buffers);
consolidateIfNeeded();
return this;
}

@Override
public CompositeByteBuf addComponent(int cIndex, ByteBuf buffer) {
addComponent0(cIndex, buffer);
consolidateIfNeeded();
return this;
}

private int addComponent0(int cIndex, ByteBuf buffer) {
checkComponentIndex(cIndex);

if (buffer == null) {
Expand All @@ -107,34 +119,12 @@ public CompositeByteBuf addComponent(int cIndex, ByteBuf buffer) {
if (buffer instanceof Iterable) {
@SuppressWarnings("unchecked")
Iterable<ByteBuf> composite = (Iterable<ByteBuf>) buffer;
addComponents(cIndex, composite);
return this;
return addComponents0(cIndex, composite);
}

int readableBytes = buffer.readableBytes();
if (readableBytes == 0) {
return this;
}

// Consolidate if the number of components will exceed the allowed maximum by the current
// operation.
final int numComponents = components.size();
if (numComponents >= maxNumComponents) {
final int capacity = components.get(numComponents - 1).endOffset + readableBytes;

ByteBuf consolidated = buffer.unsafe().newBuffer(capacity);
for (int i = 0; i < numComponents; i ++) {
ByteBuf b = components.get(i).buf;
consolidated.writeBytes(b);
b.unsafe().release();
}
consolidated.writeBytes(buffer, buffer.readerIndex(), readableBytes);

Component c = new Component(consolidated);
c.endOffset = c.length;
components.clear();
components.add(c);
return this;
return cIndex;
}

// No need to consolidate - just add a component to the list.
Expand All @@ -152,96 +142,59 @@ public CompositeByteBuf addComponent(int cIndex, ByteBuf buffer) {
components.add(cIndex, c);
updateComponentOffsets(cIndex);
}
return this;
return cIndex;
}

@Override
public CompositeByteBuf addComponents(int cIndex, ByteBuf... buffers) {
addComponents0(cIndex, buffers);
consolidateIfNeeded();
return this;
}

private int addComponents0(int cIndex, ByteBuf... buffers) {
checkComponentIndex(cIndex);

if (buffers == null) {
throw new NullPointerException("buffers");
}

ByteBuf lastBuf = null;
int cnt = 0;

int readableBytes = 0;
for (ByteBuf b: buffers) {
if (b == null) {
break;
}
lastBuf = b;
cnt ++;
readableBytes += b.readableBytes();
}

if (readableBytes == 0) {
return this;
}

// Consolidate if the number of components will exceed the maximum by this operation.
final int numComponents = components.size();
if (numComponents + cnt >= maxNumComponents) {
final ByteBuf consolidated;
if (numComponents != 0) {
final int capacity = components.get(numComponents - 1).endOffset + readableBytes;
consolidated = lastBuf.unsafe().newBuffer(capacity);
for (int i = 0; i < cIndex; i ++) {
ByteBuf b = components.get(i).buf;
consolidated.writeBytes(b);
b.unsafe().release();
}

for (ByteBuf b: buffers) {
if (b == null) {
break;
}
consolidated.writeBytes(b, b.readerIndex(), b.readableBytes());
}

for (int i = cIndex; i < numComponents; i ++) {
ByteBuf b = components.get(i).buf;
consolidated.writeBytes(b);
b.unsafe().release();
}
} else {
consolidated = lastBuf.unsafe().newBuffer(readableBytes);
for (ByteBuf b: buffers) {
if (b == null) {
break;
}
consolidated.writeBytes(b, b.readerIndex(), b.readableBytes());
}
}

Component c = new Component(consolidated);
c.endOffset = c.length;
components.clear();
components.add(c);
updateComponentOffsets(0);
return this;
return cIndex;
}

// No need for consolidation
for (ByteBuf b: buffers) {
if (b == null) {
break;
}

if (b.readable()) {
addComponent(cIndex ++, b);
cIndex = addComponent0(cIndex, b) + 1;
int size = components.size();
if (cIndex > size) {
// was consolidated, so adjust index. #707
cIndex = size;
}
}
}
return this;
return cIndex;
}

@Override
public CompositeByteBuf addComponents(int cIndex, Iterable<ByteBuf> buffers) {
addComponents0(cIndex, buffers);
consolidateIfNeeded();
return this;
}

private int addComponents0(int cIndex, Iterable<ByteBuf> buffers) {
if (buffers == null) {
throw new NullPointerException("buffers");
}
Expand All @@ -252,8 +205,7 @@ public CompositeByteBuf addComponents(int cIndex, Iterable<ByteBuf> buffers) {
for (int i = 0; i < array.length; i ++) {
array[i] = list.get(i).buf;
}
addComponents(cIndex, array);
return this;
return addComponents0(cIndex, array);
}

if (buffers instanceof List) {
Expand All @@ -262,8 +214,7 @@ public CompositeByteBuf addComponents(int cIndex, Iterable<ByteBuf> buffers) {
for (int i = 0; i < array.length; i ++) {
array[i] = list.get(i);
}
addComponents(cIndex, array);
return this;
return addComponents0(cIndex, array);
}

if (buffers instanceof Collection) {
Expand All @@ -273,16 +224,39 @@ public CompositeByteBuf addComponents(int cIndex, Iterable<ByteBuf> buffers) {
for (ByteBuf b: col) {
array[i ++] = b;
}
addComponents(cIndex, array);
return this;
return addComponents0(cIndex, array);
}

List<ByteBuf> list = new ArrayList<ByteBuf>();
for (ByteBuf b: buffers) {
list.add(b);
}
addComponents(cIndex, list.toArray(new ByteBuf[list.size()]));
return this;
return addComponents0(cIndex, list.toArray(new ByteBuf[list.size()]));
}


/**
* This should only be called as last operation from a method as this may adjust the underlying
* array of components and so affect the index etc.
*/
private void consolidateIfNeeded() {
// Consolidate if the number of components will exceed the allowed maximum by the current
// operation.
final int numComponents = components.size();
if (numComponents > maxNumComponents) {
final int capacity = components.get(numComponents - 1).endOffset;

ByteBuf consolidated = components.get(numComponents - 1).buf.unsafe().newBuffer(capacity);
for (int i = 0; i < numComponents; i ++) {
ByteBuf b = components.get(i).buf;
consolidated.writeBytes(b);
b.unsafe().release();
}
Component c = new Component(consolidated);
c.endOffset = c.length;
components.clear();
components.add(c);
}
}

private void checkComponentIndex(int cIndex) {
Expand Down Expand Up @@ -1243,7 +1217,7 @@ public CompositeByteBuf discardReadBytes() {
public String toString() {
String result = super.toString();
result = result.substring(0, result.length() - 1);
return result + ", components=" + components.size() + ")";
return result + ", components=" + components.size() + ')';
}

private static final class Component {
Expand Down
9 changes: 4 additions & 5 deletions buffer/src/main/java/io/netty/buffer/DirectByteBuf.java
Expand Up @@ -15,6 +15,8 @@
*/
package io.netty.buffer;

import sun.misc.Cleaner;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -25,8 +27,6 @@
import java.nio.channels.GatheringByteChannel;
import java.nio.channels.ScatteringByteChannel;

import sun.misc.Cleaner;

/**
* A NIO {@link ByteBuffer} based buffer. It is recommended to use {@link Unpooled#directBuffer(int)}
* and {@link Unpooled#wrappedBuffer(ByteBuffer)} instead of calling the
Expand Down Expand Up @@ -208,8 +208,7 @@ public short getShort(int index) {

@Override
public int getUnsignedMedium(int index) {
return (getByte(index) & 0xff) << 16 | (getByte(index + 1) & 0xff) << 8 |
(getByte(index + 2) & 0xff) << 0;
return (getByte(index) & 0xff) << 16 | (getByte(index + 1) & 0xff) << 8 | getByte(index + 2) & 0xff;
}

@Override
Expand Down Expand Up @@ -278,7 +277,7 @@ public ByteBuf setShort(int index, int value) {
public ByteBuf setMedium(int index, int value) {
setByte(index, (byte) (value >>> 16));
setByte(index + 1, (byte) (value >>> 8));
setByte(index + 2, (byte) (value >>> 0));
setByte(index + 2, (byte) value);
return this;
}

Expand Down
5 changes: 0 additions & 5 deletions buffer/src/main/java/io/netty/buffer/DuplicatedByteBuf.java
Expand Up @@ -108,11 +108,6 @@ public long getLong(int index) {
return buffer.getLong(index);
}

@Override
public ByteBuf duplicate() {
return new DuplicatedByteBuf(this);
}

@Override
public ByteBuf copy(int index, int length) {
return buffer.copy(index, length);
Expand Down

0 comments on commit cf87dec

Please sign in to comment.