Automated code clean-up

This commit is contained in:
Trustin Lee 2012-05-31 12:03:01 -07:00
parent 734d452be2
commit 77274ae743
59 changed files with 300 additions and 230 deletions

View File

@ -17,6 +17,7 @@ package io.netty.buffer;
import static io.netty.buffer.ChannelBuffers.*; import static io.netty.buffer.ChannelBuffers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import io.netty.util.CharsetUtil;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -26,7 +27,6 @@ import java.util.HashSet;
import java.util.Random; import java.util.Random;
import java.util.Set; import java.util.Set;
import io.netty.util.CharsetUtil;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;

View File

@ -16,8 +16,8 @@
package io.netty.buffer; package io.netty.buffer;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Test; import org.junit.Test;

View File

@ -15,8 +15,8 @@
*/ */
package io.netty.buffer; package io.netty.buffer;
import static org.easymock.EasyMock.*;
import static io.netty.buffer.ChannelBuffers.*; import static io.netty.buffer.ChannelBuffers.*;
import static org.easymock.EasyMock.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;

View File

@ -15,13 +15,13 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers;
/** /**
* The default {@link HttpChunkTrailer} implementation. * The default {@link HttpChunkTrailer} implementation.
*/ */

View File

@ -15,14 +15,14 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import java.util.List;
import java.util.Map;
import java.util.Set;
import io.netty.buffer.ChannelBuffer; import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers; import io.netty.buffer.ChannelBuffers;
import io.netty.util.internal.StringUtil; import io.netty.util.internal.StringUtil;
import java.util.List;
import java.util.Map;
import java.util.Set;
/** /**
* The default {@link HttpMessage} implementation. * The default {@link HttpMessage} implementation.
*/ */

View File

@ -15,15 +15,15 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers;
import io.netty.channel.ChannelPipeline;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers;
import io.netty.channel.ChannelPipeline;
/** /**
* An HTTP chunk which is used for HTTP chunked transfer-encoding. * An HTTP chunk which is used for HTTP chunked transfer-encoding.
* {@link HttpMessageDecoder} generates {@link HttpChunk} after * {@link HttpMessageDecoder} generates {@link HttpChunk} after

View File

@ -15,15 +15,15 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers;
import java.util.Calendar; import java.util.Calendar;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers;
/** /**
* An HTTP message which provides common properties for {@link HttpRequest} and * An HTTP message which provides common properties for {@link HttpRequest} and
* {@link HttpResponse}. * {@link HttpResponse}.

View File

@ -37,9 +37,9 @@ public class DefaultHttpDataFactory implements HttpDataFactory {
*/ */
public static long MINSIZE = 0x4000; public static long MINSIZE = 0x4000;
private boolean useDisk; private final boolean useDisk;
private boolean checkSize; private final boolean checkSize;
private long minSize; private long minSize;
@ -55,7 +55,7 @@ public class DefaultHttpDataFactory implements HttpDataFactory {
public DefaultHttpDataFactory() { public DefaultHttpDataFactory() {
useDisk = false; useDisk = false;
checkSize = true; checkSize = true;
this.minSize = MINSIZE; minSize = MINSIZE;
} }
/** /**

View File

@ -15,13 +15,13 @@
*/ */
package io.netty.handler.codec.http.multipart; package io.netty.handler.codec.http.multipart;
import io.netty.buffer.ChannelBuffer;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import io.netty.buffer.ChannelBuffer;
/** /**
* Extended interface for InterfaceHttpData * Extended interface for InterfaceHttpData
*/ */

View File

@ -113,7 +113,7 @@ public class CloseWebSocketFrame extends WebSocketFrame {
* a status code is set, -1 is returned. * a status code is set, -1 is returned.
*/ */
public int getStatusCode() { public int getStatusCode() {
ChannelBuffer binaryData = this.getBinaryData(); ChannelBuffer binaryData = getBinaryData();
if (binaryData == null || binaryData.capacity() == 0) { if (binaryData == null || binaryData.capacity() == 0) {
return -1; return -1;
} }
@ -130,7 +130,7 @@ public class CloseWebSocketFrame extends WebSocketFrame {
* text is not supplied, an empty string is returned. * text is not supplied, an empty string is returned.
*/ */
public String getReasonText() { public String getReasonText() {
ChannelBuffer binaryData = this.getBinaryData(); ChannelBuffer binaryData = getBinaryData();
if (binaryData == null || binaryData.capacity() <= 2) { if (binaryData == null || binaryData.capacity() <= 2) {
return ""; return "";
} }

View File

@ -144,7 +144,7 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker {
throw new WebSocketHandshakeException("Requested subprotocol(s) not supported: " + subprotocols); throw new WebSocketHandshakeException("Requested subprotocol(s) not supported: " + subprotocols);
} else { } else {
res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol); res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
this.setSelectedSubprotocol(selectedSubprotocol); setSelectedSubprotocol(selectedSubprotocol);
} }
} }

View File

@ -138,7 +138,7 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker {
throw new WebSocketHandshakeException("Requested subprotocol(s) not supported: " + subprotocols); throw new WebSocketHandshakeException("Requested subprotocol(s) not supported: " + subprotocols);
} else { } else {
res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol); res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
this.setSelectedSubprotocol(selectedSubprotocol); setSelectedSubprotocol(selectedSubprotocol);
} }
} }

View File

@ -139,7 +139,7 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker {
throw new WebSocketHandshakeException("Requested subprotocol(s) not supported: " + subprotocols); throw new WebSocketHandshakeException("Requested subprotocol(s) not supported: " + subprotocols);
} else { } else {
res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol); res.addHeader(Names.SEC_WEBSOCKET_PROTOCOL, selectedSubprotocol);
this.setSelectedSubprotocol(selectedSubprotocol); setSelectedSubprotocol(selectedSubprotocol);
} }
} }

View File

@ -15,14 +15,14 @@
*/ */
package io.netty.handler.codec.http.websocketx; package io.netty.handler.codec.http.websocketx;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import io.netty.buffer.ChannelBuffer; import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBuffers; import io.netty.buffer.ChannelBuffers;
import io.netty.handler.codec.base64.Base64; import io.netty.handler.codec.base64.Base64;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/** /**
* TODO Document me. * TODO Document me.
*/ */

View File

@ -15,11 +15,11 @@
*/ */
package io.netty.handler.codec.rtsp; package io.netty.handler.codec.rtsp;
import io.netty.handler.codec.http.HttpMethod;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import io.netty.handler.codec.http.HttpMethod;
/** /**
* The request method of RTSP. * The request method of RTSP.
* @apiviz.exclude * @apiviz.exclude

View File

@ -16,7 +16,6 @@
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.*; import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
import io.netty.buffer.ChannelBuffer; import io.netty.buffer.ChannelBuffer;
import io.netty.handler.codec.compression.CompressionException; import io.netty.handler.codec.compression.CompressionException;
import io.netty.util.internal.jzlib.JZlib; import io.netty.util.internal.jzlib.JZlib;

View File

@ -15,11 +15,10 @@
*/ */
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import java.util.zip.Deflater; import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
import io.netty.buffer.ChannelBuffer; import io.netty.buffer.ChannelBuffer;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.*; import java.util.zip.Deflater;
class SpdyHeaderBlockZlibCompressor extends SpdyHeaderBlockCompressor { class SpdyHeaderBlockZlibCompressor extends SpdyHeaderBlockCompressor {

View File

@ -15,12 +15,11 @@
*/ */
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import java.util.zip.DataFormatException; import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
import java.util.zip.Inflater;
import io.netty.buffer.ChannelBuffer; import io.netty.buffer.ChannelBuffer;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.*; import java.util.zip.DataFormatException;
import java.util.zip.Inflater;
class SpdyHeaderBlockZlibDecompressor extends SpdyHeaderBlockDecompressor { class SpdyHeaderBlockZlibDecompressor extends SpdyHeaderBlockDecompressor {

View File

@ -15,12 +15,7 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;

View File

@ -15,9 +15,7 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.*;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertNotNull;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.Date; import java.util.Date;

View File

@ -15,13 +15,14 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import io.netty.util.CharsetUtil;
import java.net.URI; import java.net.URI;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import io.netty.util.CharsetUtil;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;

View File

@ -15,10 +15,10 @@
*/ */
package io.netty.handler.codec.embedder; package io.netty.handler.codec.embedder;
import java.util.Collection;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
import java.util.Collection;
/** /**
* A helper that wraps an encoder or a decoder (codec) so that they can be used * A helper that wraps an encoder or a decoder (codec) so that they can be used
* without doing actual I/O in unit tests or higher level codecs. Please refer * without doing actual I/O in unit tests or higher level codecs. Please refer

View File

@ -15,9 +15,10 @@
*/ */
package io.netty.handler.codec.marshalling; package io.netty.handler.codec.marshalling;
import org.jboss.marshalling.Unmarshaller;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import org.jboss.marshalling.Unmarshaller;
/** /**
* This provider is responsible to get an {@link Unmarshaller} for a {@link ChannelHandlerContext} * This provider is responsible to get an {@link Unmarshaller} for a {@link ChannelHandlerContext}
* *

View File

@ -98,7 +98,7 @@ public class ObjectDecoderInputStream extends InputStream implements
} else { } else {
this.in = new DataInputStream(in); this.in = new DataInputStream(in);
} }
this.classResolver = ClassResolvers.weakCachingResolver(classLoader); classResolver = ClassResolvers.weakCachingResolver(classLoader);
this.maxObjectSize = maxObjectSize; this.maxObjectSize = maxObjectSize;
} }

View File

@ -15,16 +15,16 @@
*/ */
package io.netty.handler.codec.serialization; package io.netty.handler.codec.serialization;
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBufferOutputStream;
import io.netty.buffer.ChannelBuffers;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectOutput; import java.io.ObjectOutput;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
import java.io.OutputStream; import java.io.OutputStream;
import io.netty.buffer.ChannelBuffer;
import io.netty.buffer.ChannelBufferOutputStream;
import io.netty.buffer.ChannelBuffers;
/** /**
* An {@link ObjectOutput} which is interoperable with {@link ObjectDecoder} * An {@link ObjectOutput} which is interoperable with {@link ObjectDecoder}
* and {@link ObjectDecoderInputStream}. * and {@link ObjectDecoderInputStream}.

View File

@ -15,13 +15,13 @@
*/ */
package io.netty.handler.codec.protobuf; package io.netty.handler.codec.protobuf;
import static io.netty.buffer.ChannelBuffers.*;
import static org.hamcrest.core.Is.*; import static org.hamcrest.core.Is.*;
import static org.hamcrest.core.IsNull.*; import static org.hamcrest.core.IsNull.*;
import static io.netty.buffer.ChannelBuffers.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import io.netty.buffer.ChannelBuffer; import io.netty.buffer.ChannelBuffer;
import io.netty.handler.codec.embedder.DecoderEmbedder; import io.netty.handler.codec.embedder.DecoderEmbedder;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;

View File

@ -15,12 +15,12 @@
*/ */
package io.netty.handler.codec.protobuf; package io.netty.handler.codec.protobuf;
import static org.hamcrest.core.Is.*;
import static io.netty.buffer.ChannelBuffers.*; import static io.netty.buffer.ChannelBuffers.*;
import static org.hamcrest.core.Is.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import io.netty.buffer.ChannelBuffer; import io.netty.buffer.ChannelBuffer;
import io.netty.handler.codec.embedder.EncoderEmbedder; import io.netty.handler.codec.embedder.EncoderEmbedder;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;

View File

@ -29,6 +29,9 @@ public class UniqueName implements Comparable<UniqueName> {
this.name = name; this.name = name;
} }
/**
* @param args arguments to validate
*/
protected void validateArgs(Object... args) { protected void validateArgs(Object... args) {
// Subclasses will override. // Subclasses will override.
} }

View File

@ -142,7 +142,7 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
return segments[hash >>> segmentShift & segmentMask]; return segments[hash >>> segmentShift & segmentMask];
} }
private int hashOf(Object key) { private static int hashOf(Object key) {
return hash(System.identityHashCode(key)); return hash(System.identityHashCode(key));
} }
@ -315,7 +315,7 @@ public final class ConcurrentIdentityWeakKeyHashMap<K, V> extends AbstractMap<K,
return new Segment[i]; return new Segment[i];
} }
private boolean keyEq(Object src, Object dest) { private static boolean keyEq(Object src, Object dest) {
return src == dest; return src == dest;
} }

