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