All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.xml.ws.security.opt.impl.util.DecryptedInputStream Maven / Gradle / Ivy

There is a newer version: 4.0.3
Show newest version
/*
 * Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0, which is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package com.sun.xml.ws.security.opt.impl.util;

import java.io.ByteArrayInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 *
 * @author [email protected]
 */
public class DecryptedInputStream extends FilterInputStream{
    
    private static final int SKIP_BUFFER_SIZE = 2048;
    // skipBuffer is initialized in skip(long), if needed.
    private static byte[] skipBuffer;
    
    private StringBuilder startElement = new StringBuilder(" parentNS) {
        super(is);
        Set> set = parentNS.entrySet(); 
        Iterator> iter = set.iterator();
        while(iter.hasNext()){
           Map.Entry entry = iter.next();
           if(!"".equals(entry.getKey())){
               startElement.append(" xmlns:"+entry.getKey()+"=\""+entry.getValue()+"\"");
           } else{
               startElement.append(" xmlns=\"" + entry.getValue()+"\"");
           }
        }
        startElement.append(" >");
        String startElem = startElement.toString();
        startIS = new ByteArrayInputStream(startElem.getBytes());
    }
    
    public int read() throws IOException{
        int readVal = startIS.read();
        if(readVal != -1){
            return readVal;
        }
        readVal = in.read();
        if(readVal != -1){
            return readVal;
        }
        return endIS.read();
    }
    
    public int read(byte [] b) throws IOException{
        return read(b,0,b.length-1);
    }
    
    public int read(byte[] b , int off, int len) throws IOException{
        if (b == null) {
	    throw new NullPointerException();
	} else if ((off < 0) || (off > b.length) || (len < 0) ||
		   ((off + len) > b.length) || ((off + len) < 0)) {
	    throw new IndexOutOfBoundsException();
	} else if (len == 0) {
	    return 0;
	}
        int readVal = read();
        if(readVal == -1){
            return -1;
        }
        b[off] = (byte)readVal;
        int i = 1;
        for(; i < len; i++){
            readVal = read();
            if(readVal == -1){
                break;
            }
            if(b != null){
                b[off+i] = (byte)readVal;
            }
        }
        return i;
    }
    
    public long skip(long n) throws IOException {
        long remaining = n;
	int nr;
	if (skipBuffer == null)
	    skipBuffer = new byte[SKIP_BUFFER_SIZE];

	byte[] localSkipBuffer = skipBuffer;
        
        if (n <= 0) {
	    return 0;
	}

	while (remaining > 0) {
	    nr = read(localSkipBuffer, 0,
		      (int) Math.min(SKIP_BUFFER_SIZE, remaining));
	    if (nr < 0) {
		break;
	    }
	    remaining -= nr;
	}
	
	return n - remaining;
    }
    
    public boolean markSupported() {
	return false;
    }
    
    public synchronized void reset() throws IOException {
	throw new IOException("mark/reset not supported");
    }
    
    public void close() throws IOException{
        startIS.close();
        in.close();
        endIS.close();
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy