KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > security > utils > UnsyncBufferedOutputStream


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17 package com.sun.org.apache.xml.internal.security.utils;
18
19 import java.io.IOException JavaDoc;
20 import java.io.OutputStream JavaDoc;
21
22 /**
23  * A class that buffers writte without synchronize its methods
24  * @author raul
25  *
26  */

27 public class UnsyncBufferedOutputStream extends OutputStream JavaDoc {
28     final OutputStream JavaDoc out;
29     static final int size=8*1024;
30     final byte[] buf=new byte[size];
31     int pointer=0;
32     /**
33      * Creates a buffered output stream without synchronization
34      * @param out the outputstream to buffer
35      */

36     public UnsyncBufferedOutputStream(OutputStream JavaDoc out) {
37         this.out=out;
38     }
39     
40     /** @inheritDoc */
41     public void write(byte[] arg0) throws IOException JavaDoc {
42         write(arg0,0,arg0.length);
43     }
44     
45     /** @inheritDoc */
46     public void write(byte[] arg0, int arg1, int len) throws IOException JavaDoc {
47         int newLen=pointer+len;
48         if (newLen> size) {
49             flushBuffer();
50             if (len>size) {
51                 out.write(arg0,arg1,len);
52                 return;
53             }
54             newLen=len;
55         }
56         System.arraycopy(arg0,arg1,buf,pointer,len);
57         pointer=newLen;
58     }
59     
60     private final void flushBuffer() throws IOException JavaDoc {
61         if (pointer>0)
62             out.write(buf,0,pointer);
63         pointer=0;
64         
65     }
66     
67     /** @inheritDoc */
68     public void write(int arg0) throws IOException JavaDoc {
69         if (pointer>= size) {
70             flushBuffer();
71         }
72         buf[pointer++]=(byte)arg0;
73
74     }
75     
76     /** @inheritDoc */
77     public void flush() throws IOException JavaDoc {
78         flushBuffer();
79         out.flush();
80     }
81
82     /** @inheritDoc */
83     public void close() throws IOException JavaDoc {
84         flush();
85     }
86
87 }
88
Popular Tags