001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Rustem V. Rafikov
019: * @version $Revision: 1.3 $
020: */package javax.imageio.stream;
021:
022: import java.io.*;
023:
024: public class FileImageOutputStream extends ImageOutputStreamImpl {
025:
026: RandomAccessFile file;
027:
028: public FileImageOutputStream(File f) throws FileNotFoundException,
029: IOException {
030: this (f != null ? new RandomAccessFile(f, "rw") : null);
031: }
032:
033: public FileImageOutputStream(RandomAccessFile raf) {
034: if (raf == null) {
035: throw new IllegalArgumentException(
036: "file should not be NULL");
037: }
038: file = raf;
039: }
040:
041: @Override
042: public void write(int b) throws IOException {
043: checkClosed();
044: // according to the spec for ImageOutputStreamImpl#flushBits()
045: flushBits();
046: file.write(b);
047: streamPos++;
048: }
049:
050: @Override
051: public void write(byte[] b, int off, int len) throws IOException {
052: checkClosed();
053: // according to the spec for ImageOutputStreamImpl#flushBits()
054: flushBits();
055: file.write(b, off, len);
056: streamPos += len;
057: }
058:
059: @Override
060: public int read() throws IOException {
061: checkClosed();
062: int rt = file.read();
063: if (rt != -1) {
064: streamPos++;
065: }
066: return rt;
067: }
068:
069: @Override
070: public int read(byte[] b, int off, int len) throws IOException {
071: checkClosed();
072: int rt = file.read(b, off, len);
073: if (rt != -1) {
074: streamPos += rt;
075: }
076: return rt;
077: }
078:
079: @Override
080: public long length() {
081: try {
082: checkClosed();
083: return file.length();
084: } catch (IOException e) {
085: return super .length(); // -1L
086: }
087: }
088:
089: @Override
090: public void seek(long pos) throws IOException {
091: //-- checkClosed() is performed in super.seek()
092: super .seek(pos);
093: file.seek(pos);
094: streamPos = file.getFilePointer();
095: }
096:
097: @Override
098: public void close() throws IOException {
099: super.close();
100: file.close();
101: }
102: }
|