@Override public int read() throws IOException { if (this.bytesReceived == this.bytesToReceive) { return -1; } final int read = this.wrappedInputStream.read(); if (read < 0) { throwEOFException(); } ++this.bytesReceived; if (this.md != null) { this.md.update((byte) read); if (this.bytesReceived == this.bytesToReceive) { final BlobKey computedKey = new BlobKey(this.md.digest()); if (!computedKey.equals(this.blobKey)) { throw new IOException("Detected data corruption during transfer"); } } } return read; }
@Override public int read(byte[] b, int off, int len) throws IOException { final int bytesMissing = this.bytesToReceive - this.bytesReceived; if (bytesMissing == 0) { return -1; } final int maxRecv = Math.min(len, bytesMissing); final int read = this.wrappedInputStream.read(b, off, maxRecv); if (read < 0) { throwEOFException(); } this.bytesReceived += read; if (this.md != null) { this.md.update(b, off, read); if (this.bytesReceived == this.bytesToReceive) { final BlobKey computedKey = new BlobKey(this.md.digest()); if (!computedKey.equals(this.blobKey)) { throw new IOException("Detected data corruption during transfer"); } } } return read; }
private BlobKey receivePutResponseAndCompare(InputStream is, MessageDigest md) throws IOException { int response = is.read(); if (response < 0) { throw new EOFException("Premature end of response"); } else if (response == RETURN_OKAY) { if (md == null) { // not content addressable return null; } BlobKey remoteKey = BlobKey.readFromInputStream(is); BlobKey localKey = new BlobKey(md.digest()); if (!localKey.equals(remoteKey)) { throw new IOException("Detected data corruption during transfer"); } return localKey; } else if (response == RETURN_ERROR) { Throwable cause = readExceptionFromStream(is); throw new IOException("Server side error: " + cause.getMessage(), cause); } else { throw new IOException("Unrecognized response: " + response + '.'); } }