• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Apache Commons: IOUtils.write - how to restrict bytes-stream during write

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I realised reading a file into bytes-array with next writing this array to file in another directory by the following approach:


And this code done well - in target directory appear new file with the relevant content (for example: "abcd").

Further I tried to improve my approach through ApacheCommons abilities.



But this approach entails the problem: in target directory the new file appeals, but this written content is irrelevant. Besides the written text (for example: "abcd") it contains some unreadable unencoded symbols, and I see the warning about problem with encoding.

I suppose this problem is related to the fact, that some excess bytes read and/or write to OutpumStream. And this problem (probably) arise due to the fact I don't restrict read and write-operations, applying IOUtils (second approach).

And in first approach I restrict the write of byte-array in OutputStream by size of length-variable:


If problem in that I must have a way to restrict the write by tools of AppacheCommons.
But I don't see any way to do so in IOUtils-methods.
 
Sheriff
Posts: 22116
114
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're writing to a ZipOutputStream. That class doesn't just write content directly but creates ZIP files. That includes some "magic" code in between the possibly readable stuff. But the thing is, you shouldn't try to read it as a text file, because ZIP files are not text files, they are always binary files.

By the way, if you're using IOUtils, why not use its copy methods? Because there is another issue with your second code snippet that the first one didn't have. You create a byte[] of size BUFFER_SIZE. That's a byte array with all 0 bytes. You then read into it. There are three possibilities:
1) The file size is exactly BUFFER_SIZE. Everything will work just fine.
2) The file size is larger than BUFFER_SIZE. The output file will only contain the first BUFFER_SIZE bytes.
3) The file size is smaller than BUFFER_SIZE. The output file will have a size of exactly BUFFER_SIZE bytes - each byte after the file size will be the 0 they started out as, and those are also written to the file.
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic