13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.imageio.stream; 27 28 import java.io.IOException; 29 import java.io.UTFDataFormatException; 30 import java.nio.ByteOrder; 31 32 /** 33 * An abstract class implementing the <code>ImageOutputStream</code> interface. 34 * This class is designed to reduce the number of methods that must 35 * be implemented by subclasses. 36 * 37 */ 38 public abstract class ImageOutputStreamImpl 39 extends ImageInputStreamImpl 40 implements ImageOutputStream { 41 42 /** 43 * Constructs an <code>ImageOutputStreamImpl</code>. 44 */ 45 public ImageOutputStreamImpl() { 46 } 47 48 public abstract void write(int b) throws IOException; 49 50 public void write(byte b[]) throws IOException { 51 write(b, 0, b.length); 52 } 53 54 public abstract void write(byte b[], int off, int len) throws IOException; 55 56 public void writeBoolean(boolean v) throws IOException { 57 write(v ? 1 : 0); 58 } 59 60 public void writeByte(int v) throws IOException { 61 write(v); 62 } 63 464 // of the current byte with 0s 465 else { // EOF 466 partialByte = 0; 467 } 468 469 int shift = 8 - numBits; 470 int mask = -1 >>> (32 - numBits); 471 partialByte &= ~(mask << shift); 472 partialByte |= (bits & mask) << shift; 473 // bitOffset is always already 0 when we get here. 474 write(partialByte); 475 seek(getStreamPosition() - 1); 476 bitOffset = numBits; 477 } 478 } 479 480 /** 481 * If the bit offset is non-zero, forces the remaining bits 482 * in the current byte to 0 and advances the stream position 483 * by one. This method should be called by subclasses at the 484 * beginning of the <code>write(int)</code> and 485 * <code>write(byte[], int, int)</code> methods. 486 * 487 * @exception IOException if an I/O error occurs. 488 */ 489 protected final void flushBits() throws IOException { 490 checkClosed(); 491 if (bitOffset != 0) { 492 int offset = bitOffset; 493 int partialByte = read(); // Sets bitOffset to 0 494 if (partialByte < 0) { 495 // Fix 4465683: When bitOffset is set 496 // to something non-zero beyond EOF, 497 // we should set that whole byte to 498 // zero and write it to stream. 499 partialByte = 0; 500 bitOffset = 0; 501 } 502 else { 503 seek(getStreamPosition() - 1); 504 partialByte &= -1 << (8 - offset); 505 } | 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javax.imageio.stream; 27 28 import java.io.IOException; 29 import java.io.UTFDataFormatException; 30 import java.nio.ByteOrder; 31 32 /** 33 * An abstract class implementing the {@code ImageOutputStream} interface. 34 * This class is designed to reduce the number of methods that must 35 * be implemented by subclasses. 36 * 37 */ 38 public abstract class ImageOutputStreamImpl 39 extends ImageInputStreamImpl 40 implements ImageOutputStream { 41 42 /** 43 * Constructs an {@code ImageOutputStreamImpl}. 44 */ 45 public ImageOutputStreamImpl() { 46 } 47 48 public abstract void write(int b) throws IOException; 49 50 public void write(byte b[]) throws IOException { 51 write(b, 0, b.length); 52 } 53 54 public abstract void write(byte b[], int off, int len) throws IOException; 55 56 public void writeBoolean(boolean v) throws IOException { 57 write(v ? 1 : 0); 58 } 59 60 public void writeByte(int v) throws IOException { 61 write(v); 62 } 63 464 // of the current byte with 0s 465 else { // EOF 466 partialByte = 0; 467 } 468 469 int shift = 8 - numBits; 470 int mask = -1 >>> (32 - numBits); 471 partialByte &= ~(mask << shift); 472 partialByte |= (bits & mask) << shift; 473 // bitOffset is always already 0 when we get here. 474 write(partialByte); 475 seek(getStreamPosition() - 1); 476 bitOffset = numBits; 477 } 478 } 479 480 /** 481 * If the bit offset is non-zero, forces the remaining bits 482 * in the current byte to 0 and advances the stream position 483 * by one. This method should be called by subclasses at the 484 * beginning of the {@code write(int)} and 485 * {@code write(byte[], int, int)} methods. 486 * 487 * @exception IOException if an I/O error occurs. 488 */ 489 protected final void flushBits() throws IOException { 490 checkClosed(); 491 if (bitOffset != 0) { 492 int offset = bitOffset; 493 int partialByte = read(); // Sets bitOffset to 0 494 if (partialByte < 0) { 495 // Fix 4465683: When bitOffset is set 496 // to something non-zero beyond EOF, 497 // we should set that whole byte to 498 // zero and write it to stream. 499 partialByte = 0; 500 bitOffset = 0; 501 } 502 else { 503 seek(getStreamPosition() - 1); 504 partialByte &= -1 << (8 - offset); 505 } |