aspose file tools*
The moose likes Beginning Java and the fly likes IndexOutOfBoundsException thrown by java.nio bytebuffer.put(byte[] arsrc, int offset , int length)  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "IndexOutOfBoundsException thrown by java.nio bytebuffer.put(byte[] arsrc, int offset , int length)  " Watch "IndexOutOfBoundsException thrown by java.nio bytebuffer.put(byte[] arsrc, int offset , int length)  " New topic
Author

IndexOutOfBoundsException thrown by java.nio bytebuffer.put(byte[] arsrc, int offset , int length)

Guy Rich
Ranch Hand

Joined: May 03, 2011
Posts: 33

I'm curious as to why I'm getting this runtime error when from my perspective I shouldn't

here's the code section:


This is what's displayed when I run the program:
2011.05.12 10:48:07
LU62XnsCvr Diagnostic:
LU62XCI0100: Method = SendMesg
Message to be sent:2011.05.12 10:48:07 LU62XCE0313: CPIC Return Code =1 CM Alloc ConversationID=[B@201d201d
mesg_str Length=89
Derived mesgwork Length=192
Class Var MGBuffer length value:192
Buffer Offset Value=0
LU62XnsCvr End Diagnostic

Exception in thread "main" java.lang.IndexOutOfBoundsException
at java.nio.Buffer.checkBounds(Buffer.java:543)
at java.nio.HeapByteBuffer.put(HeapByteBuffer.java:177)
at APPC_LU62.Runtime.LU62XnsCvr.SendMesg(LU62XnsCvr.java:652)
at APPC_LU62.Runtime.LU62XnsCvr.StartConvrs(LU62XnsCvr.java:517)
at APPC_LU62.Runtime.LU62XnsCvr.ProcessRqsts(LU62XnsCvr.java:398)
at APPC_LU62.Runtime.LU62XnsCvr.main(LU62XnsCvr.java:357)

here's integer variable MGBUFLN declared prior to any reference to it within LU62XnsCvr Class
final static int MGBUFLN = 192 ; //Message Buffer Length

here's the byte array that's used as the "source" declared as a LU62XnsCvr Class member variable...
static byte[] mesgwork = new byte[MGBUFLN] ;

This I copied from the Oracle Java Doc website; don't know exactly how current it is, but it's marked as java 6
and I'm running IBM's SDK which is using java 1.6

put
public ByteBuffer put(byte[] src,
int offset,
int length)
Relative bulk put method (optional operation).
This method transfers bytes into this buffer from the given source array. If there are more bytes to be copied from the array than remain in this buffer, that is, if length > remaining(), then no bytes are transferred and a BufferOverflowException is thrown.
Otherwise, this method copies length bytes from the given array into this buffer, starting at the given offset in the array and at the current position of this buffer. The position of this buffer is then incremented by length.
In other words, an invocation of this method of the form dst.put(src, off, len) has exactly the same effect as the loop
for (int i = off; i < off + len; i++)
dst.put(a[i]);
except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.
Parameters:
src - The array from which bytes are to be read
offset - The offset within the array of the first byte to be read; must be non-negative and no larger than array.length
length - The number of bytes to be read from the given array; must be non-negative and no larger than array.length - offset
Returns:
This buffer
Throws:
BufferOverflowException - If there is insufficient space in this buffer
IndexOutOfBoundsException - If the preconditions on the offset and length parameters do not hold
ReadOnlyBufferException - If this buffer is read-only
________________________________________

I'm a little concerned with the statements:
Otherwise, this method copies length bytes from the given array into this buffer, starting at the given offset in the array and at the current position of this buffer. The position of this buffer is then incremented by length.

and then:
except that it first checks that there is sufficient space in this buffer and it is potentially much more efficient.

Now I want to completely "fill" the 192 byte buffer (index therefore ranges from 0 - 191)
So IF as is put forth in the doc, the buffer is "incremented" by the length (192 bytes in this case)
then it appears to me by implication the "logic" is going to add 192 bytes to index and low and behold ... we're out of bounds on the index ...

I'd really appreciate anyone's opinion on this. Waiting for your comments and/or suggestions...

Thanks

Guy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IndexOutOfBoundsException thrown by java.nio bytebuffer.put(byte[] arsrc, int offset , int length)