|
| 1 | +package com.emc.ecs.sync.util; |
| 2 | + |
| 3 | +import com.emc.object.util.ProgressListener; |
| 4 | +import com.emc.object.util.ProgressOutputStream; |
| 5 | +import com.filepool.fplibrary.FPTag; |
| 6 | +import org.slf4j.Logger; |
| 7 | +import org.slf4j.LoggerFactory; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.OutputStream; |
| 11 | +import java.io.PipedInputStream; |
| 12 | +import java.io.PipedOutputStream; |
| 13 | +import java.security.DigestInputStream; |
| 14 | +import java.security.MessageDigest; |
| 15 | +import java.security.NoSuchAlgorithmException; |
| 16 | + |
| 17 | +public class BlobInputStream extends CountingInputStream { |
| 18 | + private static final Logger log = LoggerFactory.getLogger(BlobInputStream.class); |
| 19 | + |
| 20 | + private FPTag tag; |
| 21 | + BlobReader reader; |
| 22 | + Thread readerThread; |
| 23 | + |
| 24 | + public BlobInputStream(FPTag tag, int bufferSize) throws IOException { |
| 25 | + this(tag, bufferSize, null); |
| 26 | + } |
| 27 | + |
| 28 | + public BlobInputStream(FPTag tag, int bufferSize, ProgressListener listener) throws IOException { |
| 29 | + super(null); |
| 30 | + this.tag = tag; |
| 31 | + |
| 32 | + // piped streams and a reader task are necessary because of the odd stream handling in the CAS JNI wrapper |
| 33 | + PipedInputStream pin = new PipedInputStream(bufferSize); |
| 34 | + PipedOutputStream pout = new PipedOutputStream(pin); |
| 35 | + |
| 36 | + try { |
| 37 | + in = new DigestInputStream(pin, MessageDigest.getInstance("md5")); |
| 38 | + } catch (NoSuchAlgorithmException e) { |
| 39 | + throw new RuntimeException("could not initialize MD5 digest", e); |
| 40 | + } |
| 41 | + |
| 42 | + OutputStream out = pout; |
| 43 | + if (listener != null) out = new ProgressOutputStream(out, listener); |
| 44 | + |
| 45 | + reader = new BlobReader(out); |
| 46 | + readerThread = new Thread(reader); |
| 47 | + readerThread.start(); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public int read() throws IOException { |
| 52 | + checkReader(); |
| 53 | + int result = super.read(); |
| 54 | + checkReader(); // also check after read in case we were blocked |
| 55 | + return result; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public int read(byte[] b) throws IOException { |
| 60 | + return read(b, 0, b.length); |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public int read(byte[] b, int off, int len) throws IOException { |
| 65 | + checkReader(); |
| 66 | + int result = super.read(b, off, len); |
| 67 | + checkReader(); // also check after read in case we were blocked |
| 68 | + return result; |
| 69 | + } |
| 70 | + |
| 71 | + private void checkReader() throws IOException { |
| 72 | + if (reader.isFailed()) throw new IOException("blob reader failed", reader.getError()); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public void close() throws IOException { |
| 77 | + try { |
| 78 | + super.close(); |
| 79 | + } finally { |
| 80 | + |
| 81 | + // if the reader is active, closing the pipe (above) will throw an exception in PipedOutputStream.write |
| 82 | + // if it is waiting for buffer space however, it will need to be interrupted or it will be frozen indefinitely |
| 83 | + if (!reader.isComplete() && !reader.isFailed()) |
| 84 | + readerThread.interrupt(); |
| 85 | + |
| 86 | + // if the reader is complete, this does nothing; if close was called early, this will wait until the reader |
| 87 | + // thread is notified of the close (an IOException will be thrown from PipedOutputStream.write) |
| 88 | + try { |
| 89 | + readerThread.join(); |
| 90 | + } catch (Throwable t) { |
| 91 | + log.warn("could not join reader thread", t); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public byte[] getMd5Digest() { |
| 97 | + if (!(in instanceof DigestInputStream)) throw new UnsupportedOperationException("MD5 checksum is not enabled"); |
| 98 | + if (!isClosed()) throw new UnsupportedOperationException("cannot get MD5 until stream is closed"); |
| 99 | + return ((DigestInputStream) in).getMessageDigest().digest(); |
| 100 | + } |
| 101 | + |
| 102 | + class BlobReader implements Runnable { |
| 103 | + private OutputStream out; |
| 104 | + private volatile boolean complete = false; |
| 105 | + private volatile boolean failed = false; |
| 106 | + private volatile Throwable error; |
| 107 | + |
| 108 | + BlobReader(OutputStream out) { |
| 109 | + this.out = out; |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public synchronized void run() { |
| 114 | + try (OutputStream outputStream = out) { |
| 115 | + tag.BlobRead(outputStream); |
| 116 | + complete = true; |
| 117 | + } catch (Throwable t) { |
| 118 | + failed = true; |
| 119 | + error = t; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + public boolean isComplete() { |
| 124 | + return complete; |
| 125 | + } |
| 126 | + |
| 127 | + public boolean isFailed() { |
| 128 | + return failed; |
| 129 | + } |
| 130 | + |
| 131 | + public Throwable getError() { |
| 132 | + return error; |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments