1. /*
  2. * @(#)FileImageInputStream.java 1.20 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.imageio.stream;
  8. import java.io.DataInput;
  9. import java.io.File;
  10. import java.io.FileNotFoundException;
  11. import java.io.IOException;
  12. import java.io.RandomAccessFile;
  13. /**
  14. * An implementation of <code>ImageInputStream</code> that gets its
  15. * input from a <code>File</code> or <code>RandomAccessFile</code>.
  16. * The file contents are assumed to be stable during the lifetime of
  17. * the object.
  18. *
  19. * @version 0.5
  20. */
  21. public class FileImageInputStream extends ImageInputStreamImpl {
  22. private RandomAccessFile raf;
  23. /**
  24. * Constructs a <code>FileImageInputStream</code> that will read
  25. * from a given <code>File</code>.
  26. *
  27. * <p> The file contents must not change between the time this
  28. * object is constructed and the time of the last call to a read
  29. * method.
  30. *
  31. * @param f a <code>File</code> to read from.
  32. *
  33. * @exception IllegalArgumentException if <code>f</code> is
  34. * <code>null</code>.
  35. * @exception SecurityException if a security manager exists
  36. * and does not allow read access to the file.
  37. * @exception FileNotFoundException if <code>f</code> is a
  38. * directory or cannot be opened for reading for any other reason.
  39. * @exception IOException if an I/O error occurs.
  40. */
  41. public FileImageInputStream(File f)
  42. throws FileNotFoundException, IOException {
  43. if (f == null) {
  44. throw new IllegalArgumentException("f == null!");
  45. }
  46. this.raf = new RandomAccessFile(f, "r");
  47. }
  48. /**
  49. * Constructs a <code>FileImageInputStream</code> that will read
  50. * from a given <code>RandomAccessFile</code>.
  51. *
  52. * <p> The file contents must not change between the time this
  53. * object is constructed and the time of the last call to a read
  54. * method.
  55. *
  56. * @param raf a <code>RandomAccessFile</code> to read from.
  57. *
  58. * @exception IllegalArgumentException if <code>raf</code> is
  59. * <code>null</code>.
  60. */
  61. public FileImageInputStream(RandomAccessFile raf) {
  62. if (raf == null) {
  63. throw new IllegalArgumentException("raf == null!");
  64. }
  65. this.raf = raf;
  66. }
  67. public int read() throws IOException {
  68. checkClosed();
  69. bitOffset = 0;
  70. int val = raf.read();
  71. if (val != -1) {
  72. ++streamPos;
  73. }
  74. return val;
  75. }
  76. public int read(byte[] b, int off, int len) throws IOException {
  77. checkClosed();
  78. bitOffset = 0;
  79. int nbytes = raf.read(b, off, len);
  80. if (nbytes != -1) {
  81. streamPos += nbytes;
  82. }
  83. return nbytes;
  84. }
  85. /**
  86. * Returns the length of the underlying file, or <code>-1</code>
  87. * if it is unknown.
  88. *
  89. * @return the file length as a <code>long</code>, or
  90. * <code>-1</code>.
  91. */
  92. public long length() {
  93. try {
  94. checkClosed();
  95. return raf.length();
  96. } catch (IOException e) {
  97. return -1L;
  98. }
  99. }
  100. public void seek(long pos) throws IOException {
  101. checkClosed();
  102. if (pos < flushedPos) {
  103. throw new IndexOutOfBoundsException("pos < flushedPos!");
  104. }
  105. bitOffset = 0;
  106. raf.seek(pos);
  107. streamPos = raf.getFilePointer();
  108. }
  109. public void close() throws IOException {
  110. super.close();
  111. raf.close();
  112. }
  113. }