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:
030: /**
031: *
032: * @author Ashutosh.Shahi@Sun.com
033: */
034: public class CheckedInputStream extends FilterInputStream {
035:
036: int read;
037: boolean isEmpty = false;
038: boolean xmlDecl = false;
039: byte[] tmpBytes = new byte[4];
040: ByteArrayInputStream tmpIs = null;
041:
042: /** Creates a new instance of CheckedCipherInputStream */
043: public CheckedInputStream(InputStream cin) throws IOException {
044: super (cin);
045: read = cin.read();
046: if (read == -1) {
047: isEmpty = true;
048: } else {
049: cin.read(tmpBytes, 0, 4);
050: tmpIs = new ByteArrayInputStream(tmpBytes);
051: }
052: }
053:
054: public int read() throws IOException {
055: if (read != -1) {
056: int tmp = read;
057: read = -1;
058:
059: if (tmp == '<' && "?xml".equals(new String(tmpBytes))) {
060: xmlDecl = true;
061: int c = super .read();
062: while (c != '>') {
063: //do nothing
064: c = super .read();
065: }
066: }
067:
068: if (!xmlDecl) {
069: return tmp;
070: }
071: }
072:
073: if (!xmlDecl) {
074: int c = tmpIs.read();
075: if (c != -1) {
076: return c;
077: }
078: }
079:
080: return super .read();
081: }
082:
083: public int read(byte[] b) throws IOException {
084: return read(b, 0, b.length);
085: }
086:
087: public int read(byte[] b, int off, int len) throws IOException {
088: if (read != -1) {
089:
090: if (read == '<' && "?xml".equals(new String(tmpBytes))) {
091: xmlDecl = true;
092: int c = super .read();
093: while (c != '>') {
094: //do nothing
095: c = super .read();
096: }
097: }
098:
099: int i = 0;
100: b[off + i] = (byte) read;
101: i++;
102: len--;
103: read = -1;
104:
105: if (!xmlDecl) {
106:
107: int c = tmpIs.read();
108: while (c != -1 && len > 0) {
109: b[off + i] = (byte) c;
110: i++;
111: c = tmpIs.read();
112: len--;
113: }
114:
115: }
116: int rb = 0;
117: if (len > 0) {
118: rb = super .read(b, off + i, len);
119: }
120:
121: return rb + i;
122: }
123: return super .read(b, off, len);
124: }
125:
126: public long skip(long n) throws IOException {
127: if (read != -1) {
128: read = -1;
129: return super .skip(n - 1) + 1;
130: }
131: return super .skip(n);
132: }
133:
134: public boolean isEmpty() {
135: return isEmpty;
136: }
137: }
|