The Android Open Source Project | 7c1b96a | 2008-10-21 07:00:00 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | /** |
| 18 | * @author Rustem V. Rafikov |
| 19 | * @version $Revision: 1.3 $ |
| 20 | */ |
| 21 | package javax.imageio.stream; |
| 22 | |
| 23 | import java.io.*; |
| 24 | |
| 25 | /** |
| 26 | * The FileImageOutputStream class implements ImageOutputStream |
| 27 | * and writes the output data to a File or RandomAccessFile. |
| 28 | */ |
| 29 | public class FileImageOutputStream extends ImageOutputStreamImpl { |
| 30 | |
| 31 | /** The file. */ |
| 32 | RandomAccessFile file; |
| 33 | |
| 34 | /** |
| 35 | * Instantiates a new FileImageOutputStream with the specified |
| 36 | * File. |
| 37 | * |
| 38 | * @param f the output File. |
| 39 | * |
| 40 | * @throws FileNotFoundException if the file not found. |
| 41 | * @throws IOException Signals that an I/O exception has occurred. |
| 42 | */ |
| 43 | public FileImageOutputStream(File f) throws FileNotFoundException, IOException { |
| 44 | this(f != null |
| 45 | ? new RandomAccessFile(f, "rw") |
| 46 | : null); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Instantiates a new FileImageOutputStream with the specified |
| 51 | * RandomAccessFile. |
| 52 | * |
| 53 | * @param raf the output RandomAccessFile. |
| 54 | */ |
| 55 | public FileImageOutputStream(RandomAccessFile raf) { |
| 56 | if (raf == null) { |
| 57 | throw new IllegalArgumentException("file should not be NULL"); |
| 58 | } |
| 59 | file = raf; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void write(int b) throws IOException { |
| 64 | checkClosed(); |
| 65 | // according to the spec for ImageOutputStreamImpl#flushBits() |
| 66 | flushBits(); |
| 67 | file.write(b); |
| 68 | streamPos++; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public void write(byte[] b, int off, int len) throws IOException { |
| 73 | checkClosed(); |
| 74 | // according to the spec for ImageOutputStreamImpl#flushBits() |
| 75 | flushBits(); |
| 76 | file.write(b, off, len); |
| 77 | streamPos += len; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int read() throws IOException { |
| 82 | checkClosed(); |
| 83 | int rt = file.read(); |
| 84 | if (rt != -1) { |
| 85 | streamPos++; |
| 86 | } |
| 87 | return rt; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public int read(byte[] b, int off, int len) throws IOException { |
| 92 | checkClosed(); |
| 93 | int rt = file.read(b, off, len); |
| 94 | if (rt != -1) { |
| 95 | streamPos += rt; |
| 96 | } |
| 97 | return rt; |
| 98 | } |
| 99 | |
| 100 | @Override |
| 101 | public long length() { |
| 102 | try { |
| 103 | checkClosed(); |
| 104 | return file.length(); |
| 105 | } catch(IOException e) { |
| 106 | return super.length(); // -1L |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void seek(long pos) throws IOException { |
| 112 | //-- checkClosed() is performed in super.seek() |
| 113 | super.seek(pos); |
| 114 | file.seek(pos); |
| 115 | streamPos = file.getFilePointer(); |
| 116 | } |
| 117 | |
| 118 | @Override |
| 119 | public void close() throws IOException { |
| 120 | super.close(); |
| 121 | file.close(); |
| 122 | } |
| 123 | } |