Hi, Trustin:

The call I am using is buf.getString( decoder ).  The limit does not change, but the position does; it remains at the "broken" byte.  I thought it would simply read the buffer between position() and limit() without changing anything - did I misunderstand this?

Yigal


Trustin Lee wrote:
On 10/16/07, Yigal Rachman <[EMAIL PROTECTED]> wrote:
  
Hi, Folks:

When ByteBuffer.getString() breaks midway because of a
CharacterCodingException, it fails to restore the position and limit;
thus getString() is destructive when this happens.  One can, of course,
work around this, but it should not be necessary.
    

Could you please specify some example code that reproduces the
problem?  We already have a test case for the same case, and it seems
to work fine:

    public void testGetStringWithFailure() throws Exception {
        String test = "\u30b3\u30e1\u30f3\u30c8\u7de8\u96c6";
        ByteBuffer buffer = ByteBuffer.wrap(test.getBytes("Shift_JIS"));

        // Make sure the limit doesn't change when an exception arose.
        int oldLimit = buffer.limit();
        int oldPos = buffer.position();
        try {
            buffer.getString(3, Charset.forName("ASCII").newDecoder());
            Assert.fail();
        } catch (Exception e) {
            Assert.assertEquals(oldLimit, buffer.limit());
            Assert.assertEquals(oldPos, buffer.position());
        }

        try {
            buffer.getString(Charset.forName("ASCII").newDecoder());
            Assert.fail();
        } catch (Exception e) {
            Assert.assertEquals(oldLimit, buffer.limit());
            Assert.assertEquals(oldPos, buffer.position());
        }
    }

HTH,
Trustin
  

Reply via email to