java.nio bytebuffer.put(byte[] arsrc, int offset , int length) 抛出 IndexOutOfBoundsException
大家好,我希望有人可以帮助我解决这个问题。
我很好奇为什么我会收到此运行时错误,而从我的角度来看,我不应该这样做 这是代码部分:
// Send Message to the Message Log
public static void SendMesg()
{
String mesg_str = message_data.toString() ;
int msgstr_len = mesg_str.length(); // determine actual message length
int array_len = mesgwork.length ; // determine actual mesgwork array length
dt_stamp = getDateTime() ;
System.out.println(dt_stamp) ;
System.out.println( " LU62XnsCvr Diagnostic:");
System.out.println(" LU62XCI0100: Method = SendMesg") ;
System.out.println(" Message to be sent: " ) ;
System.out.println(mesg_str) ;
System.out.println("mesg_str Length=") ;
System.out.println(msgstr_len) ;
System.out.println("Derived mesgwork Length=") ;
System.out.println(array_len) ;
System.out.println("Class Var MGBuffer length value: ") ;
System.out.println(MGBUFLN) ;
System.out.println("Buffer Offset Value=") ;
System.out.println(bufroffset) ;
System.out.println( " LU62XnsCvr End Diagnostic") ;
mesgwork = mesg_str.getBytes() ; //Convert msg string to byte array
mesg_bufr.put( mesgwork, bufroffset, MGBUFLN ) ;// <= error occurs here
pgm_cntl = WRITE_MESG ;
FileControl() ;
if (pgm_cntl == WRITE_ERROR)
{
sys_return = pgm_cntl ;
SysEnd( sys_return ) ;
}
mesgcount = mesgcount + 1 ; // increment the message counter
mesg_bufr.clear() ;
message_data.append(" ") ; // 16 bytes of blanks
clearByteArray( mesgwork, MGBUFLN ) ;
} // End of Send Message log write sub-routine
这是我运行程序时显示的内容:
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)
这是在 LU62XnsCvr 类中对其进行任何引用之前声明的整数变量 MGBUFLN
final static int MGBUFLN = 192 ; //Message Buffer Length
这是用作声明为 LU62XnsCvr 类成员变量的“源”的字节数组...
static byte[] mesgwork = new byte[MGBUFLN] ;
这是我从 Oracle Java Doc 网站复制的;不知道具体的电流是多少,
但它被标记为 java 6 并且我正在运行 IBM 的 SDK,它使用 java 1.6
public ByteBuffer put(byte[] src, int 偏移量, int 长度)
相对批量放置方法(可选 手术)。该方法传输 从给定的字节到此缓冲区 源数组。如果还有更多 要从数组复制的字节数 保留在这个缓冲区中,也就是说,如果 长度>剩余(),则没有字节 被转移并且 抛出 BufferOverflowException。 否则,此方法复制长度 从给定数组中的字节到此 缓冲区,从给定的开始 数组中和当前位置的偏移量 该缓冲区的位置。这 该缓冲区的位置是 按长度递增。在其他方面 的话,调用这个方法 形式
dst.put(src, off, len)
有 与循环效果完全相同for (int i = off; i < off + len; i++) dst.put(a[i]);
除了它首先检查是否存在 该缓冲区中有足够的空间并且它 可能效率更高。
参数:
- src - 数组来自 要读取哪些字节
- 偏移量 - 数组内的偏移量 要读取的第一个字节;必须是 非负且不大于 数组长度
- 长度 - 数量 要从给定数组中读取的字节; 必须为非负且不大于 比 array.length - 偏移量
返回:此缓冲区
抛出:
- BufferOverflowException - 如果存在 该缓冲区空间不足
- IndexOutOfBoundsException - 如果 偏移量和长度的前提条件 参数不成立
- ReadOnlyBufferException - 如果这样 缓冲区是只读的
我有点担心这些语句:
否则,此方法复制长度 从给定数组中的字节到此 缓冲区,从给定的开始 数组中和当前位置的偏移量 该缓冲区的位置。这 该缓冲区的位置是 按长度递增。
进而:
除了它首先检查是否存在 该缓冲区中有足够的空间并且 可能还有更多 高效。
// * 我的附加评论 * //
现在我想完全“填充”192 字节缓冲区(因此索引范围为 0 - 191)
因此,如果如文档中所述,缓冲区将按长度“增加”
(在本例中为 192 字节)
那么在我看来,“逻辑”将向索引添加 192 字节,并且
低了,你瞧...我们超出了索引范围...
我真的很感激任何人对此的看法。
等待您的意见和/或建议...
谢谢
家伙
Hello all I hope someone can help me resolve this issue..
I'm curious as to why I'm getting this runtime error when from my perspective I shouldn't
here's the code section:
// Send Message to the Message Log
public static void SendMesg()
{
String mesg_str = message_data.toString() ;
int msgstr_len = mesg_str.length(); // determine actual message length
int array_len = mesgwork.length ; // determine actual mesgwork array length
dt_stamp = getDateTime() ;
System.out.println(dt_stamp) ;
System.out.println( " LU62XnsCvr Diagnostic:");
System.out.println(" LU62XCI0100: Method = SendMesg") ;
System.out.println(" Message to be sent: " ) ;
System.out.println(mesg_str) ;
System.out.println("mesg_str Length=") ;
System.out.println(msgstr_len) ;
System.out.println("Derived mesgwork Length=") ;
System.out.println(array_len) ;
System.out.println("Class Var MGBuffer length value: ") ;
System.out.println(MGBUFLN) ;
System.out.println("Buffer Offset Value=") ;
System.out.println(bufroffset) ;
System.out.println( " LU62XnsCvr End Diagnostic") ;
mesgwork = mesg_str.getBytes() ; //Convert msg string to byte array
mesg_bufr.put( mesgwork, bufroffset, MGBUFLN ) ;// <= error occurs here
pgm_cntl = WRITE_MESG ;
FileControl() ;
if (pgm_cntl == WRITE_ERROR)
{
sys_return = pgm_cntl ;
SysEnd( sys_return ) ;
}
mesgcount = mesgcount + 1 ; // increment the message counter
mesg_bufr.clear() ;
message_data.append(" ") ; // 16 bytes of blanks
clearByteArray( mesgwork, MGBUFLN ) ;
} // End of Send Message log write sub-routine
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
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 formdst.put(src, off, len)
has
exactly the same effect as the loopfor (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 - offsetReturns: 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.
// * my additional comments * //
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在该行中,
您尝试将 MGBUFLN (即 192)字节从 mesgwork 字节数组传输到缓冲区。但是您的 mesgwork 数组中只有 89 个字节,这就是您收到越界异常的原因。
试试这个:
In the line
you are trying to transfer MGBUFLN (ie 192) bytes from the mesgwork byte array to the buffer. But your mesgwork array has only 89 bytes in it, which is why you are getting the out of bounds exception.
try this:
mesgwork
是一个包含的元素少于bufroffset + MGBUFLN
的数组,由于您使用 size =
MGBULFN
初始化数组,那么我会假设您的bufferoffset
必须始终为 0 才能正常工作。除此之外 -
MGBULFN
是一个相当神秘的常量名称。考虑重命名它。mesgwork
is an array that contains less elements thanbufroffset + MGBUFLN
Since you initialize your array with size =
MGBULFN
, then I would assume yourbufferoffset
must be always 0 in order for this to work.Apart from that -
MGBULFN
is a rather cryptic name for a constant. Consider renaming it.