View Javadoc
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  }