jdk/src/share/classes/com/sun/org/apache/xml/internal/security/utils/UnsyncByteArrayOutputStream.java
author mullan
Mon, 26 Sep 2011 17:20:45 -0700
changeset 10694 cf59e2badd14
parent 6898 92eff65fd33d
child 18780 f47b920867e7
permissions -rw-r--r--
7088502: Security libraries don't build with javac -Werror Summary: Changes to files in src/share/classes/com/sun/org/apache/xml/internal/security and its subpackages to remove warnings Reviewed-by: mullan Contributed-by: kurchi.subhra.hazra@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * reserved comment block
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT REMOVE OR ALTER!
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
/*
6898
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
     6
 * Copyright 1999-2010 The Apache Software Foundation.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *  Licensed under the Apache License, Version 2.0 (the "License");
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 *  you may not use this file except in compliance with the License.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *  You may obtain a copy of the License at
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 *      http://www.apache.org/licenses/LICENSE-2.0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *  Unless required by applicable law or agreed to in writing, software
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 *  distributed under the License is distributed on an "AS IS" BASIS,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 *  See the License for the specific language governing permissions and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *  limitations under the License.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
package com.sun.org.apache.xml.internal.security.utils;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    23
import java.io.OutputStream;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
/**
6898
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    26
 * A simple Unsynced ByteArrayOutputStream
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @author raul
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    30
public class UnsyncByteArrayOutputStream extends OutputStream  {
6898
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    31
    private static final int INITIAL_SIZE = 8192;
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 6898
diff changeset
    32
    private static ThreadLocal<byte[]> bufCache = new ThreadLocal<byte[]>() {
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 6898
diff changeset
    33
        protected synchronized byte[] initialValue() {
6898
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    34
            return new byte[INITIAL_SIZE];
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    35
        }
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    36
    };
6898
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    37
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    38
    private byte[] buf;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    39
    private int size = INITIAL_SIZE;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    40
    private int pos = 0;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    41
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    42
    public UnsyncByteArrayOutputStream() {
10694
cf59e2badd14 7088502: Security libraries don't build with javac -Werror
mullan
parents: 6898
diff changeset
    43
        buf = bufCache.get();
6898
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    44
    }
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    45
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    46
    public void write(byte[] arg0) {
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    47
        int newPos = pos + arg0.length;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    48
        if (newPos > size) {
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    49
            expandSize(newPos);
1337
e8d6cef36199 6469266: Integrate Apache XMLSec 1.4.2 into JDK 7
mullan
parents: 2
diff changeset
    50
        }
6898
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    51
        System.arraycopy(arg0, 0, buf, pos, arg0.length);
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    52
        pos = newPos;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    53
    }
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    54
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    55
    public void write(byte[] arg0, int arg1, int arg2) {
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    56
        int newPos = pos + arg2;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    57
        if (newPos > size) {
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    58
            expandSize(newPos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
6898
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    60
        System.arraycopy(arg0, arg1, buf, pos, arg2);
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    61
        pos = newPos;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    62
    }
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    63
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    64
    public void write(int arg0) {
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    65
        int newPos = pos + 1;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    66
        if (newPos > size) {
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    67
            expandSize(newPos);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
6898
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    69
        buf[pos++] = (byte)arg0;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    70
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
6898
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    72
    public byte[] toByteArray() {
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    73
        byte result[] = new byte[pos];
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    74
        System.arraycopy(buf, 0, result, 0, pos);
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    75
        return result;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    76
    }
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    77
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    78
    public void reset() {
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    79
        pos = 0;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    80
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
6898
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    82
    private void expandSize(int newPos) {
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    83
        int newSize = size;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    84
        while (newPos > newSize) {
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    85
            newSize = newSize<<2;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
6898
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    87
        byte newBuf[] = new byte[newSize];
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    88
        System.arraycopy(buf, 0, newBuf, 0, pos);
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    89
        buf = newBuf;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    90
        size = newSize;
92eff65fd33d 6954275: XML signatures with reference data larger 16KB and cacheRef on fails to validate
mullan
parents: 1337
diff changeset
    91
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
}