Add IllegalBufferAccessException and checks on MessageBuf impls

- Related: #827
This commit is contained in:
Trustin Lee 2012-12-17 18:03:31 +09:00
parent def12a171c
commit ca93b624ff
3 changed files with 146 additions and 7 deletions

View File

@ -17,6 +17,7 @@ package io.netty.buffer;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.Collection; import java.util.Collection;
import java.util.Iterator;
final class DefaultMessageBuf<T> extends ArrayDeque<T> implements MessageBuf<T> { final class DefaultMessageBuf<T> extends ArrayDeque<T> implements MessageBuf<T> {
@ -35,8 +36,111 @@ final class DefaultMessageBuf<T> extends ArrayDeque<T> implements MessageBuf<T>
return BufType.MESSAGE; return BufType.MESSAGE;
} }
@Override
public void addFirst(T t) {
ensureValid();
super.addFirst(t);
}
@Override
public void addLast(T t) {
ensureValid();
super.addLast(t);
}
@Override
public T pollFirst() {
ensureValid();
return super.pollFirst();
}
@Override
public T pollLast() {
ensureValid();
return super.pollLast();
}
@Override
public T getFirst() {
ensureValid();
return super.getFirst();
}
@Override
public T getLast() {
ensureValid();
return super.getLast();
}
@Override
public T peekFirst() {
ensureValid();
return super.peekFirst();
}
@Override
public T peekLast() {
ensureValid();
return super.peekLast();
}
@Override
public boolean removeFirstOccurrence(Object o) {
ensureValid();
return super.removeFirstOccurrence(o);
}
@Override
public boolean removeLastOccurrence(Object o) {
ensureValid();
return super.removeLastOccurrence(o);
}
@Override
public Iterator<T> iterator() {
ensureValid();
return super.iterator();
}
@Override
public Iterator<T> descendingIterator() {
ensureValid();
return super.descendingIterator();
}
@Override
public boolean contains(Object o) {
ensureValid();
return super.contains(o);
}
@Override
public void clear() {
ensureValid();
super.clear();
}
@Override
public Object[] toArray() {
ensureValid();
return super.toArray();
}
@Override
public <T1 extends Object> T1[] toArray(T1[] a) {
ensureValid();
return super.toArray(a);
}
@Override
public ArrayDeque<T> clone() {
ensureValid();
return super.clone();
}
@Override @Override
public int drainTo(Collection<? super T> c) { public int drainTo(Collection<? super T> c) {
ensureValid();
int cnt = 0; int cnt = 0;
for (;;) { for (;;) {
T o = poll(); T o = poll();
@ -51,6 +155,7 @@ final class DefaultMessageBuf<T> extends ArrayDeque<T> implements MessageBuf<T>
@Override @Override
public int drainTo(Collection<? super T> c, int maxElements) { public int drainTo(Collection<? super T> c, int maxElements) {
ensureValid();
int cnt = 0; int cnt = 0;
while (cnt < maxElements) { while (cnt < maxElements) {
T o = poll(); T o = poll();
@ -71,5 +176,12 @@ final class DefaultMessageBuf<T> extends ArrayDeque<T> implements MessageBuf<T>
@Override @Override
public void free() { public void free() {
freed = true; freed = true;
super.clear();
}
private void ensureValid() {
if (freed) {
throw new IllegalBufferAccessException();
}
} }
} }

View File

@ -17,24 +17,24 @@
package io.netty.buffer; package io.netty.buffer;
/** /**
* A {@link RuntimeException} raised by a {@link ByteBuf} where a user requested an operation on a freed * An {@link IllegalStateException} raised when a user attempts to access a {@link Buf} which was freed by
* {@link ByteBuf}. * {@link Buf#free()} already.
*/ */
public class IllegalMemoryAccessException extends RuntimeException { public class IllegalBufferAccessException extends IllegalStateException {
private static final long serialVersionUID = -6734326916218551327L; private static final long serialVersionUID = -6734326916218551327L;
public IllegalMemoryAccessException() { } public IllegalBufferAccessException() { }
public IllegalMemoryAccessException(String message) { public IllegalBufferAccessException(String message) {
super(message); super(message);
} }
public IllegalMemoryAccessException(String message, Throwable cause) { public IllegalBufferAccessException(String message, Throwable cause) {
super(message, cause); super(message, cause);
} }
public IllegalMemoryAccessException(Throwable cause) { public IllegalBufferAccessException(Throwable cause) {
super(cause); super(cause);
} }
} }

View File

@ -38,96 +38,115 @@ final class QueueBackedMessageBuf<T> implements MessageBuf<T> {
@Override @Override
public boolean add(T e) { public boolean add(T e) {
ensureValid();
return queue.add(e); return queue.add(e);
} }
@Override @Override
public boolean offer(T e) { public boolean offer(T e) {
ensureValid();
return queue.offer(e); return queue.offer(e);
} }
@Override @Override
public T remove() { public T remove() {
ensureValid();
return queue.remove(); return queue.remove();
} }
@Override @Override
public T poll() { public T poll() {
ensureValid();
return queue.poll(); return queue.poll();
} }
@Override @Override
public T element() { public T element() {
ensureValid();
return queue.element(); return queue.element();
} }
@Override @Override
public T peek() { public T peek() {
ensureValid();
return queue.peek(); return queue.peek();
} }
@Override @Override
public int size() { public int size() {
ensureValid();
return queue.size(); return queue.size();
} }
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
ensureValid();
return queue.isEmpty(); return queue.isEmpty();
} }
@Override @Override
public boolean contains(Object o) { public boolean contains(Object o) {
ensureValid();
return queue.contains(o); return queue.contains(o);
} }
@Override @Override
public Iterator<T> iterator() { public Iterator<T> iterator() {
ensureValid();
return queue.iterator(); return queue.iterator();
} }
@Override @Override
public Object[] toArray() { public Object[] toArray() {
ensureValid();
return queue.toArray(); return queue.toArray();
} }
@Override @Override
public <E> E[] toArray(E[] a) { public <E> E[] toArray(E[] a) {
ensureValid();
return queue.toArray(a); return queue.toArray(a);
} }
@Override @Override
public boolean remove(Object o) { public boolean remove(Object o) {
ensureValid();
return queue.remove(o); return queue.remove(o);
} }
@Override @Override
public boolean containsAll(Collection<?> c) { public boolean containsAll(Collection<?> c) {
ensureValid();
return queue.containsAll(c); return queue.containsAll(c);
} }
@Override @Override
public boolean addAll(Collection<? extends T> c) { public boolean addAll(Collection<? extends T> c) {
ensureValid();
return queue.addAll(c); return queue.addAll(c);
} }
@Override @Override
public boolean removeAll(Collection<?> c) { public boolean removeAll(Collection<?> c) {
ensureValid();
return queue.removeAll(c); return queue.removeAll(c);
} }
@Override @Override
public boolean retainAll(Collection<?> c) { public boolean retainAll(Collection<?> c) {
ensureValid();
return queue.retainAll(c); return queue.retainAll(c);
} }
@Override @Override
public void clear() { public void clear() {
ensureValid();
queue.clear(); queue.clear();
} }
@Override @Override
public int drainTo(Collection<? super T> c) { public int drainTo(Collection<? super T> c) {
ensureValid();
int cnt = 0; int cnt = 0;
for (;;) { for (;;) {
T o = poll(); T o = poll();
@ -142,6 +161,7 @@ final class QueueBackedMessageBuf<T> implements MessageBuf<T> {
@Override @Override
public int drainTo(Collection<? super T> c, int maxElements) { public int drainTo(Collection<? super T> c, int maxElements) {
ensureValid();
int cnt = 0; int cnt = 0;
while (cnt < maxElements) { while (cnt < maxElements) {
T o = poll(); T o = poll();
@ -162,10 +182,17 @@ final class QueueBackedMessageBuf<T> implements MessageBuf<T> {
@Override @Override
public void free() { public void free() {
freed = true; freed = true;
queue.clear();
} }
@Override @Override
public String toString() { public String toString() {
return queue.toString(); return queue.toString();
} }
private void ensureValid() {
if (freed) {
throw new IllegalBufferAccessException();
}
}
} }