public int lengthAndChecksumOverhead() { return 8 + checksumLength(); }
private void skipChecksummedObject(ObjectInputStream is, long totalLength) throws IOException { long length = is.readLong(); if(length > totalLength) throw new IOException("Too long: "+length+" > "+totalLength); FileUtil.skipFully(is, length + checker.checksumLength()); }
private void writeRequestIdentifier(DataOutput os, RequestIdentifier req) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); OutputStream oos = checker.checksumWriter(baos); DataOutputStream dos = new DataOutputStream(oos); req.writeTo(dos); dos.close(); byte[] buf = baos.toByteArray(); os.writeShort(buf.length - checker.checksumLength()); os.write(buf); }
void readStatus() throws IOException, ChecksumFailedException, StorageFormatException { byte[] data = new byte[statusLength-parent.checker.checksumLength()]; parent.preadChecksummed(parent.crossSegmentStatusOffset(segNo), data, 0, data.length); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); if(dis.readInt() != segNo) throw new StorageFormatException("Bad segment number"); encoded = dis.readBoolean(); }
public void readStatus() throws IOException, ChecksumFailedException, StorageFormatException { byte[] data = new byte[statusLength-parent.checker.checksumLength()]; parent.preadChecksummed(parent.getOffsetSegmentStatus(segNo), data, 0, data.length); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(data)); if(dis.readInt() != segNo) throw new StorageFormatException("Bad segment number"); encoded = dis.readBoolean(); blockChooser.read(dis); }
void preadChecksummed(long fileOffset, byte[] buf, int offset, int length) throws IOException, ChecksumFailedException { byte[] checksumBuf = new byte[checker.checksumLength()]; RAFLock lock = raf.lockOpen(); try { raf.pread(fileOffset, buf, offset, length); raf.pread(fileOffset+length, checksumBuf, 0, checker.checksumLength()); } finally { lock.unlock(); } if(!checker.checkChecksum(buf, offset, length, checksumBuf)) { Arrays.fill(buf, offset, offset+length, (byte)0); throw new ChecksumFailedException(); } }
ClientCHK readKey(int blockNumber) throws IOException, MissingKeyException { byte[] buf = parent.innerReadSegmentKey(segNo, blockNumber); ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataOutputStream dos = new DataOutputStream(baos); dos.writeInt(segNo); dos.writeInt(blockNumber); dos.close(); byte[] prefix = baos.toByteArray(); byte[] checkBuf = new byte[prefix.length + buf.length]; System.arraycopy(prefix, 0, checkBuf, 0, prefix.length); int checksumLength = parent.checker.checksumLength(); System.arraycopy(buf, 0, checkBuf, prefix.length, buf.length - checksumLength); byte[] checksum = Arrays.copyOfRange(buf, buf.length - checksumLength, buf.length); if(parent.checker.checkChecksum(checkBuf, 0, checkBuf.length, checksum)) throw new MissingKeyException(); DataInputStream dis = new DataInputStream(new ByteArrayInputStream(buf)); byte b = dis.readByte(); if(b != 1) throw new MissingKeyException(); ClientCHK key = innerReadKey(dis); setHasKey(blockNumber); if(logDEBUG) Logger.debug(this, "Returning "+key); return key; }
public SplitFileInserterCrossSegmentStorage(SplitFileInserterStorage parent, int segNo, boolean persistent, int segLen, int crossCheckBlocks) { this.parent = parent; this.segNo = segNo; this.dataBlockCount = segLen; this.crossCheckBlockCount = crossCheckBlocks; this.totalBlocks = dataBlockCount + crossCheckBlocks; segments = new SplitFileInserterSegmentStorage[totalBlocks]; blockNumbers = new int[totalBlocks]; try { CountedOutputStream cos = new CountedOutputStream(new NullOutputStream()); DataOutputStream dos = new DataOutputStream(cos); innerStoreStatus(dos); dos.close(); statusLength = (int) cos.written() + parent.checker.checksumLength(); } catch (IOException e) { throw new Error(e); // Impossible } }
byte[] preadChecksummedWithLength(long fileOffset) throws IOException, ChecksumFailedException, StorageFormatException { byte[] checksumBuf = new byte[checker.checksumLength()]; RAFLock lock = raf.lockOpen(); byte[] lengthBuf = new byte[8]; byte[] buf; int length; try { raf.pread(fileOffset, lengthBuf, 0, lengthBuf.length); long len = new DataInputStream(new ByteArrayInputStream(lengthBuf)).readLong(); if(len + fileOffset > rafLength || len > Integer.MAX_VALUE || len < 0) throw new StorageFormatException("Bogus length "+len); length = (int)len; buf = new byte[length]; raf.pread(fileOffset+lengthBuf.length, buf, 0, length); raf.pread(fileOffset+length+lengthBuf.length, checksumBuf, 0, checker.checksumLength()); } finally { lock.unlock(); } if(!checker.checkChecksum(buf, 0, length, checksumBuf)) { Arrays.fill(buf, 0, length, (byte)0); throw new ChecksumFailedException(); } return buf; }
public SplitFileInserterSegmentStorage(SplitFileInserterStorage parent, int segNo, boolean persistent, int dataBlocks, int checkBlocks, int crossCheckBlocks, int keyLength, byte splitfileCryptoAlgorithm, byte[] splitfileCryptoKey, Random random, int maxRetries, int consecutiveRNFsCountAsSuccess, KeysFetchingLocally keysFetching) { this.parent = parent; this.segNo = segNo; this.dataBlockCount = dataBlocks; this.checkBlockCount = checkBlocks; this.crossCheckBlockCount = crossCheckBlocks; totalBlockCount = dataBlockCount + crossCheckBlockCount + checkBlockCount; this.keyLength = keyLength; crossSegmentBlockSegments = new SplitFileInserterCrossSegmentStorage[crossCheckBlocks]; crossSegmentBlockNumbers = new int[crossCheckBlocks]; blocksHaveKeys = new boolean[totalBlockCount]; this.splitfileCryptoAlgorithm = splitfileCryptoAlgorithm; this.splitfileCryptoKey = splitfileCryptoKey; crossDataBlocksAllocated = new boolean[dataBlocks + crossCheckBlocks]; blockChooser = new SplitFileInserterSegmentBlockChooser(this, totalBlockCount, random, maxRetries, keysFetching, consecutiveRNFsCountAsSuccess); try { CountedOutputStream cos = new CountedOutputStream(new NullOutputStream()); DataOutputStream dos = new DataOutputStream(cos); innerStoreStatus(dos); dos.close(); statusLength = (int) cos.written() + parent.checker.checksumLength(); } catch (IOException e) { throw new Error(e); // Impossible } }
innerStoreStatus(dos); dos.close(); int computedStatusLength = (int) cos.written() + parent.checker.checksumLength(); if(computedStatusLength > statusLength) throw new StorageFormatException("Stored status length smaller than required");
innerStoreStatus(dos); dos.close(); int minStatusLength = (int) cos.written() + parent.checker.checksumLength(); if(minStatusLength > statusLength) throw new StorageFormatException("Bad status length (too short)");
this.random = random; this.checksumChecker = checker; this.checksumLength = checker.checksumLength(); this.maxRetries = origContext.maxSplitfileBlockRetries; this.cooldownTries = origContext.getCooldownRetries();
this.errors = new FailureCodeTracker(false); this.checksumChecker = checker; this.checksumLength = checker.checksumLength(); this.persistent = persistent; this.completeViaTruncation = (storageFile != null);