01: /*
02: * BufferedStreamWriter.java
03: *
04: * Created on August 7, 2006, 11:39 AM
05: *
06: * The contents of this file are subject to the terms
07: * of the Common Development and Distribution License
08: * (the License). You may not use this file except in
09: * compliance with the License.
10: *
11: * You can obtain a copy of the license at
12: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
13: * See the License for the specific language governing
14: * permissions and limitations under the License.
15: *
16: * When distributing Covered Code, include this CDDL
17: * Header Notice in each file and include the License file
18: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
19: * If applicable, add the following below the CDDL Header,
20: * with the fields enclosed by brackets [] replaced by
21: * you own identifying information:
22: * "Portions Copyrighted [year] [name of copyright owner]"
23: *
24: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
25: */
26:
27: package com.sun.xml.ws.security.opt.impl.util;
28:
29: import java.io.IOException;
30: import javax.crypto.CipherOutputStream;
31:
32: /**
33: *
34: * @author K.Venugopal@sun.com
35: */
36: public class BufferedStreamWriter extends java.io.OutputStream {
37: int size = 4 * 1024;
38: byte[] buf = null;
39: int pos = 0;
40: CipherOutputStream cos = null;
41:
42: /** Creates a new instance of BufferedStreamWriter */
43: public BufferedStreamWriter(CipherOutputStream cos) {
44: buf = new byte[this .size];
45: this .cos = cos;
46: }
47:
48: public BufferedStreamWriter(CipherOutputStream cos, int size) {
49: buf = new byte[size];
50: this .cos = cos;
51: }
52:
53: public void write(byte[] arg0) throws IOException {
54: int newPos = pos + arg0.length;
55: if (newPos >= size) {
56: flush();
57: System.arraycopy(arg0, 0, buf, 0, arg0.length);
58: pos = arg0.length;
59: } else {
60: System.arraycopy(arg0, 0, buf, pos, arg0.length);
61: pos = newPos;
62: }
63: }
64:
65: /** @inheritDoc */
66: public void write(byte[] arg0, int arg1, int arg2)
67: throws IOException {
68: int newPos = pos + arg2;
69: if (newPos >= size) {
70: flush();
71: System.arraycopy(arg0, arg1, buf, 0, arg2);
72: pos = arg2;
73: } else {
74: System.arraycopy(arg0, arg1, buf, pos, arg2);
75: pos = newPos;
76: }
77: }
78:
79: /** @inheritDoc */
80: public void write(int arg0) throws IOException {
81: if (pos >= size) {
82: flush();
83: }
84: buf[pos++] = (byte) arg0;
85: }
86:
87: public void flush() throws IOException {
88: cos.write(buf, 0, pos);
89: pos = 0;
90: }
91: }
|