BufferedReader.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /* BufferedReader.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.xml.stream;
  32. import java.io.IOException;
  33. import java.io.Reader;
  34. /**
  35. * A mark-capable buffered reader.
  36. *
  37. * @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
  38. */
  39. class BufferedReader
  40. extends Reader
  41. {
  42. static final int DEFAULT_BUFFER_SIZE = 4096;
  43. final Reader in;
  44. char[] buf;
  45. int pos, count, markpos, marklimit, bufferSize;
  46. BufferedReader(Reader in)
  47. {
  48. this(in, DEFAULT_BUFFER_SIZE);
  49. }
  50. BufferedReader(Reader in, int bufferSize)
  51. {
  52. if (bufferSize < 1)
  53. throw new IllegalArgumentException();
  54. this.in = in;
  55. this.bufferSize = bufferSize;
  56. buf = new char[bufferSize];
  57. pos = count = bufferSize;
  58. }
  59. public void close()
  60. throws IOException
  61. {
  62. buf = null;
  63. pos = count = 0;
  64. markpos = -1;
  65. in.close();
  66. }
  67. public void mark(int readlimit)
  68. throws IOException
  69. {
  70. marklimit = readlimit;
  71. markpos = pos;
  72. }
  73. public boolean markSupported()
  74. {
  75. return true;
  76. }
  77. public int read()
  78. throws IOException
  79. {
  80. if (pos >= count && !refill())
  81. return -1;
  82. return (int) buf[pos++];
  83. }
  84. public int read(char[] b)
  85. throws IOException
  86. {
  87. return read(b, 0, b.length);
  88. }
  89. public int read(char[] b, int off, int len)
  90. throws IOException
  91. {
  92. if (off < 0 || len < 0 || b.length - off < len)
  93. throw new IndexOutOfBoundsException();
  94. if (len == 0)
  95. return 0;
  96. if (pos >= count && !refill())
  97. return -1;
  98. int ret = Math.min(count - pos, len);
  99. System.arraycopy(buf, pos, b, off, ret);
  100. pos += ret;
  101. off += ret;
  102. len -= ret;
  103. while (len > 0 && refill())
  104. {
  105. int remain = Math.min(count - pos, len);
  106. System.arraycopy(buf, pos, b, off, remain);
  107. pos += remain;
  108. off += remain;
  109. len -= remain;
  110. ret += remain;
  111. }
  112. return ret;
  113. }
  114. public void reset()
  115. throws IOException
  116. {
  117. if (markpos == -1)
  118. throw new IOException(buf == null ? "Stream closed." : "Invalid mark.");
  119. pos = markpos;
  120. }
  121. public long skip(long n)
  122. throws IOException
  123. {
  124. if (buf == null)
  125. throw new IOException("Stream closed.");
  126. final long origN = n;
  127. while (n > 0L)
  128. {
  129. if (pos >= count && !refill())
  130. break;
  131. int numread = (int) Math.min((long) (count - pos), n);
  132. pos += numread;
  133. n -= numread;
  134. }
  135. return origN - n;
  136. }
  137. private boolean refill()
  138. throws IOException
  139. {
  140. if (buf == null)
  141. throw new IOException("Stream closed.");
  142. int markcount = count - markpos;
  143. if (markpos == -1 || markcount >= marklimit)
  144. {
  145. markpos = -1;
  146. pos = count = 0;
  147. }
  148. else
  149. {
  150. char[] newbuf = buf;
  151. if (markpos < bufferSize)
  152. {
  153. newbuf = new char[count - markpos + bufferSize];
  154. }
  155. System.arraycopy(buf, markpos, newbuf, 0, markcount);
  156. buf = newbuf;
  157. count = markcount;
  158. pos -= markpos;
  159. markpos = 0;
  160. }
  161. int numread = in.read(buf, count, bufferSize);
  162. if (numread <= 0)
  163. return false;
  164. count += numread;
  165. return true;
  166. }
  167. }