java.nio.InvalidMarkException Java Examples

The following examples show how to use java.nio.InvalidMarkException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QpidByteBufferTest.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@Test
public void testSettingPositionBackwardsResetsMark()
{
    _parent.position(8);
    _parent.mark();
    _parent.position(7);
    try
    {
        _parent.reset();
        fail("Expected exception not thrown");
    }
    catch (InvalidMarkException e)
    {
        // pass
    }
}
 
Example #2
Source File: ChannelImageOutputStreamTest.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example #3
Source File: MultiByteBuff.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * 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;
}
 
Example #4
Source File: CompositeReadableBufferTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@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) {
    }
}
 
Example #5
Source File: CompositeReadableBufferTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@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) {
    }
}
 
Example #6
Source File: CompositeReadableBufferTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@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) {
    }
}
 
Example #7
Source File: CompositeReadableBufferTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@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) {
    }
}
 
Example #8
Source File: CompositeReadableBufferTest.java    From qpid-proton-j with Apache License 2.0 6 votes vote down vote up
@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) {
    }
}
 
Example #9
Source File: QpidByteBufferTest.java    From qpid-broker-j with Apache License 2.0 6 votes vote down vote up
@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
    }
}
 
Example #10
Source File: CharByteInputReader.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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;
}
 
Example #11
Source File: MultiQpidByteBuffer.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #12
Source File: CharByteInputReader.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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);
	}
}
 
Example #13
Source File: CharByteInputReader.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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;
}
 
Example #14
Source File: CharByteInputReader.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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;
}
 
Example #15
Source File: CharByteInputReader.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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;
}
 
Example #16
Source File: CharByteInputReader.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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;
}
 
Example #17
Source File: CharByteInputReader.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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;
}
 
Example #18
Source File: CharByteInputReader.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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;
			
		}
 
Example #19
Source File: CharByteInputReader.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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;
}
 
Example #20
Source File: CharByteInputReader.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void revert() throws OperationNotSupportedException {
	if (currentMark == INVALID_MARK) {
		throw new InvalidMarkException();
	}
	byteBuffer.position(currentMark);
	currentMark = INVALID_MARK;
}
 
Example #21
Source File: CompositeReadableBufferTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #22
Source File: CompositeReadableBufferTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example #23
Source File: CompositeReadableBufferTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@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) {}
}
 
Example #24
Source File: CompositeReadableBufferTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@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());
}
 
Example #25
Source File: CompositeReadableBuffer.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Override
public CompositeReadableBuffer reset() {
    if (mark < 0) {
        throw new InvalidMarkException();
    }

    position(mark);

    return this;
}
 
Example #26
Source File: CompositeReadableBufferTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@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
}
 
Example #27
Source File: ByteBufferInputStream.java    From offheap-store with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void reset() throws IOException {
  try {
    buffer.reset();
  } catch (InvalidMarkException e) {
    throw (IOException) (new IOException().initCause(e));
  }
}
 
Example #28
Source File: AbstractBuffer.java    From catalyst with Apache License 2.0 5 votes vote down vote up
@Override
public Buffer reset() {
  if (mark == -1)
    throw new InvalidMarkException();
  position = mark;
  return this;
}
 
Example #29
Source File: AbstractBuffer.java    From atomix with Apache License 2.0 5 votes vote down vote up
@Override
public Buffer reset() {
  if (mark == -1) {
    throw new InvalidMarkException();
  }
  position = mark;
  return this;
}
 
Example #30
Source File: NioByteString.java    From play-store-api with GNU General Public License v3.0 4 votes vote down vote up
@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;
    }
  };
}