Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Input streams aren't really designed for creating copies or re-using, they're specifically for situations where you don't want to read off into a byte array and use array operations on that (this is especially useful when the whole array isn't available, as in, for e.g. socket comunication). You could buffer up into a byte array, which is the process of reading sections from the stream into a byte array buffer until you have enough information.</p> <p>But that's unnecessary for calculating an md5. Notice that <a href="http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html" rel="nofollow noreferrer">InputStream</a> is abstract, so it needs be implemented in an extended class. It has many implementations- <a href="http://docs.oracle.com/javase/6/docs/api/java/util/zip/GZIPInputStream.html" rel="nofollow noreferrer"><code>GZIPInputStream</code></a>, fileinputstream etc. These are, in design pattern speak, <a href="http://www.javaworld.com/javaworld/jw-12-2001/jw-1214-designpatterns.html" rel="nofollow noreferrer">decorators</a> of the IO stream: they add extra functionality to the abstract base IO classes. For example, <code>GZIPInputStream</code> gzips up the stream.</p> <p>So, what you need is a stream to do this for md5. There is, joyfully, a well documented similar thing: see <a href="https://stackoverflow.com/questions/304268/getting-a-files-md5-checksum-in-java">this</a> answer. So you should just be able to pass your dropbox input stream (as it will be itself an input stream) to create a new <a href="http://docs.oracle.com/javase/6/docs/api/java/security/DigestInputStream.html" rel="nofollow noreferrer"><code>DigestInputStream</code></a>, and then you can both take the md5 and continue to read as before.</p> <p>Worried about type casting? The idea with decorators in Java is that, since the <code>InputStream</code> <em>base class</em> interfaces all the methods and 'beef' you need to do your IO, there's no harm in passing instances of objects inheriting from <code>InputStream</code> in the constructor of each stream implementation, and you can still do the same core IO.</p> <p>Finally, I should probably answer your actual question- say you <em>still</em> want to "cache" or "backup" the stream anyway? Well, you could just write it to a byte array. This is well documented, but can become faff when your streams get more complicated. Alternatively, try looking at a <a href="http://docs.oracle.com/javase/6/docs/api/java/io/PushbackInputStream.html" rel="nofollow noreferrer"><code>PushbackInputStream</code></a>. Here, you can easily write a function to read off n bytes, perform and operation on them, and then restore them to the stream. Generally good to avoid these implementations of streams in Java, as it's bad for memory use, but no worse than buffering everything up which you'd otherwise have to do.</p> <p>Or, of course, I would have a go with <code>DigestInputStream</code>.</p> <p>Hope this helps,</p> <p>Best.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload