1

I am trying to encrypt an InputStream which I am getting from FileItem.getInputStream(); and I want to know the encrypted stream's length.

If it would be a simple FileInputStream, I could have tried File#length() or FileInputStream#getChennal().size() though not sure even those would have given me exactly what I want, but, in this case what I have is an InputStream (the encrypted one) and I want to find length of the same, I tried searching on Internet but there I could not find any efficient solution to that.

Please help

dev2d
  • 4,245
  • 3
  • 31
  • 54
  • 1
    Isn't [`FileItem#getSize()`](http://commons.apache.org/proper/commons-fileupload/apidocs/org/apache/commons/fileupload/FileItem.html#getSize%28%29) what you need? – bereal Mar 19 '14 at 14:08
  • Why don't you use Java 7's file API? It has `Files.size()` – fge Mar 19 '14 at 14:09
  • He wants to know the size of the encrypted stream. – D.R. Mar 19 '14 at 14:10
  • @bereal : no because it will give me size of stream(file) before encryption – dev2d Mar 19 '14 at 14:10
  • @VD' then it depends on the encryption algorithm and mode of operation. – bereal Mar 19 '14 at 14:12
  • well i can share those details too but ultimately what we want is.. efficient way of knowing length of an InputStream – dev2d Mar 19 '14 at 14:13
  • @VD' I'm a bit confused: you have an input stream which you want to encrypt. You can know the length of that stream from its `FileItem`. Now, you want to know the length of data after encryption, right? – bereal Mar 19 '14 at 14:17
  • Then, that would depend on the encryption algorithm, mode, padding etc. – bereal Mar 19 '14 at 14:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/50045/discussion-between-vd-and-bereal) – dev2d Mar 19 '14 at 14:41

2 Answers2

2

You have to read the whole stream for that. Streams are probably not even complete when you start reading from them, so the size may not be known at that time.

D.R.
  • 20,268
  • 21
  • 102
  • 205
  • Efficient one my friend, this one is there everywhere on the internet! is it advisable to achieve it this way, i may be using files in GBs – dev2d Mar 19 '14 at 14:09
  • @VD': Use a streaming form of encryption - there are plenty of those around. Why do you need the length ahead of time? – Jon Skeet Mar 19 '14 at 14:10
  • I actually need to store it on Cloud, and Cloud API needs it :( – dev2d Mar 19 '14 at 14:11
  • If the CloudAPI does only retrieve a final byte[] you need to load the whole stream into memory anyways. edit: if you want to upload gigabytes at once into the cloud...have fun... – D.R. Mar 19 '14 at 14:12
  • Why don't you just write the encrypted stream to another file and then transfer that file? With Java 7's API it is as easy as two `File.copy()`s away – fge Mar 19 '14 at 14:23
2

You can output whats in InputStream and get the length

ObjA = new ObjA;

while( more message to read ){
ObjA = inputStreamObj.read();
}

System.out.println(ObjA.length());