ZipFile.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /* ZipFile.java --
  2. Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006
  3. Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package java.util.zip;
  33. import gnu.java.util.EmptyEnumeration;
  34. import java.io.EOFException;
  35. import java.io.File;
  36. import java.io.FileNotFoundException;
  37. import java.io.IOException;
  38. import java.io.InputStream;
  39. import java.io.RandomAccessFile;
  40. import java.io.UnsupportedEncodingException;
  41. import java.nio.ByteBuffer;
  42. import java.nio.charset.Charset;
  43. import java.nio.charset.CharsetDecoder;
  44. import java.util.Enumeration;
  45. import java.util.Iterator;
  46. import java.util.LinkedHashMap;
  47. /**
  48. * This class represents a Zip archive. You can ask for the contained
  49. * entries, or get an input stream for a file entry. The entry is
  50. * automatically decompressed.
  51. *
  52. * This class is thread safe: You can open input streams for arbitrary
  53. * entries in different threads.
  54. *
  55. * @author Jochen Hoenicke
  56. * @author Artur Biesiadowski
  57. */
  58. public class ZipFile implements ZipConstants
  59. {
  60. /**
  61. * Mode flag to open a zip file for reading.
  62. */
  63. public static final int OPEN_READ = 0x1;
  64. /**
  65. * Mode flag to delete a zip file after reading.
  66. */
  67. public static final int OPEN_DELETE = 0x4;
  68. /**
  69. * This field isn't defined in the JDK's ZipConstants, but should be.
  70. */
  71. static final int ENDNRD = 4;
  72. // Name of this zip file.
  73. private final String name;
  74. // File from which zip entries are read.
  75. private final RandomAccessFile raf;
  76. // The entries of this zip file when initialized and not yet closed.
  77. private LinkedHashMap<String, ZipEntry> entries;
  78. private boolean closed = false;
  79. /**
  80. * Helper function to open RandomAccessFile and throw the proper
  81. * ZipException in case opening the file fails.
  82. *
  83. * @param name the file name, or null if file is provided
  84. *
  85. * @param file the file, or null if name is provided
  86. *
  87. * @return the newly open RandomAccessFile, never null
  88. */
  89. private RandomAccessFile openFile(String name,
  90. File file)
  91. throws ZipException, IOException
  92. {
  93. try
  94. {
  95. return
  96. (name != null)
  97. ? new RandomAccessFile(name, "r")
  98. : new RandomAccessFile(file, "r");
  99. }
  100. catch (FileNotFoundException f)
  101. {
  102. ZipException ze = new ZipException(f.getMessage());
  103. ze.initCause(f);
  104. throw ze;
  105. }
  106. }
  107. /**
  108. * Opens a Zip file with the given name for reading.
  109. * @exception IOException if a i/o error occured.
  110. * @exception ZipException if the file doesn't contain a valid zip
  111. * archive.
  112. */
  113. public ZipFile(String name) throws ZipException, IOException
  114. {
  115. this.raf = openFile(name,null);
  116. this.name = name;
  117. checkZipFile();
  118. }
  119. /**
  120. * Opens a Zip file reading the given File.
  121. * @exception IOException if a i/o error occured.
  122. * @exception ZipException if the file doesn't contain a valid zip
  123. * archive.
  124. */
  125. public ZipFile(File file) throws ZipException, IOException
  126. {
  127. this.raf = openFile(null,file);
  128. this.name = file.getPath();
  129. checkZipFile();
  130. }
  131. /**
  132. * Opens a Zip file reading the given File in the given mode.
  133. *
  134. * If the OPEN_DELETE mode is specified, the zip file will be deleted at
  135. * some time moment after it is opened. It will be deleted before the zip
  136. * file is closed or the Virtual Machine exits.
  137. *
  138. * The contents of the zip file will be accessible until it is closed.
  139. *
  140. * @since JDK1.3
  141. * @param mode Must be one of OPEN_READ or OPEN_READ | OPEN_DELETE
  142. *
  143. * @exception IOException if a i/o error occured.
  144. * @exception ZipException if the file doesn't contain a valid zip
  145. * archive.
  146. */
  147. public ZipFile(File file, int mode) throws ZipException, IOException
  148. {
  149. if (mode != OPEN_READ && mode != (OPEN_READ | OPEN_DELETE))
  150. throw new IllegalArgumentException("invalid mode");
  151. if ((mode & OPEN_DELETE) != 0)
  152. file.deleteOnExit();
  153. this.raf = openFile(null,file);
  154. this.name = file.getPath();
  155. checkZipFile();
  156. }
  157. private void checkZipFile() throws ZipException
  158. {
  159. boolean valid = false;
  160. try
  161. {
  162. byte[] buf = new byte[4];
  163. raf.readFully(buf);
  164. int sig = buf[0] & 0xFF
  165. | ((buf[1] & 0xFF) << 8)
  166. | ((buf[2] & 0xFF) << 16)
  167. | ((buf[3] & 0xFF) << 24);
  168. valid = sig == LOCSIG;
  169. }
  170. catch (IOException _)
  171. {
  172. }
  173. if (!valid)
  174. {
  175. try
  176. {
  177. raf.close();
  178. }
  179. catch (IOException _)
  180. {
  181. }
  182. throw new ZipException("Not a valid zip file");
  183. }
  184. }
  185. /**
  186. * Checks if file is closed and throws an exception.
  187. */
  188. private void checkClosed()
  189. {
  190. if (closed)
  191. throw new IllegalStateException("ZipFile has closed: " + name);
  192. }
  193. /**
  194. * Read the central directory of a zip file and fill the entries
  195. * array. This is called exactly once when first needed. It is called
  196. * while holding the lock on <code>raf</code>.
  197. *
  198. * @exception IOException if a i/o error occured.
  199. * @exception ZipException if the central directory is malformed
  200. */
  201. private void readEntries() throws ZipException, IOException
  202. {
  203. /* Search for the End Of Central Directory. When a zip comment is
  204. * present the directory may start earlier.
  205. * Note that a comment has a maximum length of 64K, so that is the
  206. * maximum we search backwards.
  207. */
  208. PartialInputStream inp = new PartialInputStream(raf, 4096);
  209. long pos = raf.length() - ENDHDR;
  210. long top = Math.max(0, pos - 65536);
  211. do
  212. {
  213. if (pos < top)
  214. throw new ZipException
  215. ("central directory not found, probably not a zip file: " + name);
  216. inp.seek(pos--);
  217. }
  218. while (inp.readLeInt() != ENDSIG);
  219. if (inp.skip(ENDTOT - ENDNRD) != ENDTOT - ENDNRD)
  220. throw new EOFException(name);
  221. int count = inp.readLeShort();
  222. if (inp.skip(ENDOFF - ENDSIZ) != ENDOFF - ENDSIZ)
  223. throw new EOFException(name);
  224. int centralOffset = inp.readLeInt();
  225. entries = new LinkedHashMap<String, ZipEntry> (count+count/2);
  226. inp.seek(centralOffset);
  227. for (int i = 0; i < count; i++)
  228. {
  229. if (inp.readLeInt() != CENSIG)
  230. throw new ZipException("Wrong Central Directory signature: " + name);
  231. inp.skip(4);
  232. int flags = inp.readLeShort();
  233. if ((flags & 1) != 0)
  234. throw new ZipException("invalid CEN header (encrypted entry)");
  235. int method = inp.readLeShort();
  236. int dostime = inp.readLeInt();
  237. int crc = inp.readLeInt();
  238. int csize = inp.readLeInt();
  239. int size = inp.readLeInt();
  240. int nameLen = inp.readLeShort();
  241. int extraLen = inp.readLeShort();
  242. int commentLen = inp.readLeShort();
  243. inp.skip(8);
  244. int offset = inp.readLeInt();
  245. String name = inp.readString(nameLen);
  246. ZipEntry entry = new ZipEntry(name);
  247. entry.setMethod(method);
  248. entry.setCrc(crc & 0xffffffffL);
  249. entry.setSize(size & 0xffffffffL);
  250. entry.setCompressedSize(csize & 0xffffffffL);
  251. entry.setDOSTime(dostime);
  252. if (extraLen > 0)
  253. {
  254. byte[] extra = new byte[extraLen];
  255. inp.readFully(extra);
  256. entry.setExtra(extra);
  257. }
  258. if (commentLen > 0)
  259. {
  260. entry.setComment(inp.readString(commentLen));
  261. }
  262. entry.offset = offset;
  263. entries.put(name, entry);
  264. }
  265. }
  266. /**
  267. * Closes the ZipFile. This also closes all input streams given by
  268. * this class. After this is called, no further method should be
  269. * called.
  270. *
  271. * @exception IOException if a i/o error occured.
  272. */
  273. public void close() throws IOException
  274. {
  275. RandomAccessFile raf = this.raf;
  276. if (raf == null)
  277. return;
  278. synchronized (raf)
  279. {
  280. closed = true;
  281. entries = null;
  282. raf.close();
  283. }
  284. }
  285. /**
  286. * Calls the <code>close()</code> method when this ZipFile has not yet
  287. * been explicitly closed.
  288. */
  289. protected void finalize() throws IOException
  290. {
  291. if (!closed && raf != null) close();
  292. }
  293. /**
  294. * Returns an enumeration of all Zip entries in this Zip file.
  295. *
  296. * @exception IllegalStateException when the ZipFile has already been closed
  297. */
  298. public Enumeration<? extends ZipEntry> entries()
  299. {
  300. checkClosed();
  301. try
  302. {
  303. return new ZipEntryEnumeration(getEntries().values().iterator());
  304. }
  305. catch (IOException ioe)
  306. {
  307. return new EmptyEnumeration<ZipEntry>();
  308. }
  309. }
  310. /**
  311. * Checks that the ZipFile is still open and reads entries when necessary.
  312. *
  313. * @exception IllegalStateException when the ZipFile has already been closed.
  314. * @exception IOException when the entries could not be read.
  315. */
  316. private LinkedHashMap<String, ZipEntry> getEntries() throws IOException
  317. {
  318. synchronized(raf)
  319. {
  320. checkClosed();
  321. if (entries == null)
  322. readEntries();
  323. return entries;
  324. }
  325. }
  326. /**
  327. * Searches for a zip entry in this archive with the given name.
  328. *
  329. * @param name the name. May contain directory components separated by
  330. * slashes ('/').
  331. * @return the zip entry, or null if no entry with that name exists.
  332. *
  333. * @exception IllegalStateException when the ZipFile has already been closed
  334. */
  335. public ZipEntry getEntry(String name)
  336. {
  337. checkClosed();
  338. try
  339. {
  340. LinkedHashMap<String, ZipEntry> entries = getEntries();
  341. ZipEntry entry = entries.get(name);
  342. // If we didn't find it, maybe it's a directory.
  343. if (entry == null && !name.endsWith("/"))
  344. entry = entries.get(name + '/');
  345. return entry != null ? new ZipEntry(entry, name) : null;
  346. }
  347. catch (IOException ioe)
  348. {
  349. return null;
  350. }
  351. }
  352. /**
  353. * Creates an input stream reading the given zip entry as
  354. * uncompressed data. Normally zip entry should be an entry
  355. * returned by getEntry() or entries().
  356. *
  357. * This implementation returns null if the requested entry does not
  358. * exist. This decision is not obviously correct, however, it does
  359. * appear to mirror Sun's implementation, and it is consistant with
  360. * their javadoc. On the other hand, the old JCL book, 2nd Edition,
  361. * claims that this should return a "non-null ZIP entry". We have
  362. * chosen for now ignore the old book, as modern versions of Ant (an
  363. * important application) depend on this behaviour. See discussion
  364. * in this thread:
  365. * http://gcc.gnu.org/ml/java-patches/2004-q2/msg00602.html
  366. *
  367. * @param entry the entry to create an InputStream for.
  368. * @return the input stream, or null if the requested entry does not exist.
  369. *
  370. * @exception IllegalStateException when the ZipFile has already been closed
  371. * @exception IOException if a i/o error occured.
  372. * @exception ZipException if the Zip archive is malformed.
  373. */
  374. public InputStream getInputStream(ZipEntry entry) throws IOException
  375. {
  376. checkClosed();
  377. LinkedHashMap<String, ZipEntry> entries = getEntries();
  378. String name = entry.getName();
  379. ZipEntry zipEntry = entries.get(name);
  380. if (zipEntry == null)
  381. return null;
  382. PartialInputStream inp = new PartialInputStream(raf, 1024);
  383. inp.seek(zipEntry.offset);
  384. if (inp.readLeInt() != LOCSIG)
  385. throw new ZipException("Wrong Local header signature: " + name);
  386. inp.skip(4);
  387. if (zipEntry.getMethod() != inp.readLeShort())
  388. throw new ZipException("Compression method mismatch: " + name);
  389. inp.skip(16);
  390. int nameLen = inp.readLeShort();
  391. int extraLen = inp.readLeShort();
  392. inp.skip(nameLen + extraLen);
  393. inp.setLength(zipEntry.getCompressedSize());
  394. int method = zipEntry.getMethod();
  395. switch (method)
  396. {
  397. case ZipOutputStream.STORED:
  398. return inp;
  399. case ZipOutputStream.DEFLATED:
  400. inp.addDummyByte();
  401. final Inflater inf = new Inflater(true);
  402. final int sz = (int) entry.getSize();
  403. return new InflaterInputStream(inp, inf)
  404. {
  405. public int available() throws IOException
  406. {
  407. if (sz == -1)
  408. return super.available();
  409. if (super.available() != 0)
  410. return sz - inf.getTotalOut();
  411. return 0;
  412. }
  413. };
  414. default:
  415. throw new ZipException("Unknown compression method " + method);
  416. }
  417. }
  418. /**
  419. * Returns the (path) name of this zip file.
  420. */
  421. public String getName()
  422. {
  423. return name;
  424. }
  425. /**
  426. * Returns the number of entries in this zip file.
  427. *
  428. * @exception IllegalStateException when the ZipFile has already been closed
  429. */
  430. public int size()
  431. {
  432. checkClosed();
  433. try
  434. {
  435. return getEntries().size();
  436. }
  437. catch (IOException ioe)
  438. {
  439. return 0;
  440. }
  441. }
  442. private static class ZipEntryEnumeration implements Enumeration<ZipEntry>
  443. {
  444. private final Iterator<ZipEntry> elements;
  445. public ZipEntryEnumeration(Iterator<ZipEntry> elements)
  446. {
  447. this.elements = elements;
  448. }
  449. public boolean hasMoreElements()
  450. {
  451. return elements.hasNext();
  452. }
  453. public ZipEntry nextElement()
  454. {
  455. /* We return a clone, just to be safe that the user doesn't
  456. * change the entry.
  457. */
  458. return (ZipEntry) (elements.next().clone());
  459. }
  460. }
  461. private static final class PartialInputStream extends InputStream
  462. {
  463. /**
  464. * The UTF-8 charset use for decoding the filenames.
  465. */
  466. private static final Charset UTF8CHARSET = Charset.forName("UTF-8");
  467. /**
  468. * The actual UTF-8 decoder. Created on demand.
  469. */
  470. private CharsetDecoder utf8Decoder;
  471. private final RandomAccessFile raf;
  472. private final byte[] buffer;
  473. private long bufferOffset;
  474. private int pos;
  475. private long end;
  476. // We may need to supply an extra dummy byte to our reader.
  477. // See Inflater. We use a count here to simplify the logic
  478. // elsewhere in this class. Note that we ignore the dummy
  479. // byte in methods where we know it is not needed.
  480. private int dummyByteCount;
  481. public PartialInputStream(RandomAccessFile raf, int bufferSize)
  482. throws IOException
  483. {
  484. this.raf = raf;
  485. buffer = new byte[bufferSize];
  486. bufferOffset = -buffer.length;
  487. pos = buffer.length;
  488. end = raf.length();
  489. }
  490. void setLength(long length)
  491. {
  492. end = bufferOffset + pos + length;
  493. }
  494. private void fillBuffer() throws IOException
  495. {
  496. synchronized (raf)
  497. {
  498. long len = end - bufferOffset;
  499. if (len == 0 && dummyByteCount > 0)
  500. {
  501. buffer[0] = 0;
  502. dummyByteCount = 0;
  503. }
  504. else
  505. {
  506. raf.seek(bufferOffset);
  507. raf.readFully(buffer, 0, (int) Math.min(buffer.length, len));
  508. }
  509. }
  510. }
  511. public int available()
  512. {
  513. long amount = end - (bufferOffset + pos);
  514. if (amount > Integer.MAX_VALUE)
  515. return Integer.MAX_VALUE;
  516. return (int) amount;
  517. }
  518. public int read() throws IOException
  519. {
  520. if (bufferOffset + pos >= end + dummyByteCount)
  521. return -1;
  522. if (pos == buffer.length)
  523. {
  524. bufferOffset += buffer.length;
  525. pos = 0;
  526. fillBuffer();
  527. }
  528. return buffer[pos++] & 0xFF;
  529. }
  530. public int read(byte[] b, int off, int len) throws IOException
  531. {
  532. if (len > end + dummyByteCount - (bufferOffset + pos))
  533. {
  534. len = (int) (end + dummyByteCount - (bufferOffset + pos));
  535. if (len == 0)
  536. return -1;
  537. }
  538. int totalBytesRead = Math.min(buffer.length - pos, len);
  539. System.arraycopy(buffer, pos, b, off, totalBytesRead);
  540. pos += totalBytesRead;
  541. off += totalBytesRead;
  542. len -= totalBytesRead;
  543. while (len > 0)
  544. {
  545. bufferOffset += buffer.length;
  546. pos = 0;
  547. fillBuffer();
  548. int remain = Math.min(buffer.length, len);
  549. System.arraycopy(buffer, pos, b, off, remain);
  550. pos += remain;
  551. off += remain;
  552. len -= remain;
  553. totalBytesRead += remain;
  554. }
  555. return totalBytesRead;
  556. }
  557. public long skip(long amount) throws IOException
  558. {
  559. if (amount < 0)
  560. return 0;
  561. if (amount > end - (bufferOffset + pos))
  562. amount = end - (bufferOffset + pos);
  563. seek(bufferOffset + pos + amount);
  564. return amount;
  565. }
  566. void seek(long newpos) throws IOException
  567. {
  568. long offset = newpos - bufferOffset;
  569. if (offset >= 0 && offset <= buffer.length)
  570. {
  571. pos = (int) offset;
  572. }
  573. else
  574. {
  575. bufferOffset = newpos;
  576. pos = 0;
  577. fillBuffer();
  578. }
  579. }
  580. void readFully(byte[] buf) throws IOException
  581. {
  582. if (read(buf, 0, buf.length) != buf.length)
  583. throw new EOFException();
  584. }
  585. void readFully(byte[] buf, int off, int len) throws IOException
  586. {
  587. if (read(buf, off, len) != len)
  588. throw new EOFException();
  589. }
  590. int readLeShort() throws IOException
  591. {
  592. int result;
  593. if(pos + 1 < buffer.length)
  594. {
  595. result = ((buffer[pos + 0] & 0xff) | (buffer[pos + 1] & 0xff) << 8);
  596. pos += 2;
  597. }
  598. else
  599. {
  600. int b0 = read();
  601. int b1 = read();
  602. if (b1 == -1)
  603. throw new EOFException();
  604. result = (b0 & 0xff) | (b1 & 0xff) << 8;
  605. }
  606. return result;
  607. }
  608. int readLeInt() throws IOException
  609. {
  610. int result;
  611. if(pos + 3 < buffer.length)
  612. {
  613. result = (((buffer[pos + 0] & 0xff) | (buffer[pos + 1] & 0xff) << 8)
  614. | ((buffer[pos + 2] & 0xff)
  615. | (buffer[pos + 3] & 0xff) << 8) << 16);
  616. pos += 4;
  617. }
  618. else
  619. {
  620. int b0 = read();
  621. int b1 = read();
  622. int b2 = read();
  623. int b3 = read();
  624. if (b3 == -1)
  625. throw new EOFException();
  626. result = (((b0 & 0xff) | (b1 & 0xff) << 8) | ((b2 & 0xff)
  627. | (b3 & 0xff) << 8) << 16);
  628. }
  629. return result;
  630. }
  631. /**
  632. * Decode chars from byte buffer using UTF8 encoding. This
  633. * operation is performance-critical since a jar file contains a
  634. * large number of strings for the name of each file in the
  635. * archive. This routine therefore avoids using the expensive
  636. * utf8Decoder when decoding is straightforward.
  637. *
  638. * @param buffer the buffer that contains the encoded character
  639. * data
  640. * @param pos the index in buffer of the first byte of the encoded
  641. * data
  642. * @param length the length of the encoded data in number of
  643. * bytes.
  644. *
  645. * @return a String that contains the decoded characters.
  646. */
  647. private String decodeChars(byte[] buffer, int pos, int length)
  648. throws IOException
  649. {
  650. String result;
  651. int i=length - 1;
  652. while ((i >= 0) && (buffer[i] <= 0x7f))
  653. {
  654. i--;
  655. }
  656. if (i < 0)
  657. {
  658. result = new String(buffer, 0, pos, length);
  659. }
  660. else
  661. {
  662. ByteBuffer bufferBuffer = ByteBuffer.wrap(buffer, pos, length);
  663. if (utf8Decoder == null)
  664. utf8Decoder = UTF8CHARSET.newDecoder();
  665. utf8Decoder.reset();
  666. char [] characters = utf8Decoder.decode(bufferBuffer).array();
  667. result = String.valueOf(characters);
  668. }
  669. return result;
  670. }
  671. String readString(int length) throws IOException
  672. {
  673. if (length > end - (bufferOffset + pos))
  674. throw new EOFException();
  675. String result = null;
  676. try
  677. {
  678. if (buffer.length - pos >= length)
  679. {
  680. result = decodeChars(buffer, pos, length);
  681. pos += length;
  682. }
  683. else
  684. {
  685. byte[] b = new byte[length];
  686. readFully(b);
  687. result = decodeChars(b, 0, length);
  688. }
  689. }
  690. catch (UnsupportedEncodingException uee)
  691. {
  692. throw new AssertionError(uee);
  693. }
  694. return result;
  695. }
  696. public void addDummyByte()
  697. {
  698. dummyByteCount = 1;
  699. }
  700. }
  701. }