1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /*
   6  * Copyright 1999-2005 The Apache Software Foundation.
   7  *
   8  *  Licensed under the Apache License, Version 2.0 (the "License");
   9  *  you may not use this file except in compliance with the License.
  10  *  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  *  Unless required by applicable law or agreed to in writing, software
  15  *  distributed under the License is distributed on an "AS IS" BASIS,
  16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  *  See the License for the specific language governing permissions and
  18  *  limitations under the License.
  19  *
  20  */
  21 package com.sun.org.apache.xml.internal.security.utils;
  22 
  23 import java.io.IOException;
  24 import java.io.OutputStream;
  25 
  26 /**
  27  * A class that buffers writte without synchronize its methods
  28  * @author raul
  29  *
  30  */
  31 public class UnsyncBufferedOutputStream extends OutputStream {
  32         final OutputStream out;
  33 
  34         final byte[] buf;
  35         static final int size=8*1024;
  36         private static ThreadLocal<byte[]> bufCahce = new ThreadLocal<byte[]>() {
  37         protected synchronized byte[] initialValue() {
  38             return new byte[size];
  39         }
  40     };
  41         int pointer=0;
  42         /**
  43          * Creates a buffered output stream without synchronization
  44          * @param out the outputstream to buffer
  45          */
  46         public UnsyncBufferedOutputStream(OutputStream out) {
  47                 buf=bufCahce.get();
  48                 this.out=out;
  49         }
  50 
  51         /** @inheritDoc */
  52         public void write(byte[] arg0) throws IOException {
  53                 write(arg0,0,arg0.length);
  54         }
  55 
  56         /** @inheritDoc */
  57         public void write(byte[] arg0, int arg1, int len) throws IOException {
  58                 int newLen=pointer+len;
  59                 if (newLen> size) {
  60                         flushBuffer();
  61                         if (len>size) {
  62                                 out.write(arg0,arg1,len);
  63                                 return;
  64                         }
  65                         newLen=len;
  66                 }
  67                 System.arraycopy(arg0,arg1,buf,pointer,len);
  68                 pointer=newLen;
  69         }
  70 
  71         private final void flushBuffer() throws IOException {
  72                 if (pointer>0)
  73                         out.write(buf,0,pointer);
  74                 pointer=0;
  75 
  76         }
  77 
  78         /** @inheritDoc */
  79         public void write(int arg0) throws IOException {
  80                 if (pointer>= size) {
  81                         flushBuffer();
  82                 }
  83                 buf[pointer++]=(byte)arg0;
  84 
  85         }
  86 
  87         /** @inheritDoc */
  88         public void flush() throws IOException {
  89                 flushBuffer();
  90                 out.flush();
  91         }
  92 
  93         /** @inheritDoc */
  94         public void close() throws IOException {
  95                 flush();
  96         }
  97 
  98 }