1. /*
  2. * @(#)FileImageOutputStream.java 1.14 03/12/19
  3. *
  4. * Copyright 2004 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>ImageOutputStream</code> that writes its
  15. * output directly to a <code>File</code> or
  16. * <code>RandomAccessFile</code>.
  17. *
  18. * @version 0.5
  19. */
  20. public class FileImageOutputStream extends ImageOutputStreamImpl {
  21. private RandomAccessFile raf;
  22. /**
  23. * Constructs a <code>FileImageOutputStream</code> that will write
  24. * to a given <code>File</code>.
  25. *
  26. * @param f a <code>File</code> to write to.
  27. *
  28. * @exception IllegalArgumentException if <code>f</code> is
  29. * <code>null</code>.
  30. * @exception SecurityException if a security manager exists
  31. * and does not allow write access to the file.
  32. * @exception FileNotFoundException if <code>f</code> does not denote
  33. * a regular file or it cannot be opened for reading and writing for any
  34. * other reason.
  35. * @exception IOException if an I/O error occurs.
  36. */
  37. public FileImageOutputStream(File f)
  38. throws FileNotFoundException, IOException {
  39. this(f == null ? null : new RandomAccessFile(f, "rw"));
  40. }
  41. /**
  42. * Constructs a <code>FileImageOutputStream</code> that will write
  43. * to a given <code>RandomAccessFile</code>.
  44. *
  45. * @param raf a <code>RandomAccessFile</code> to write to.
  46. *
  47. * @exception IllegalArgumentException if <code>raf</code> is
  48. * <code>null</code>.
  49. */
  50. public FileImageOutputStream(RandomAccessFile raf) {
  51. if (raf == null) {
  52. throw new IllegalArgumentException("raf == null!");
  53. }
  54. this.raf = raf;
  55. }
  56. public int read() throws IOException {
  57. checkClosed();
  58. bitOffset = 0;
  59. int val = raf.read();
  60. if (val != -1) {
  61. ++streamPos;
  62. }
  63. return val;
  64. }
  65. public int read(byte[] b, int off, int len) throws IOException {
  66. checkClosed();
  67. bitOffset = 0;
  68. int nbytes = raf.read(b, off, len);
  69. if (nbytes != -1) {
  70. streamPos += nbytes;
  71. }
  72. return nbytes;
  73. }
  74. public void write(int b) throws IOException {
  75. checkClosed();
  76. flushBits();
  77. raf.write(b);
  78. ++streamPos;
  79. }
  80. public void write(byte[] b, int off, int len) throws IOException {
  81. checkClosed();
  82. flushBits();
  83. raf.write(b, off, len);
  84. streamPos += len;
  85. }
  86. public long length() {
  87. try {
  88. checkClosed();
  89. return raf.length();
  90. } catch (IOException e) {
  91. return -1L;
  92. }
  93. }
  94. /**
  95. * Sets the current stream position and resets the bit offset to
  96. * 0. It is legal to seeking past the end of the file; an
  97. * <code>EOFException</code> will be thrown only if a read is
  98. * performed. The file length will not be increased until a write
  99. * is performed.
  100. *
  101. * @exception IndexOutOfBoundsException if <code>pos</code> is smaller
  102. * than the flushed position.
  103. * @exception IOException if any other I/O error occurs.
  104. */
  105. public void seek(long pos) throws IOException {
  106. checkClosed();
  107. if (pos < flushedPos) {
  108. throw new IndexOutOfBoundsException("pos < flushedPos!");
  109. }
  110. bitOffset = 0;
  111. raf.seek(pos);
  112. streamPos = raf.getFilePointer();
  113. }
  114. public void close() throws IOException {
  115. super.close();
  116. raf.close();
  117. }
  118. }