src/share/classes/com/sun/org/apache/xml/internal/security/utils/UnsyncBufferedOutputStream.java

Print this page


   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 }
   1 /*
   2  * reserved comment block
   3  * DO NOT REMOVE OR ALTER!
   4  */
   5 /**
   6  * Licensed to the Apache Software Foundation (ASF) under one
   7  * or more contributor license agreements. See the NOTICE file
   8  * distributed with this work for additional information
   9  * regarding copyright ownership. The ASF licenses this file
  10  * to you under the Apache License, Version 2.0 (the
  11  * "License"); you may not use this file except in compliance
  12  * with the License. You may obtain a copy of the License at
  13  *
  14  * http://www.apache.org/licenses/LICENSE-2.0
  15  *
  16  * Unless required by applicable law or agreed to in writing,
  17  * software distributed under the License is distributed on an
  18  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  19  * KIND, either express or implied. See the License for the
  20  * specific language governing permissions and limitations
  21  * under the License.
  22  */
  23 package com.sun.org.apache.xml.internal.security.utils;
  24 
  25 import java.io.IOException;
  26 import java.io.OutputStream;
  27 
  28 /**
  29  * A class that buffers without synchronizing its methods
  30  * @author raul

  31  */
  32 public class UnsyncBufferedOutputStream extends OutputStream {
  33     static final int size = 8*1024;
  34     
  35     private int pointer = 0;
  36     private final OutputStream out;
  37 
  38     private final byte[] buf;
  39     








  40     /**
  41      * Creates a buffered output stream without synchronization
  42      * @param out the outputstream to buffer
  43      */
  44     public UnsyncBufferedOutputStream(OutputStream out) {
  45         buf = new byte[size];
  46         this.out = out;
  47     }
  48 
  49     /** @inheritDoc */
  50     public void write(byte[] arg0) throws IOException {
  51         write(arg0, 0, arg0.length);
  52     }
  53 
  54     /** @inheritDoc */
  55     public void write(byte[] arg0, int arg1, int len) throws IOException {
  56         int newLen = pointer+len;
  57         if (newLen > size) {
  58             flushBuffer();              
  59             if (len > size) {
  60                 out.write(arg0, arg1,len);
  61                 return;
  62             }
  63             newLen = len;
  64         }
  65         System.arraycopy(arg0, arg1, buf, pointer, len);
  66         pointer = newLen;
  67     }
  68 
  69     private void flushBuffer() throws IOException {
  70         if (pointer > 0) {
  71             out.write(buf, 0, pointer);
  72         }
  73         pointer = 0;
  74 
  75     }
  76 
  77     /** @inheritDoc */
  78     public void write(int arg0) throws IOException {            
  79         if (pointer >= size) {
  80             flushBuffer();
  81         }
  82         buf[pointer++] = (byte)arg0;
  83 
  84     }
  85 
  86     /** @inheritDoc */  
  87     public void flush() throws IOException {
  88         flushBuffer();
  89         out.flush();
  90     }
  91 
  92     /** @inheritDoc */
  93     public void close() throws IOException {
  94         flush();
  95         out.close();
  96     }
  97 
  98 }