KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datasystem > persistentsystem > versioninfo > EncryptDRandomAccessFileUpto3_2


1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.versioninfo;
2
3 import java.io.*;
4
5
6 import com.daffodilwoods.daffodildb.server.datasystem.encryptdecrypt.*;
7 import com.daffodilwoods.database.resource.*;
8 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.DRandomAccessFileUpto3_2;
9 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.DatabaseProperties;
10 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.FileGenerator;
11
12 public class EncryptDRandomAccessFileUpto3_2 extends DRandomAccessFileUpto3_2{
13
14
15     private EDBlockCipher m_EDBlockEDDes3Cipher;
16     private int m_blockIterations;
17
18     DatabaseProperties databaseProperties;
19
20     public EncryptDRandomAccessFileUpto3_2(RandomAccessFile raf0,long fileSize0,String JavaDoc databaseURL0,int incrementFactor0,boolean multiFileSupport1,FileGenerator fileGenerator0,EDBlockCipher bolckCipher0,int clusterSize )throws DException {
21         super(raf0,fileSize0,databaseURL0,incrementFactor0,multiFileSupport1,fileGenerator0,clusterSize);
22         m_EDBlockEDDes3Cipher = bolckCipher0;
23         m_blockIterations = clusterSize/ m_EDBlockEDDes3Cipher.getBlockSize();
24     }
25
26     /**
27      * Write the bytes after encryption with the help of EDBlockCipher which is capable of encrypting or decrypting the bytes.
28      *
29      * @param startAddress From where we have to write the bytes
30      * @param bytes bytes to be written
31      * @throws DException FileNotFound Exception
32      */

33
34    protected synchronized void write(long startAddress,byte [] bytes) throws DException{
35         try {
36             seek(startAddress);
37             if(startAddress != 0){
38                 byte abyte2[] = new byte[bytes.length];
39                 for(int j = 0; j < m_blockIterations; j++)
40                     m_EDBlockEDDes3Cipher.encryptBytes(bytes, j * m_EDBlockEDDes3Cipher.getBlockSize(), abyte2, j * m_EDBlockEDDes3Cipher.getBlockSize());
41                 raf.write(abyte2);
42             }
43             else
44                 raf.write(bytes);
45         }
46         catch (IOException ex) {
47             throw new DException("DSE2025",new Object JavaDoc[] {ex.getMessage()});
48         }
49     }
50
51     /**
52      * Returns the bytes after decryption with the help of EDBlockCipher which is capable of encrypting or decrypting the bytes.
53      *
54      * @param startAddress From where we have to read the bytes
55      * @param bytes bytes to be read
56      * @throws DException FileNotFound Exception
57      */

58
59     protected synchronized byte[] read(byte b[],long addr) throws DException {
60             seek(addr);
61             try {
62               raf.read(b);
63             }
64             catch (IOException ex) {
65                 throw new DException("DSE2025",new Object JavaDoc[] {ex.getMessage()});
66             }
67             byte abyte1[] = new byte[b.length];
68             for(int i = 0; i < m_blockIterations; i++)
69                 m_EDBlockEDDes3Cipher.decryptBytes(b, i * m_EDBlockEDDes3Cipher.getBlockSize(), abyte1, i * m_EDBlockEDDes3Cipher.getBlockSize());
70             return abyte1;
71          }
72 }
73
Popular Tags