1 19 20 package org.netbeans.spi.xml.cookies; 21 22 import org.xml.sax.InputSource ; 23 import java.io.*; 24 25 30 final class ShareableInputSource extends InputSource { 31 32 private ByteStream stream; 33 private CharacterStream reader; 34 private boolean initialized[] = new boolean[2]; 35 36 private final InputSource peer; 37 private final int bufferSize; 38 private final static int BUFFER_SIZE = 1024 * 1024 + 7; 40 private IOException resetException; 41 42 public static ShareableInputSource create(InputSource peer) { 43 if (peer == null) throw new NullPointerException (); 44 if (peer instanceof ShareableInputSource) { 45 return (ShareableInputSource) peer; 46 } else { 47 return new ShareableInputSource(peer, BUFFER_SIZE); 48 } 49 } 50 51 private ShareableInputSource(InputSource peer, int bufferSize) { 52 this.peer = peer; 53 this.bufferSize = bufferSize; 54 } 55 56 public InputStream getByteStream() { 57 InputStream in = peer.getByteStream(); 58 if (initialized[1] == false && in != null) { 59 stream = new ByteStream(in , bufferSize); 60 stream.mark(bufferSize); 61 initialized[1] = true; 62 } 63 return stream; 64 } 65 66 public Reader getCharacterStream() { 67 Reader in = peer.getCharacterStream(); 68 if (initialized[0] == false && in != null) { 69 reader = new CharacterStream(in, bufferSize/2); 70 initialized[0] = true; 71 try { 72 reader.mark(bufferSize/2); 73 } catch (IOException ex) { 74 resetException = ex; 75 } 76 } 77 return reader; 78 } 79 80 83 public void reset() throws IOException { 84 if (resetException != null) throw resetException; 85 if (initialized[1]) stream.reset(); 86 if (initialized[0]) reader.reset(); 87 } 88 89 92 public void closeAll() throws IOException { 93 if (initialized[1]) stream.internalClose(); 94 if (initialized[0]) reader.internalClose(); 95 } 96 97 public String getEncoding() { 98 return peer.getEncoding(); 99 } 100 101 public String getSystemId() { 102 return peer.getSystemId(); 103 } 104 105 public String getPublicId() { 106 return peer.getPublicId(); 107 } 108 109 private static class ByteStream extends BufferedInputStream { 110 public ByteStream(InputStream peer, int buffer) { 111 super(peer, buffer); 112 } 113 114 public void close() throws IOException { 115 } 117 118 private void internalClose() throws IOException { 119 super.close(); 120 } 121 } 122 123 private static class CharacterStream extends BufferedReader { 124 public CharacterStream(Reader peer, int buffer) { 125 super(peer, buffer); 126 } 127 128 public void close() throws IOException { 129 } 131 132 private void internalClose() throws IOException { 133 super.close(); 134 } 135 } 136 137 } 138 | Popular Tags |