001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the License). You may not use this file except in
005: * compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * Header Notice in each file and include the License file
014: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
015: * If applicable, add the following below the CDDL Header,
016: * with the fields enclosed by brackets [] replaced by
017: * you own identifying information:
018: * "Portions Copyrighted [year] [name of copyright owner]"
019: *
020: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
021: */
022:
023: package com.sun.xml.ws.security.opt.impl.util;
024:
025: import java.io.ByteArrayInputStream;
026: import java.io.FilterInputStream;
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.util.HashMap;
030: import java.util.Iterator;
031: import java.util.Map;
032: import java.util.Set;
033:
034: /**
035: *
036: * @author Ashutosh.Shahi@sun.com
037: */
038: public class DecryptedInputStream extends FilterInputStream {
039:
040: private static final int SKIP_BUFFER_SIZE = 2048;
041: // skipBuffer is initialized in skip(long), if needed.
042: private static byte[] skipBuffer;
043:
044: private StringBuilder startElement = new StringBuilder(
045: "<StartElement");
046: private static final String endElement = "</StartElement>";
047: private InputStream startIS = null;
048: private InputStream endIS = new ByteArrayInputStream(endElement
049: .getBytes());
050:
051: /** Creates a new instance of DecryptedInputStream */
052: public DecryptedInputStream(InputStream is,
053: HashMap<String, String> parentNS) {
054: super (is);
055: Set<Map.Entry<String, String>> set = parentNS.entrySet();
056: Iterator<Map.Entry<String, String>> iter = set.iterator();
057: while (iter.hasNext()) {
058: Map.Entry<String, String> entry = iter.next();
059: if (!"".equals(entry.getKey())) {
060: startElement.append(" xmlns:" + entry.getKey() + "=\""
061: + entry.getValue() + "\"");
062: } else {
063: startElement.append(" xmlns=\"" + entry.getValue()
064: + "\"");
065: }
066: }
067: startElement.append(" >");
068: String startElem = startElement.toString();
069: startIS = new ByteArrayInputStream(startElem.getBytes());
070: }
071:
072: public int read() throws IOException {
073: int readVal = startIS.read();
074: if (readVal != -1) {
075: return readVal;
076: }
077: readVal = in.read();
078: if (readVal != -1) {
079: return readVal;
080: }
081: return endIS.read();
082: }
083:
084: public int read(byte[] b) throws IOException {
085: return read(b, 0, b.length - 1);
086: }
087:
088: public int read(byte[] b, int off, int len) throws IOException {
089: if (b == null) {
090: throw new NullPointerException();
091: } else if ((off < 0) || (off > b.length) || (len < 0)
092: || ((off + len) > b.length) || ((off + len) < 0)) {
093: throw new IndexOutOfBoundsException();
094: } else if (len == 0) {
095: return 0;
096: }
097: int readVal = read();
098: if (readVal == -1) {
099: return -1;
100: }
101: b[off] = (byte) readVal;
102: int i = 1;
103: for (; i < len; i++) {
104: readVal = read();
105: if (readVal == -1) {
106: break;
107: }
108: if (b != null) {
109: b[off + i] = (byte) readVal;
110: }
111: }
112: return i;
113: }
114:
115: public long skip(long n) throws IOException {
116: long remaining = n;
117: int nr;
118: if (skipBuffer == null)
119: skipBuffer = new byte[SKIP_BUFFER_SIZE];
120:
121: byte[] localSkipBuffer = skipBuffer;
122:
123: if (n <= 0) {
124: return 0;
125: }
126:
127: while (remaining > 0) {
128: nr = read(localSkipBuffer, 0, (int) Math.min(
129: SKIP_BUFFER_SIZE, remaining));
130: if (nr < 0) {
131: break;
132: }
133: remaining -= nr;
134: }
135:
136: return n - remaining;
137: }
138:
139: public boolean markSupported() {
140: return false;
141: }
142:
143: public synchronized void reset() throws IOException {
144: throw new IOException("mark/reset not supported");
145: }
146:
147: public void close() throws IOException {
148: startIS.close();
149: in.close();
150: endIS.close();
151: }
152:
153: }
|