001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /**
019: * @author Alexander Y. Kleymenov
020: * @version $Revision$
021: */package javax.crypto;
022:
023: import java.io.FilterOutputStream;
024: import java.io.IOException;
025: import java.io.OutputStream;
026: import javax.crypto.NullCipher;
027:
028: /**
029: * @com.intel.drl.spec_ref
030: */
031: public class CipherOutputStream extends FilterOutputStream {
032:
033: private final Cipher cipher;
034: private final byte[] arr = new byte[1];
035:
036: /**
037: * @com.intel.drl.spec_ref
038: */
039: public CipherOutputStream(OutputStream os, Cipher c) {
040: super (os);
041: cipher = c;
042: }
043:
044: /**
045: * @com.intel.drl.spec_ref
046: */
047: protected CipherOutputStream(OutputStream os) {
048: this (os, new NullCipher());
049: }
050:
051: /**
052: * @com.intel.drl.spec_ref
053: */
054: @Override
055: public void write(int b) throws IOException {
056: byte[] result;
057: arr[0] = (byte) b;
058: result = cipher.update(arr);
059: if (result != null) {
060: out.write(result);
061: }
062: }
063:
064: /**
065: * @com.intel.drl.spec_ref
066: */
067: @Override
068: public void write(byte[] b) throws IOException {
069: write(b, 0, b.length);
070: }
071:
072: /**
073: * @com.intel.drl.spec_ref
074: */
075: @Override
076: public void write(byte[] b, int off, int len) throws IOException {
077: if (len == 0) {
078: return;
079: }
080: byte[] result = cipher.update(b, off, len);
081: if (result != null) {
082: out.write(result);
083: }
084: }
085:
086: /**
087: * @com.intel.drl.spec_ref
088: */
089: @Override
090: public void flush() throws IOException {
091: out.flush();
092: }
093:
094: /**
095: * @com.intel.drl.spec_ref
096: */
097: @Override
098: public void close() throws IOException {
099: byte[] result;
100: try {
101: if (cipher != null) {
102: result = cipher.doFinal();
103: if (result != null) {
104: out.write(result);
105: }
106: }
107: if (out != null) {
108: out.flush();
109: }
110: } catch (BadPaddingException e) {
111: throw new IOException(e.getMessage());
112: } catch (IllegalBlockSizeException e) {
113: throw new IOException(e.getMessage());
114: } finally {
115: if (out != null) {
116: out.close();
117: }
118: }
119: }
120: }
|