View File

@ -672,7 +672,7 @@ public class LegacyLinkedTransferQueue<E> extends AbstractQueue<E>
continue retry; // lost race vs opposite mode continue retry; // lost race vs opposite mode
} }
if (how != ASYNC) { if (how != ASYNC) {
return awaitMatch(s, pred, e, (how == TIMED), nanos); return awaitMatch(s, pred, e, how == TIMED, nanos);
} }
} }
return e; // not waiting return e; // not waiting
@ -1351,7 +1351,7 @@ public class LegacyLinkedTransferQueue<E> extends AbstractQueue<E>
throws java.io.IOException, ClassNotFoundException { throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject(); s.defaultReadObject();
for (;;) { for (;;) {
@SuppressWarnings("unchecked") E item = (E) s.readObject(); E item = (E) s.readObject();
if (item == null) { if (item == null) {
break; break;
} else { } else {

View File

@ -496,7 +496,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
*/ */
final boolean isMatched() { final boolean isMatched() {
Object x = item; Object x = item;
return (x == this) || ((x == null) == isData); return x == this || x == null == isData;
} }
/** /**
@ -514,7 +514,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
final boolean cannotPrecede(boolean haveData) { final boolean cannotPrecede(boolean haveData) {
boolean d = isData; boolean d = isData;
Object x; Object x;
return d != haveData && (x = item) != this && (x != null) == d; return d != haveData && (x = item) != this && x != null == d;
} }
/** /**
@ -598,8 +598,9 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* @throws NullPointerException if haveData mode but e is null * @throws NullPointerException if haveData mode but e is null
*/ */
private E xfer(E e, boolean haveData, int how, long nanos) { private E xfer(E e, boolean haveData, int how, long nanos) {
if (haveData && (e == null)) if (haveData && e == null) {
throw new NullPointerException(); throw new NullPointerException();
}
Node s = null; // the node to append, if needed Node s = null; // the node to append, if needed
retry: retry:
@ -608,9 +609,10 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
for (Node h = head, p = h; p != null;) { // find & match first node for (Node h = head, p = h; p != null;) { // find & match first node
boolean isData = p.isData; boolean isData = p.isData;
Object item = p.item; Object item = p.item;
if (item != p && (item != null) == isData) { // unmatched if (item != p && item != null == isData) { // unmatched
if (isData == haveData) // can't match if (isData == haveData) {
break; break;
}
if (p.casItem(item, e)) { // match if (p.casItem(item, e)) { // match
for (Node q = p; q != h;) { for (Node q = p; q != h;) {
Node n = q.next; // update by 2 unless singleton Node n = q.next; // update by 2 unless singleton
@ -620,24 +622,30 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
} // advance and retry } // advance and retry
if ((h = head) == null || if ((h = head) == null ||
(q = h.next) == null || !q.isMatched()) (q = h.next) == null || !q.isMatched())
{
break; // unless slack < 2 break; // unless slack < 2
} }
}
LockSupport.unpark(p.waiter); LockSupport.unpark(p.waiter);
return LinkedTransferQueue.<E>cast(item); return LinkedTransferQueue.<E>cast(item);
} }
} }
Node n = p.next; Node n = p.next;
p = (p != n) ? n : (h = head); // Use head if p offlist p = p != n ? n : (h = head); // Use head if p offlist
} }
if (how != NOW) { // No matches available if (how != NOW) { // No matches available
if (s == null) if (s == null) {
s = new Node(e, haveData); s = new Node(e, haveData);
}
Node pred = tryAppend(s, haveData); Node pred = tryAppend(s, haveData);
if (pred == null) if (pred == null)
{
continue retry; // lost race vs opposite mode continue retry; // lost race vs opposite mode
if (how != ASYNC) }
return awaitMatch(s, pred, e, (how == TIMED), nanos); if (how != ASYNC) {
return awaitMatch(s, pred, e, how == TIMED, nanos);
}
} }
return e; // not waiting return e; // not waiting
} }
@ -657,21 +665,25 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
Node n, u; // temps for reads of next & tail Node n, u; // temps for reads of next & tail
if (p == null && (p = head) == null) { if (p == null && (p = head) == null) {
if (casHead(null, s)) if (casHead(null, s))
{
return s; // initialize return s; // initialize
} }
else if (p.cannotPrecede(haveData)) }
else if (p.cannotPrecede(haveData)) {
return null; // lost race vs opposite mode return null; // lost race vs opposite mode
else if ((n = p.next) != null) // not last; keep traversing } else if ((n = p.next) != null) {
p = p != t && t != (u = tail) ? (t = u) : // stale tail p = p != t && t != (u = tail) ? (t = u) : // stale tail
(p != n) ? n : null; // restart if off list p != n ? n : null; // restart if off list
else if (!p.casNext(null, s)) } else if (!p.casNext(null, s)) {
p = p.next; // re-read on CAS failure p = p.next; // re-read on CAS failure
else { } else {
if (p != t) { // update if slack now >= 2 if (p != t) { // update if slack now >= 2
while ((tail != t || !casTail(t, s)) && while ((tail != t || !casTail(t, s)) &&
(t = tail) != null && (t = tail) != null &&
(s = t.next) != null && // advance and retry (s = t.next) != null && // advance and retry
(s = s.next) != null && s != t); (s = s.next) != null && s != t) {
continue;
}
} }
return p; return p;
} }
@ -703,28 +715,32 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
s.forgetContents(); // avoid garbage s.forgetContents(); // avoid garbage
return LinkedTransferQueue.<E>cast(item); return LinkedTransferQueue.<E>cast(item);
} }
if ((w.isInterrupted() || (timed && nanos <= 0)) && if ((w.isInterrupted() || timed && nanos <= 0) &&
s.casItem(e, s)) { // cancel s.casItem(e, s)) { // cancel
unsplice(pred, s); unsplice(pred, s);
return e; return e;
} }
if (spins < 0) { // establish spins at/near front if (spins < 0) { // establish spins at/near front
if ((spins = spinsFor(pred, s.isData)) > 0) if ((spins = spinsFor(pred, s.isData)) > 0) {
randomYields = ThreadLocalRandom.current(); randomYields = ThreadLocalRandom.current();
} }
}
else if (spins > 0) { // spin else if (spins > 0) { // spin
--spins; --spins;
if (randomYields.nextInt(CHAINED_SPINS) == 0) if (randomYields.nextInt(CHAINED_SPINS) == 0)
{
Thread.yield(); // occasionally yield Thread.yield(); // occasionally yield
} }
}
else if (s.waiter == null) { else if (s.waiter == null) {
s.waiter = w; // request unpark then recheck s.waiter = w; // request unpark then recheck
} }
else if (timed) { else if (timed) {
long now = System.nanoTime(); long now = System.nanoTime();
if ((nanos -= now - lastTime) > 0) if ((nanos -= now - lastTime) > 0) {
LockSupport.parkNanos(this, nanos); LockSupport.parkNanos(this, nanos);
}
lastTime = now; lastTime = now;
} }
else { else {
@ -739,13 +755,16 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
*/ */
private static int spinsFor(Node pred, boolean haveData) { private static int spinsFor(Node pred, boolean haveData) {
if (MP && pred != null) { if (MP && pred != null) {
if (pred.isData != haveData) // phase change if (pred.isData != haveData) {
return FRONT_SPINS + CHAINED_SPINS; return FRONT_SPINS + CHAINED_SPINS;
if (pred.isMatched()) // probably at front }
if (pred.isMatched()) {
return FRONT_SPINS; return FRONT_SPINS;
if (pred.waiter == null) // pred apparently spinning }
if (pred.waiter == null) {
return CHAINED_SPINS; return CHAINED_SPINS;
} }
}
return 0; return 0;
} }
@ -758,7 +777,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
*/ */
final Node succ(Node p) { final Node succ(Node p) {
Node next = p.next; Node next = p.next;
return (p == next) ? head : next; return p == next ? head : next;
} }
/** /**
@ -767,8 +786,9 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
*/ */
private Node firstOfMode(boolean isData) { private Node firstOfMode(boolean isData) {
for (Node p = head; p != null; p = succ(p)) { for (Node p = head; p != null; p = succ(p)) {
if (!p.isMatched()) if (!p.isMatched()) {
return (p.isData == isData) ? p : null; return p.isData == isData ? p : null;
}
} }
return null; return null;
} }
@ -781,12 +801,14 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
for (Node p = head; p != null; p = succ(p)) { for (Node p = head; p != null; p = succ(p)) {
Object item = p.item; Object item = p.item;
if (p.isData) { if (p.isData) {
if (item != null && item != p) if (item != null && item != p) {
return LinkedTransferQueue.<E>cast(item); return LinkedTransferQueue.<E>cast(item);
} }
else if (item == null) }
else if (item == null) {
return null; return null;
} }
}
return null; return null;
} }
@ -798,15 +820,17 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
int count = 0; int count = 0;
for (Node p = head; p != null; ) { for (Node p = head; p != null; ) {
if (!p.isMatched()) { if (!p.isMatched()) {
if (p.isData != data) if (p.isData != data) {
return 0; return 0;
if (++count == Integer.MAX_VALUE) // saturated }
if (++count == Integer.MAX_VALUE) {
break; break;
} }
}
Node n = p.next; Node n = p.next;
if (n != p) if (n != p) {
p = n; p = n;
else { } else {
count = 0; count = 0;
p = head; p = head;
} }
@ -835,25 +859,26 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
*/ */
Node r, b; // reset lastPred upon possible deletion of lastRet Node r, b; // reset lastPred upon possible deletion of lastRet
if ((r = lastRet) != null && !r.isMatched()) if ((r = lastRet) != null && !r.isMatched()) {
lastPred = r; // next lastPred is old lastRet lastPred = r; // next lastPred is old lastRet
else if ((b = lastPred) == null || b.isMatched()) } else if ((b = lastPred) == null || b.isMatched()) {
lastPred = null; // at start of list lastPred = null; // at start of list
else { } else {
Node s, n; // help with removal of lastPred.next Node s, n; // help with removal of lastPred.next
while ((s = b.next) != null && while ((s = b.next) != null &&
s != b && s.isMatched() && s != b && s.isMatched() &&
(n = s.next) != null && n != s) (n = s.next) != null && n != s) {
b.casNext(s, n); b.casNext(s, n);
} }
}
this.lastRet = prev; this.lastRet = prev;
for (Node p = prev, s, n;;) { for (Node p = prev, s, n;;) {
s = (p == null) ? head : p.next; s = p == null ? head : p.next;
if (s == null) if (s == null) {
break; break;
else if (s == p) { } else if (s == p) {
p = null; p = null;
continue; continue;
} }
@ -865,18 +890,20 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
return; return;
} }
} }
else if (item == null) else if (item == null) {
break; break;
}
// assert s.isMatched(); // assert s.isMatched();
if (p == null) if (p == null) {
p = s; p = s;
else if ((n = s.next) == null) } else if ((n = s.next) == null) {
break; break;
else if (s == n) } else if (s == n) {
p = null; p = null;
else } else {
p.casNext(s, n); p.casNext(s, n);
} }
}
nextNode = null; nextNode = null;
nextItem = null; nextItem = null;
} }
@ -885,27 +912,34 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
advance(null); advance(null);
} }
@Override
public final boolean hasNext() { public final boolean hasNext() {
return nextNode != null; return nextNode != null;
} }
@Override
public final E next() { public final E next() {
Node p = nextNode; Node p = nextNode;
if (p == null) throw new NoSuchElementException(); if (p == null) {
throw new NoSuchElementException();
}
E e = nextItem; E e = nextItem;
advance(p); advance(p);
return e; return e;
} }
@Override
public final void remove() { public final void remove() {
final Node lastRet = this.lastRet; final Node lastRet = this.lastRet;
if (lastRet == null) if (lastRet == null) {
throw new IllegalStateException(); throw new IllegalStateException();
}
this.lastRet = null; this.lastRet = null;
if (lastRet.tryMatchData()) if (lastRet.tryMatchData()) {
unsplice(lastPred, lastRet); unsplice(lastPred, lastRet);
} }
} }
}
/* -------------- Removal methods -------------- */ /* -------------- Removal methods -------------- */
@ -929,26 +963,34 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
if (pred != null && pred != s && pred.next == s) { if (pred != null && pred != s && pred.next == s) {
Node n = s.next; Node n = s.next;
if (n == null || if (n == null ||
(n != s && pred.casNext(s, n) && pred.isMatched())) { n != s && pred.casNext(s, n) && pred.isMatched()) {
for (;;) { // check if at, or could be, head for (;;) { // check if at, or could be, head
Node h = head; Node h = head;
if (h == pred || h == s || h == null) if (h == pred || h == s || h == null)
{
return; // at head or list empty return; // at head or list empty
if (!h.isMatched()) }
if (!h.isMatched()) {
break; break;
}
Node hn = h.next; Node hn = h.next;
if (hn == null) if (hn == null)
{
return; // now empty return; // now empty
}
if (hn != h && casHead(h, hn)) if (hn != h && casHead(h, hn))
{
h.forgetNext(); // advance head h.forgetNext(); // advance head
} }
}
if (pred.next != pred && s.next != s) { // recheck if offlist if (pred.next != pred && s.next != s) { // recheck if offlist
for (;;) { // sweep now if enough votes for (;;) { // sweep now if enough votes
int v = sweepVotes; int v = sweepVotes;
if (v < SWEEP_THRESHOLD) { if (v < SWEEP_THRESHOLD) {
if (casSweepVotes(v, v + 1)) if (casSweepVotes(v, v + 1)) {
break; break;
} }
}
else if (casSweepVotes(v, 0)) { else if (casSweepVotes(v, 0)) {
sweep(); sweep();
break; break;
@ -965,18 +1007,19 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
*/ */
private void sweep() { private void sweep() {
for (Node p = head, s, n; p != null && (s = p.next) != null; ) { for (Node p = head, s, n; p != null && (s = p.next) != null; ) {
if (!s.isMatched()) if (!s.isMatched()) {
// Unmatched nodes are never self-linked // Unmatched nodes are never self-linked
p = s; p = s;
else if ((n = s.next) == null) // trailing node is pinned } else if ((n = s.next) == null) {
break; break;
else if (s == n) // stale } else if (s == n) {
// No need to also check for p == s, since that implies s == n // No need to also check for p == s, since that implies s == n
p = head; p = head;
else } else {
p.casNext(s, n); p.casNext(s, n);
} }
} }
}
/** /**
* Main implementation of remove(Object) * Main implementation of remove(Object)
@ -992,8 +1035,9 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
return true; return true;
} }
} }
else if (item == null) else if (item == null) {
break; break;
}
pred = p; pred = p;
if ((p = p.next) == pred) { // stale if ((p = p.next) == pred) { // stale
pred = null; pred = null;
@ -1031,6 +1075,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* *
* @throws NullPointerException if the specified element is null * @throws NullPointerException if the specified element is null
*/ */
@Override
public void put(E e) { public void put(E e) {
xfer(e, true, ASYNC, 0); xfer(e, true, ASYNC, 0);
} }
@ -1045,6 +1090,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* BlockingQueue.offer}) * BlockingQueue.offer})
* @throws NullPointerException if the specified element is null * @throws NullPointerException if the specified element is null
*/ */
@Override
public boolean offer(E e, long timeout, TimeUnit unit) { public boolean offer(E e, long timeout, TimeUnit unit) {
xfer(e, true, ASYNC, 0); xfer(e, true, ASYNC, 0);
return true; return true;
@ -1057,6 +1103,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* @return {@code true} (as specified by {@link Queue#offer}) * @return {@code true} (as specified by {@link Queue#offer})
* @throws NullPointerException if the specified element is null * @throws NullPointerException if the specified element is null
*/ */
@Override
public boolean offer(E e) { public boolean offer(E e) {
xfer(e, true, ASYNC, 0); xfer(e, true, ASYNC, 0);
return true; return true;
@ -1070,6 +1117,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* @return {@code true} (as specified by {@link Collection#add}) * @return {@code true} (as specified by {@link Collection#add})
* @throws NullPointerException if the specified element is null * @throws NullPointerException if the specified element is null
*/ */
@Override
public boolean add(E e) { public boolean add(E e) {
xfer(e, true, ASYNC, 0); xfer(e, true, ASYNC, 0);
return true; return true;
@ -1123,28 +1171,35 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
*/ */
public boolean tryTransfer(E e, long timeout, TimeUnit unit) public boolean tryTransfer(E e, long timeout, TimeUnit unit)
throws InterruptedException { throws InterruptedException {
if (xfer(e, true, TIMED, unit.toNanos(timeout)) == null) if (xfer(e, true, TIMED, unit.toNanos(timeout)) == null) {
return true; return true;
if (!Thread.interrupted()) }
if (!Thread.interrupted()) {
return false; return false;
}
throw new InterruptedException(); throw new InterruptedException();
} }
@Override
public E take() throws InterruptedException { public E take() throws InterruptedException {
E e = xfer(null, false, SYNC, 0); E e = xfer(null, false, SYNC, 0);
if (e != null) if (e != null) {
return e; return e;
}
Thread.interrupted(); Thread.interrupted();
throw new InterruptedException(); throw new InterruptedException();
} }
@Override
public E poll(long timeout, TimeUnit unit) throws InterruptedException { public E poll(long timeout, TimeUnit unit) throws InterruptedException {
E e = xfer(null, false, TIMED, unit.toNanos(timeout)); E e = xfer(null, false, TIMED, unit.toNanos(timeout));
if (e != null || !Thread.interrupted()) if (e != null || !Thread.interrupted()) {
return e; return e;
}
throw new InterruptedException(); throw new InterruptedException();
} }
@Override
public E poll() { public E poll() {
return xfer(null, false, NOW, 0); return xfer(null, false, NOW, 0);
} }
@ -1153,11 +1208,14 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* @throws NullPointerException {@inheritDoc} * @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc}
*/ */
@Override
public int drainTo(Collection<? super E> c) { public int drainTo(Collection<? super E> c) {
if (c == null) if (c == null) {
throw new NullPointerException(); throw new NullPointerException();
if (c == this) }
if (c == this) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
}
int n = 0; int n = 0;
for (E e; (e = poll()) != null;) { for (E e; (e = poll()) != null;) {
c.add(e); c.add(e);
@ -1170,11 +1228,14 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* @throws NullPointerException {@inheritDoc} * @throws NullPointerException {@inheritDoc}
* @throws IllegalArgumentException {@inheritDoc} * @throws IllegalArgumentException {@inheritDoc}
*/ */
@Override
public int drainTo(Collection<? super E> c, int maxElements) { public int drainTo(Collection<? super E> c, int maxElements) {
if (c == null) if (c == null) {
throw new NullPointerException(); throw new NullPointerException();
if (c == this) }
if (c == this) {
throw new IllegalArgumentException(); throw new IllegalArgumentException();
}
int n = 0; int n = 0;
for (E e; n < maxElements && (e = poll()) != null;) { for (E e; n < maxElements && (e = poll()) != null;) {
c.add(e); c.add(e);
@ -1196,10 +1257,12 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* *
* @return an iterator over the elements in this queue in proper sequence * @return an iterator over the elements in this queue in proper sequence
*/ */
@Override
public Iterator<E> iterator() { public Iterator<E> iterator() {
return new Itr(); return new Itr();
} }
@Override
public E peek() { public E peek() {
return firstDataItem(); return firstDataItem();
} }
@ -1209,11 +1272,13 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* *
* @return {@code true} if this queue contains no elements * @return {@code true} if this queue contains no elements
*/ */
@Override
public boolean isEmpty() { public boolean isEmpty() {
for (Node p = head; p != null; p = succ(p)) { for (Node p = head; p != null; p = succ(p)) {
if (!p.isMatched()) if (!p.isMatched()) {
return !p.isData; return !p.isData;
} }
}
return true; return true;
} }
@ -1233,6 +1298,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* *
* @return the number of elements in this queue * @return the number of elements in this queue
*/ */
@Override
public int size() { public int size() {
return countOfMode(true); return countOfMode(true);
} }
@ -1252,6 +1318,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* @param o element to be removed from this queue, if present * @param o element to be removed from this queue, if present
* @return {@code true} if this queue changed as a result of the call * @return {@code true} if this queue changed as a result of the call
*/ */
@Override
public boolean remove(Object o) { public boolean remove(Object o) {
return findAndRemove(o); return findAndRemove(o);
} }
@ -1264,17 +1331,22 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* @param o object to be checked for containment in this queue * @param o object to be checked for containment in this queue
* @return {@code true} if this queue contains the specified element * @return {@code true} if this queue contains the specified element
*/ */
@Override
public boolean contains(Object o) { public boolean contains(Object o) {
if (o == null) return false; if (o == null) {
return false;
}
for (Node p = head; p != null; p = succ(p)) { for (Node p = head; p != null; p = succ(p)) {
Object item = p.item; Object item = p.item;
if (p.isData) { if (p.isData) {
if (item != null && item != p && o.equals(item)) if (item != null && item != p && o.equals(item)) {
return true; return true;
} }
else if (item == null) }
else if (item == null) {
break; break;
} }
}
return false; return false;
} }
@ -1286,6 +1358,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
* {@link java.util.concurrent.BlockingQueue#remainingCapacity() * {@link java.util.concurrent.BlockingQueue#remainingCapacity()
* BlockingQueue.remainingCapacity}) * BlockingQueue.remainingCapacity})
*/ */
@Override
public int remainingCapacity() { public int remainingCapacity() {
return Integer.MAX_VALUE; return Integer.MAX_VALUE;
} }
@ -1300,8 +1373,9 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
private void writeObject(java.io.ObjectOutputStream s) private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException { throws java.io.IOException {
s.defaultWriteObject(); s.defaultWriteObject();
for (E e : this) for (E e : this) {
s.writeObject(e); s.writeObject(e);
}
// Use trailing null as sentinel // Use trailing null as sentinel
s.writeObject(null); s.writeObject(null);
} }
@ -1316,14 +1390,14 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
throws java.io.IOException, ClassNotFoundException { throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject(); s.defaultReadObject();
for (;;) { for (;;) {
@SuppressWarnings("unchecked")
E item = (E) s.readObject(); E item = (E) s.readObject();
if (item == null) if (item == null) {
break; break;
else } else {
offer(item); offer(item);
} }
} }
}
// Unsafe mechanics // Unsafe mechanics
@ -1361,6 +1435,7 @@ public class LinkedTransferQueue<E> extends AbstractQueue<E>
return java.security.AccessController.doPrivileged return java.security.AccessController.doPrivileged
(new java.security (new java.security
.PrivilegedExceptionAction<sun.misc.Unsafe>() { .PrivilegedExceptionAction<sun.misc.Unsafe>() {
@Override
public sun.misc.Unsafe run() throws Exception { public sun.misc.Unsafe run() throws Exception {
java.lang.reflect.Field f = sun.misc java.lang.reflect.Field f = sun.misc
.Unsafe.class.getDeclaredField("theUnsafe"); .Unsafe.class.getDeclaredField("theUnsafe");

View File

@ -15,12 +15,12 @@
*/ */
package io.netty.util.internal; package io.netty.util.internal;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import io.netty.logging.InternalLogger; import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory; import io.netty.logging.InternalLoggerFactory;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
/** /**
* Warn when user creates too many instances to avoid {@link OutOfMemoryError}. * Warn when user creates too many instances to avoid {@link OutOfMemoryError}.
*/ */

View File

@ -456,7 +456,7 @@ final class InfCodes {
// at least ten. The ten bytes are six bytes for the longest length/ // at least ten. The ten bytes are six bytes for the longest length/
// distance pair plus four bytes for overloading the bit buffer. // distance pair plus four bytes for overloading the bit buffer.
int inflate_fast(int bl, int bd, int[] tl, int tl_index, int[] td, static int inflate_fast(int bl, int bd, int[] tl, int tl_index, int[] td,
int td_index, InfBlocks s, ZStream z) { int td_index, InfBlocks s, ZStream z) {
int t; // temporary pointer int t; // temporary pointer
int[] tp; // temporary pointer int[] tp; // temporary pointer

View File

@ -562,7 +562,7 @@ final class Inflate {
} }
} }
int inflateSetDictionary(ZStream z, byte[] dictionary, int dictLength) { static int inflateSetDictionary(ZStream z, byte[] dictionary, int dictLength) {
int index = 0; int index = 0;
int length = dictLength; int length = dictLength;
if (z == null || z.istate == null || z.istate.mode != DICT0) { if (z == null || z.istate == null || z.istate.mode != DICT0) {

View File

@ -114,7 +114,7 @@ public final class ZStream {
if (istate == null) { if (istate == null) {
return JZlib.Z_STREAM_ERROR; return JZlib.Z_STREAM_ERROR;
} }
return istate.inflateSetDictionary(this, dictionary, dictLength); return Inflate.inflateSetDictionary(this, dictionary, dictLength);
} }
public int deflateInit(int level) { public int deflateInit(int level) {

View File

@ -66,7 +66,7 @@ public class StringUtilTest {
} }
} }
private void assertStripped(final char controlCode) { private static void assertStripped(final char controlCode) {
final Object object = "aaa" + controlCode + "bbb"; final Object object = "aaa" + controlCode + "bbb";
final String stripped = StringUtil.stripControlCharacters(object); final String stripped = StringUtil.stripControlCharacters(object);
assertEquals("aaa bbb", stripped); assertEquals("aaa bbb", stripped);