KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > runtime > CipherInputStream


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.runtime;
12
13 import java.io.*;
14
15 /**
16  * Decrypts a stream of data that was encrypted using the
17  * <code>Cipher</code> or <code>CipherOutputStream</code>.
18  *
19  * @see Cipher
20  * @see CipherOutputStream
21  */

22 public class CipherInputStream extends FilterInputStream {
23     private static final int SKIP_BUFFER_SIZE = 2048;
24     private Cipher cipher;
25
26     /**
27      * Constructs a new <code>CipherInputStream</code> that decrypts the
28      * data in the given <code>InputStream</code>. The data can only be
29      * decrypted if the given password is the same as that which was used
30      * to encrypt it.
31      *
32      * @param is
33      * @param password
34      */

35     public CipherInputStream(InputStream is, String JavaDoc password) {
36         super(is);
37         cipher = new Cipher(Cipher.DECRYPT_MODE, password);
38     }
39
40     /**
41      * @see InputStream#markSupported()
42      */

43     public boolean markSupported() {
44         return false;
45     }
46
47     /**
48      * @see InputStream#read()
49      */

50     public int read() throws IOException {
51         int b = super.read();
52         if (b == -1)
53             return -1;
54         try {
55             return (cipher.cipher((byte) b)) & 0x00ff;
56         } catch (Exception JavaDoc e) {
57             throw new IOException(e.getMessage());
58         }
59     }
60
61     /**
62      * @see InputStream#read(byte[], int, int)
63      */

64     public int read(byte b[], int off, int len) throws IOException {
65         int bytesRead = in.read(b, off, len);
66         if (bytesRead == -1)
67             return -1;
68         try {
69             byte[] result = cipher.cipher(b, off, bytesRead);
70             for (int i = 0; i < result.length; ++i)
71                 b[i + off] = result[i];
72             return bytesRead;
73         } catch (Exception JavaDoc e) {
74             throw new IOException(e.getMessage());
75         }
76     }
77
78     /**
79      * @see InputStream#skip(long)
80      */

81     public long skip(long n) throws IOException {
82         byte[] buffer = new byte[SKIP_BUFFER_SIZE];
83
84         int bytesRead = 0;
85         long bytesRemaining = n;
86
87         while (bytesRead != -1 && bytesRemaining > 0) {
88             bytesRead = read(buffer, 0, (int) Math.min(SKIP_BUFFER_SIZE, bytesRemaining));
89             if (bytesRead > 0) {
90                 bytesRemaining -= bytesRead;
91             }
92         }
93
94         return n - bytesRemaining;
95     }
96 }
97
Popular Tags