下面列出了怎么用java.nio.InvalidMarkException的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testSettingPositionBackwardsResetsMark()
{
_parent.position(8);
_parent.mark();
_parent.position(7);
try
{
_parent.reset();
fail("Expected exception not thrown");
}
catch (InvalidMarkException e)
{
// pass
}
}
@Test
public void testRewind() throws Exception
{
final int expectedLimit = 7;
_parent.position(1);
_parent.limit(expectedLimit);
_parent.mark();
_parent.position(3);
_parent.rewind();
assertEquals("Unexpected position", (long) 0, (long) _parent.position());
assertEquals("Unexpected limit", (long) expectedLimit, (long) _parent.limit());
try
{
_parent.reset();
fail("Expected exception not thrown");
}
catch (InvalidMarkException e)
{
// pass
}
}
@Test
public void testRewind() {
CompositeReadableBuffer buffer = new CompositeReadableBuffer();
buffer.append(new byte[] {0, 1, 2, 3, 4}).append(new byte[] {5, 6, 7, 8, 9});
// save state
int oldPosition = buffer.position();
int oldLimit = buffer.limit();
assertEquals(0, oldPosition);
assertEquals(10, oldLimit);
CompositeReadableBuffer self = buffer.rewind();
assertEquals(buffer.position(), 0);
assertSame(self, buffer);
try {
buffer.reset();
fail("Should throw InvalidMarkException");
} catch (InvalidMarkException e) {
}
}
@Test
public void testFlipWithOneArray() {
CompositeReadableBuffer buffer = new CompositeReadableBuffer();
buffer.append(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
// save state
int oldPosition = buffer.position();
int oldLimit = buffer.limit();
assertEquals(0, oldPosition);
assertEquals(10, oldLimit);
CompositeReadableBuffer ret = buffer.flip();
assertSame(ret, buffer);
assertEquals(buffer.position(), 0);
assertEquals(buffer.limit(), oldPosition);
try {
buffer.reset();
fail("Should throw InvalidMarkException");
} catch (InvalidMarkException e) {
}
}
@Test
public void testFlipWithMultipleArrays() {
CompositeReadableBuffer buffer = new CompositeReadableBuffer();
buffer.append(new byte[] {0, 1, 2, 3, 4}).append(new byte[] {5, 6, 7, 8, 9});
// save state
int oldPosition = buffer.position();
int oldLimit = buffer.limit();
assertEquals(0, oldPosition);
assertEquals(10, oldLimit);
CompositeReadableBuffer ret = buffer.flip();
assertSame(ret, buffer);
assertEquals(buffer.position(), 0);
assertEquals(buffer.limit(), oldPosition);
try {
buffer.reset();
fail("Should throw InvalidMarkException");
} catch (InvalidMarkException e) {
}
}
@Test
public void testClear() {
CompositeReadableBuffer buffer = new CompositeReadableBuffer();
buffer.append(new byte[] {0, 1, 2, 3, 4}).append(new byte[] {5, 6, 7, 8, 9});
assertEquals(0, buffer.position());
assertEquals(10, buffer.limit());
buffer.position(5);
assertEquals(5, buffer.position());
buffer.mark();
CompositeReadableBuffer self = buffer.clear();
assertEquals(0, buffer.position());
assertEquals(10, buffer.limit());
assertSame(self, buffer);
try {
buffer.reset();
fail("Should throw InvalidMarkException");
} catch (InvalidMarkException e) {
}
}
@Test
public void testSlice() {
CompositeReadableBuffer buffer = new CompositeReadableBuffer();
byte[] data = new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
buffer.append(data);
assertEquals(buffer.capacity(), data.length);
buffer.position(1);
buffer.limit(buffer.capacity() - 2);
ReadableBuffer slice = buffer.slice();
assertEquals(slice.position(), 0);
assertEquals(slice.limit(), buffer.remaining());
assertEquals(slice.capacity(), buffer.remaining());
assertEquals(1, slice.get());
try {
slice.reset();
fail("Should throw InvalidMarkException");
} catch (InvalidMarkException e) {
}
}
/**
* Similar to {@link ByteBuffer}.reset(), ensures that this MBB
* is reset back to last marked position.
* @return This MBB
*/
@Override
public MultiByteBuff reset() {
checkRefCount();
// when the buffer is moved to the next one.. the reset should happen on the previous marked
// item and the new one should be taken as the base
if (this.markedItemIndex < 0) throw new InvalidMarkException();
ByteBuffer markedItem = this.items[this.markedItemIndex];
markedItem.reset();
this.curItem = markedItem;
// All items after the marked position upto the current item should be reset to 0
for (int i = this.curItemIndex; i > this.markedItemIndex; i--) {
this.items[i].position(0);
}
this.curItemIndex = this.markedItemIndex;
return this;
}
/**
* Invokes {@link ChannelImageOutputStream#reset()} {@code nbMarks} times and verify that the stream position
* is the expected one. This method will then write random values at those positions, and finally compare the
* stream content.
*/
private void compareMarks(int nbMarks) throws IOException {
final ImageOutputStream referenceStream = (ImageOutputStream) this.referenceStream;
while (--nbMarks >= 0) {
referenceStream.reset();
testedStream.reset();
assertEquals(referenceStream.getBitOffset(), testedStream.getBitOffset());
assertEquals(referenceStream.getStreamPosition(), testedStream.getStreamPosition());
final long v = random.nextLong();
referenceStream.writeLong(v);
testedStream.writeLong(v);
}
/*
* Verify that we have no remaining marks, and finally compare stream content.
*/
try {
testedStream.reset();
fail("Expected no remaining marks.");
} catch (InvalidMarkException e) {
// This is the expected exception.
}
assertStreamContentEquals();
}
@Override
public QpidByteBuffer reset()
{
if (_resetFragmentIndex < 0)
{
throw new InvalidMarkException();
}
final SingleQpidByteBuffer fragment = _fragments[_resetFragmentIndex];
fragment.reset();
for (int i = _resetFragmentIndex + 1, size = _fragments.length; i < size; ++i)
{
_fragments[i].position(0);
}
return this;
}
@Override
public CompositeReadableBuffer reset() {
if (mark < 0) {
throw new InvalidMarkException();
}
position(mark);
return this;
}
@Test
public void testPositionWithOneArrayAppended() {
CompositeReadableBuffer buffer = new CompositeReadableBuffer();
buffer.append(new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 });
assertEquals(10, buffer.capacity());
assertEquals(10, buffer.limit());
assertEquals(0, buffer.position());
buffer.position(5);
assertEquals(5, buffer.position());
buffer.position(6);
assertEquals(6, buffer.position());
buffer.position(10);
assertEquals(10, buffer.position());
try {
buffer.position(11);
fail("Should throw a IllegalArgumentException");
} catch (IllegalArgumentException e) {}
buffer.mark();
buffer.position(0);
assertEquals(0, buffer.position());
try {
buffer.reset();
fail("Should throw InvalidMarkException");
} catch (InvalidMarkException ime) {}
}
@Test
public void testReset() {
CompositeReadableBuffer buffer = new CompositeReadableBuffer();
buffer.append(new byte[] {0, 1, 2, 3, 4}).append(new byte[] {5, 6, 7, 8, 9});
try {
buffer.reset();
fail("Should throw InvalidMarkException when not marked.");
} catch (InvalidMarkException e) {
}
// save state
int oldPosition = buffer.position();
int oldLimit = buffer.limit();
assertEquals(0, oldPosition);
assertEquals(10, oldLimit);
buffer.mark();
buffer.position(buffer.limit());
buffer.reset();
assertEquals(buffer.position(), oldPosition);
buffer.mark();
buffer.position(buffer.limit());
buffer.reset();
assertEquals(buffer.position(), oldPosition);
// Can call as mark is not cleared on reset.
CompositeReadableBuffer self = buffer.reset();
assertSame(self, buffer);
}
@Test
public void testFlipSliceWithOneArray() {
CompositeReadableBuffer buffer = new CompositeReadableBuffer();
buffer.append(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
buffer.mark();
buffer.position(1);
buffer.limit(buffer.remaining() - 1);
ReadableBuffer slice = buffer.slice();
assertEquals(1, slice.get(0));
slice.position(1);
slice.mark();
slice.flip();
assertEquals(1, slice.get(0));
assertEquals(1, slice.limit());
try {
slice.reset();
fail("Should throw InvalidMarkException");
} catch (InvalidMarkException e) {
}
buffer.reset();
assertEquals(0, buffer.position());
assertEquals(buffer.remaining(), buffer.limit());
}
@Test
public void testFlipSliceWithMultipleArrays() {
CompositeReadableBuffer buffer = new CompositeReadableBuffer();
buffer.append(new byte[] {0, 1, 2, 3, 4}).append(new byte[] {5, 6, 7, 8, 9});
buffer.mark();
buffer.position(5);
ReadableBuffer slice = buffer.slice();
assertEquals(5, slice.get(0));
slice.position(1);
slice.mark();
slice.flip();
assertEquals(5, slice.get(0));
assertEquals(1, slice.limit());
try {
slice.reset();
fail("Should throw InvalidMarkException");
} catch (InvalidMarkException e) {
}
buffer.reset();
assertEquals(0, buffer.position());
assertEquals(buffer.remaining(), buffer.limit());
}
@Test
public void testSliceWithSingleArrayContent() {
CompositeReadableBuffer buffer = new CompositeReadableBuffer();
buffer.append(new byte[] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
buffer.mark();
// Sliced contents should be the same as buffer
CompositeReadableBuffer slice = buffer.slice();
assertNotSame(buffer, slice);
assertEquals(buffer.capacity(), slice.capacity());
assertEquals(buffer.position(), slice.position());
assertEquals(buffer.limit(), slice.limit());
assertContentEquals(buffer, slice);
// Sliced position, mark, and limit should be independent to buffer
try {
slice.reset();
fail("Mark should be undefined in the slice and throw InvalidMarkException");
} catch (InvalidMarkException e) {}
assertEquals(slice.position(), 0);
slice.clear();
assertEquals(10, buffer.limit());
buffer.reset();
assertEquals(buffer.position(), 0);
// One array buffer should share backing array
assertTrue(buffer.hasArray());
assertTrue(slice.hasArray());
assertSame(buffer.array(), slice.array());
assertTrue(buffer.getArrays().isEmpty());
assertTrue(slice.getArrays().isEmpty());
assertSame(buffer.getArrays(), slice.getArrays()); // Same Empty Buffer
}
@Override
public synchronized void reset() throws IOException {
try {
buffer.reset();
} catch (InvalidMarkException e) {
throw (IOException) (new IOException().initCause(e));
}
}
@Override
public Buffer reset() {
if (mark == -1)
throw new InvalidMarkException();
position = mark;
return this;
}
@Override
public Buffer reset() {
if (mark == -1) {
throw new InvalidMarkException();
}
position = mark;
return this;
}
@Override
public void revert() throws OperationNotSupportedException {
if (currentMark == INVALID_MARK) {
throw new InvalidMarkException();
}
byteBuffer.position(currentMark);
currentMark = INVALID_MARK;
}
@Override
public CloverBuffer getByteSequence(int relativeEnd) throws OperationNotSupportedException, InvalidMarkException {
if (currentMark == INVALID_MARK) {
throw new InvalidMarkException();
}
int pos = byteBuffer.position();
byteBuffer.position(currentMark);
CloverBuffer seq = byteBuffer.slice();
seq.limit(pos + relativeEnd - currentMark); // set the end of the sequence
byteBuffer.position(pos); // restore original position
currentMark = INVALID_MARK;
return seq;
}
@Override
public void revert() throws OperationNotSupportedException {
if (currentMark == INVALID_MARK) {
throw new InvalidMarkException();
}
charBuffer.position(currentMark);
// discard mark automatically to avoid problems with instance user forgetting to discard it explicitly
currentMark = INVALID_MARK;
}
@Override
public CharSequence getCharSequence(int relativeEnd) throws OperationNotSupportedException,
InvalidMarkException {
if (currentMark == INVALID_MARK) {
throw new InvalidMarkException();
}
CharSequence seq = String.copyValueOf(charBuffer.array(), charBuffer.arrayOffset() + currentMark, charBuffer.arrayOffset() + charBuffer.position() + relativeEnd - currentMark);
// CharSequence seq = CharBuffer.wrap(charBuffer.array(), charBuffer.arrayOffset() + currentMark, charBuffer.arrayOffset() + charBuffer.position() + relativeEnd - currentMark);
currentMark = INVALID_MARK;
return seq;
}
@Override
public void revert() throws OperationNotSupportedException {
if (currentMark == INVALID_MARK) {
throw new InvalidMarkException();
}
charBuffer.position(currentMark);
byteBuffer.position(currentMark);
// discard mark automatically to avoid problems with instance user forgetting to discard it explicitly
currentMark = INVALID_MARK;
}
@Override
public CharSequence getCharSequence(int relativeEnd) throws OperationNotSupportedException,
InvalidMarkException {
if (currentMark == INVALID_MARK) {
throw new InvalidMarkException();
}
CharSequence seq = new String(charBuffer.array(), charBuffer.arrayOffset() + currentMark, charBuffer.arrayOffset() + charBuffer.position() + relativeEnd - currentMark);
// discard mark automatically to avoid problems with instance user forgetting to discard it explicitly
currentMark = INVALID_MARK;
return seq;
}
@Override
public CloverBuffer getByteSequence(int relativeEnd) throws OperationNotSupportedException, InvalidMarkException {
if (currentMark == INVALID_MARK) {
throw new InvalidMarkException();
}
int pos = byteBuffer.position();
byteBuffer.position(currentMark);
CloverBuffer seq = CloverBuffer.wrap(byteBuffer.slice());
seq.limit(pos + relativeEnd - currentMark); // set the end of the sequence
byteBuffer.position(pos); // restore original position
currentMark = INVALID_MARK;
return seq;
}
@Override
public CharSequence getCharSequence(int relativeEnd) throws OperationNotSupportedException,
InvalidMarkException {
if (currentCharMark == INVALID_MARK) {
throw new InvalidMarkException();
}
CharSequence seq = new String(charBuffer.array(), charBuffer.arrayOffset() + currentCharMark, charBuffer.arrayOffset() + charBuffer.position() + relativeEnd - currentCharMark);
// discard mark automatically to avoid problems with instance user forgetting to discard it explicitly
currentCharMark = currentByteMark = INVALID_MARK;
return seq;
}
@Override
public CloverBuffer getByteSequence(int relativeEnd) throws OperationNotSupportedException, InvalidMarkException {
if (currentByteMark == INVALID_MARK) {
throw new InvalidMarkException();
}
int pos = byteBuffer.position();
byteBuffer.position(currentByteMark);
CloverBuffer seq = byteBuffer.slice();
seq.limit(pos + relativeEnd - currentByteMark); // set the end of the sequence
byteBuffer.position(pos); // restore original position
currentByteMark = INVALID_MARK;
return seq;
}
/**
* Returns a subsequence starting at the position of the outer mark.
* @param relativeEnd
* @return
* @throws OperationNotSupportedException
* @throws InvalidMarkException
*/
public Object getOuterSequence(int relativeEnd) throws OperationNotSupportedException, InvalidMarkException {
inputReader.setMark(outerMark);
try {
return inputReader.getByteSequence(relativeEnd);
} catch (OperationNotSupportedException e) {
return inputReader.getCharSequence(relativeEnd);
}
}
@Override
public InputStream newInput() {
return new InputStream() {
private final ByteBuffer buf = buffer.slice();
@Override
public void mark(int readlimit) {
buf.mark();
}
@Override
public boolean markSupported() {
return true;
}
@Override
public void reset() throws IOException {
try {
buf.reset();
} catch (InvalidMarkException e) {
throw new IOException(e);
}
}
@Override
public int available() throws IOException {
return buf.remaining();
}
@Override
public int read() throws IOException {
if (!buf.hasRemaining()) {
return -1;
}
return buf.get() & 0xFF;
}
@Override
public int read(byte[] bytes, int off, int len) throws IOException {
if (!buf.hasRemaining()) {
return -1;
}
len = Math.min(len, buf.remaining());
buf.get(bytes, off, len);
return len;
}
};
}