Win a copy of Penetration Testing Basics this week in the Security forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Byte Array returned from ByteArrayOutputStream.

Akhil Jain
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I have an object which i want to serialize to a file, for which i am using ByteArrayOutputStream and ObjectOutputStream along with FileChannel to write it.

I want to write a Header Object (POJO implementing Serializable interface) at the TOP of the file before writting anything (Serialized Objects) to this file. and want to update the same header object when all other objects are being written to the file.

For this i am trying to use a specific Byte Block.
But when i get the byte[] from ByteArrayOutputStream it creates a new byte array with the size of the object in bytes.

eg. i have a header object which is almost 197 bytes in size (just a rough estimate with some testing) and i want to write this header object at TOP by allocating 300 bytes to it. so that i can start writting all other objects after 300 bytes in file.

code snippet is as follows:

1. How we can write any object in serialized form with specific byte size?
2. How can we update that object again with new data, before closing the file or on some condition?

Can someone please guide me , how this can be done, so that before reading file contents, i can actually go to the top and read header object in the begining to get file information.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic