Filtering Streams
An Introduction with Examples in Java |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
package io; import java.io.*; /** * For stripping off the high bit of a byte * * @version 1.0 * @author Prof. David Bernstein, James Madison University */ public class SevenBitInputStream extends FilterInputStream { /** * Explicit Value Constructor * * @param is The underlying InputStream to filter */ public SevenBitInputStream(InputStream is) { super(is); } /** * Reads the next byte of data from this input stream. * The value byte is returned as an int in the range 0 to 127. * * @return The value of the byte (as an int) */ public int read() throws IOException { int i; i = in.read(); // in is a variable FilterInputStream return (i & 127); // Bitwise AND } /** * Reads up to len bytes of data into an array of bytes. * * @param b The byte array into which the data are read * @param off The start offset * @param len The (maximum) number of bytes read * @return The total number of bytes read */ public int read(byte[] b, int off, int len) throws IOException { int i, n; n = in.read(b, off, len); for (i=0; i < n; i++) b[i] = (byte)(b[i] & 127); return n; } }