• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

int to byte[] gives BufferOverflowException

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am trying to convert int to a byte array with the code below


I am getting a BufferOverflowException, if I allocate one byte for 2. Isn't it supposed to take only 1 byte? Unless I allocate 4 bytes, it gives the exception (even if I allocate 2 or 3 bytes). This might be something very simple and I might have asked a pretty silly question. But, what am I missing here?
 
Ranch Hand
Posts: 479
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
From the javadoc for ByteBuffer.putInt(int value):

Writes four bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by four.

rc
 
Raghava Mutharaju
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ok, I got it now. Thank you for pointing it out

Regards,
Raghava.
reply
    Bookmark Topic Watch Topic
  • New Topic