blob: d3ec0ba628045bf8e0a8d3e8b0e2cac40c853140 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
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 */
21package com.sun.org.apache.xml.internal.security.utils;
22
23import java.io.IOException;
24import java.io.OutputStream;
25
26/**
27 * A class that buffers writte without synchronize its methods
28 * @author raul
29 *
30 */
31public class UnsyncBufferedOutputStream extends OutputStream {
32 final OutputStream out;
33 static final int size=8*1024;
34 final byte[] buf=new byte[size];
35 int pointer=0;
36 /**
37 * Creates a buffered output stream without synchronization
38 * @param out the outputstream to buffer
39 */
40 public UnsyncBufferedOutputStream(OutputStream out) {
41 this.out=out;
42 }
43
44 /** @inheritDoc */
45 public void write(byte[] arg0) throws IOException {
46 write(arg0,0,arg0.length);
47 }
48
49 /** @inheritDoc */
50 public void write(byte[] arg0, int arg1, int len) throws IOException {
51 int newLen=pointer+len;
52 if (newLen> size) {
53 flushBuffer();
54 if (len>size) {
55 out.write(arg0,arg1,len);
56 return;
57 }
58 newLen=len;
59 }
60 System.arraycopy(arg0,arg1,buf,pointer,len);
61 pointer=newLen;
62 }
63
64 private final void flushBuffer() throws IOException {
65 if (pointer>0)
66 out.write(buf,0,pointer);
67 pointer=0;
68
69 }
70
71 /** @inheritDoc */
72 public void write(int arg0) throws IOException {
73 if (pointer>= size) {
74 flushBuffer();
75 }
76 buf[pointer++]=(byte)arg0;
77
78 }
79
80 /** @inheritDoc */
81 public void flush() throws IOException {
82 flushBuffer();
83 out.flush();
84 }
85
86 /** @inheritDoc */
87 public void close() throws IOException {
88 flush();
89 }
90
91}