/* * (non-Javadoc) * * @see java.nio.channels.SeekableByteChannel#read(java.nio.ByteBuffer) */ @Override public int read(ByteBuffer dst) throws IOException { if (dst == null) { throw new NullPointerException("The destination buffer is null."); } if (dst.limit() == 0) { throw new IllegalArgumentException("The destination buffer has zero length."); } if (!_open) return -1; // Make an attempt to read up to r bytes from the channel, where // r is the number of bytes remaining in the buffer, that is, // dst.remaining(), at the moment this method is invoked. long _here = position(); // Build temporary storage. int remain = dst.remaining(); byte[] data = new byte[remain]; int length = _backing.read(data, 0, remain); if (length > 0) { // Iterate thru each byte of temporary storage and decrypt those // bytes back // into the buffer for (int index = 0; index < length; index++) { data[index] = _head.crypt(data[index], index + _here); } // Decrypt all bytes. dst.put(data, 0, length); } return length; }
@Override public void write(byte[] arr, int off, int len) throws IOException { if (_closed) return; // first make copy of relevant part of array // TODO : add bounds checking // JMC: Why not just new byte[len]?? byte[] encodedArr = new byte[len - off]; System.arraycopy(arr, off, encodedArr, 0, len); for (int i = off; i < (off + len); i++) { byte b = arr[i]; // Add to the message digest. _hash.update((byte) b); // Encode. b = _head.crypt((byte) b, _position); _position++; // Add encoded byte to b encodedArr[i] = b; } // Write. _encrypted.write(encodedArr); // System.out.println("SBJHBKJDBH"); }
/** * Read the next byte at the current position, and return it. Return -1 if the end of the file is * read. * * @return The next byte. * @throws IOException The next byte cannot be read. */ public int read() throws IOException { if (!_open) return -1; long _here = position(); int val = _backing.read(); if (val >= 0) { val = _head.crypt((byte) val, _here); } return val; }
/** * Write a byte at the current position. * * @param datum The byte. * @throws IOException The byte cannot be written. */ public void write(int datum) throws IOException { if (!_open) return; datum &= 0xff; long _here = position(); // Are we synced up? if (_here == _digestvalidto) { // Yes, digest the byte and move the valid to pointer. _digest.update((byte) datum); _digestvalidto++; // JMC: Was missing. } // Otherwise, advancing of the position will destroy the digest sync. datum = _head.crypt((byte) datum, _here); _backing.write(datum); }
/* (non-Javadoc) * @see java.io.OutputStream#write(int) */ @Override public void write(int datum) throws IOException { if (_closed) return; // Byteify this. datum &= 0xff; // Add to the message digest. _hash.update((byte) datum); // Encode. datum = _head.crypt((byte) datum, _position); // Write. _encrypted.write(datum); _position += 1; }
/* * (non-Javadoc) * * @see java.nio.channels.WritableByteChannel#write(java.nio.ByteBuffer) * * NOTE: Changes the state of src in the normal case, i.e., position = * limit. */ @Override public int write(ByteBuffer src) throws IOException { if (src == null) { throw new NullPointerException("The source buffer is null."); } // Is there something to actually write in source. if (src.limit() == 0 || src.remaining() == 0) { throw new IllegalArgumentException( "The source buffer has zero length or zero remaining bytes."); } if (!_open) return -1; // based on the specification _hear is p. long _here = position(); // Make an attempt to write up to r bytes to the channel, where // r is the number of bytes remaining in the buffer, that is, // src.remaining(), at the moment this method is invoked. // NOTE: not good to use the src.array() since that is the backing array // which // reflects the entire buffer and not from p to remaining. byte[] encr = new byte[src.remaining()]; // Update the digest to the start of the write. _updateDigest(); // Have we reached source's limit? for (int index = 0; src.hasRemaining(); index++) { // The unencrypted data. byte b = src.get(); _digest.update(b); _digestvalidto++; // encrypt it into the buffer. encr[index] = _head.crypt(b, index + _here); } _backing.write(encr); return encr.length; }