1 11 package org.eclipse.core.internal.runtime.auth; 12 13 import java.io.*; 14 15 22 public class CipherInputStream extends FilterInputStream { 23 private static final int SKIP_BUFFER_SIZE = 2048; 24 private Cipher cipher; 25 26 35 public CipherInputStream(InputStream is, String password) { 36 super(is); 37 cipher = new Cipher(Cipher.DECRYPT_MODE, password); 38 } 39 40 43 public boolean markSupported() { 44 return false; 45 } 46 47 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 e) { 57 throw new IOException(e.getMessage()); 58 } 59 } 60 61 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 e) { 74 throw new IOException(e.getMessage()); 75 } 76 } 77 78 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 |