comparison src/ch/ethz/ssh2/crypto/cipher/CipherOutputStream.java @ 307:071eccdff8ea ganymed

fix java formatting
author Carl Byington <carl@five-ten-sg.com>
date Wed, 30 Jul 2014 14:16:58 -0700
parents 91a31873c42a
children
comparison
equal deleted inserted replaced
305:d2b303406d63 307:071eccdff8ea
11 * CipherOutputStream. 11 * CipherOutputStream.
12 * 12 *
13 * @author Christian Plattner 13 * @author Christian Plattner
14 * @version $Id: CipherOutputStream.java 85 2014-04-07 14:05:09Z dkocher@sudo.ch $ 14 * @version $Id: CipherOutputStream.java 85 2014-04-07 14:05:09Z dkocher@sudo.ch $
15 */ 15 */
16 public class CipherOutputStream 16 public class CipherOutputStream {
17 { 17 BlockCipher currentCipher;
18 BlockCipher currentCipher; 18 OutputStream bo;
19 OutputStream bo; 19 byte[] buffer;
20 byte[] buffer; 20 byte[] enc;
21 byte[] enc; 21 int blockSize;
22 int blockSize; 22 int pos;
23 int pos;
24 23
25 /* 24 /*
26 * We cannot use java.io.BufferedOutputStream, since that is not available 25 * We cannot use java.io.BufferedOutputStream, since that is not available
27 * in J2ME. Everything could be improved here alot. 26 * in J2ME. Everything could be improved here alot.
28 */ 27 */
29 28
30 private static final int BUFF_SIZE = 8192; 29 private static final int BUFF_SIZE = 8192;
31 byte[] out_buffer = new byte[BUFF_SIZE]; 30 byte[] out_buffer = new byte[BUFF_SIZE];
32 int out_buffer_pos = 0; 31 int out_buffer_pos = 0;
33 32
34 public CipherOutputStream(BlockCipher tc, OutputStream bo) 33 public CipherOutputStream(BlockCipher tc, OutputStream bo) {
35 { 34 this.bo = bo;
36 this.bo = bo; 35 changeCipher(tc);
37 changeCipher(tc); 36 }
38 }
39 37
40 private void internal_write(byte[] src, int off, int len) throws IOException 38 private void internal_write(byte[] src, int off, int len) throws IOException {
41 { 39 while (len > 0) {
42 while (len > 0) 40 int space = BUFF_SIZE - out_buffer_pos;
43 { 41 int copy = (len > space) ? space : len;
44 int space = BUFF_SIZE - out_buffer_pos; 42 System.arraycopy(src, off, out_buffer, out_buffer_pos, copy);
45 int copy = (len > space) ? space : len; 43 off += copy;
44 out_buffer_pos += copy;
45 len -= copy;
46 46
47 System.arraycopy(src, off, out_buffer, out_buffer_pos, copy); 47 if (out_buffer_pos >= BUFF_SIZE) {
48 bo.write(out_buffer, 0, BUFF_SIZE);
49 out_buffer_pos = 0;
50 }
51 }
52 }
48 53
49 off += copy; 54 private void internal_write(int b) throws IOException {
50 out_buffer_pos += copy; 55 out_buffer[out_buffer_pos++] = (byte) b;
51 len -= copy;
52 56
53 if (out_buffer_pos >= BUFF_SIZE) 57 if (out_buffer_pos >= BUFF_SIZE) {
54 { 58 bo.write(out_buffer, 0, BUFF_SIZE);
55 bo.write(out_buffer, 0, BUFF_SIZE); 59 out_buffer_pos = 0;
56 out_buffer_pos = 0; 60 }
57 } 61 }
58 }
59 }
60 62
61 private void internal_write(int b) throws IOException 63 public void flush() throws IOException {
62 { 64 if (pos != 0) {
63 out_buffer[out_buffer_pos++] = (byte) b; 65 throw new IOException("FATAL: cannot flush since crypto buffer is not aligned.");
64 if (out_buffer_pos >= BUFF_SIZE) 66 }
65 {
66 bo.write(out_buffer, 0, BUFF_SIZE);
67 out_buffer_pos = 0;
68 }
69 }
70 67
71 public void flush() throws IOException 68 if (out_buffer_pos > 0) {
72 { 69 bo.write(out_buffer, 0, out_buffer_pos);
73 if (pos != 0) 70 out_buffer_pos = 0;
74 { 71 }
75 throw new IOException("FATAL: cannot flush since crypto buffer is not aligned.");
76 }
77 72
78 if (out_buffer_pos > 0) 73 bo.flush();
79 { 74 }
80 bo.write(out_buffer, 0, out_buffer_pos);
81 out_buffer_pos = 0;
82 }
83 bo.flush();
84 }
85 75
86 public void changeCipher(BlockCipher bc) 76 public void changeCipher(BlockCipher bc) {
87 { 77 this.currentCipher = bc;
88 this.currentCipher = bc; 78 blockSize = bc.getBlockSize();
89 blockSize = bc.getBlockSize(); 79 buffer = new byte[blockSize];
90 buffer = new byte[blockSize]; 80 enc = new byte[blockSize];
91 enc = new byte[blockSize]; 81 pos = 0;
92 pos = 0; 82 }
93 }
94 83
95 private void writeBlock() throws IOException 84 private void writeBlock() throws IOException {
96 { 85 try {
97 try 86 currentCipher.transformBlock(buffer, 0, enc, 0);
98 { 87 }
99 currentCipher.transformBlock(buffer, 0, enc, 0); 88 catch (Exception e) {
100 } 89 throw new IOException("Error while decrypting block.", e);
101 catch (Exception e) 90 }
102 {
103 throw new IOException("Error while decrypting block.", e);
104 }
105 91
106 internal_write(enc, 0, blockSize); 92 internal_write(enc, 0, blockSize);
107 pos = 0; 93 pos = 0;
108 } 94 }
109 95
110 public void write(byte[] src, int off, int len) throws IOException 96 public void write(byte[] src, int off, int len) throws IOException {
111 { 97 while (len > 0) {
112 while (len > 0) 98 int avail = blockSize - pos;
113 { 99 int copy = Math.min(avail, len);
114 int avail = blockSize - pos; 100 System.arraycopy(src, off, buffer, pos, copy);
115 int copy = Math.min(avail, len); 101 pos += copy;
102 off += copy;
103 len -= copy;
116 104
117 System.arraycopy(src, off, buffer, pos, copy); 105 if (pos >= blockSize) {
118 pos += copy; 106 writeBlock();
119 off += copy; 107 }
120 len -= copy; 108 }
109 }
121 110
122 if (pos >= blockSize) 111 public void write(int b) throws IOException {
123 { 112 buffer[pos++] = (byte) b;
124 writeBlock();
125 }
126 }
127 }
128 113
129 public void write(int b) throws IOException 114 if (pos >= blockSize) {
130 { 115 writeBlock();
131 buffer[pos++] = (byte) b; 116 }
132 if (pos >= blockSize) 117 }
133 {
134 writeBlock();
135 }
136 }
137 118
138 public void writePlain(int b) throws IOException 119 public void writePlain(int b) throws IOException {
139 { 120 if (pos != 0) {
140 if (pos != 0) 121 throw new IOException("Cannot write plain since crypto buffer is not aligned.");
141 { 122 }
142 throw new IOException("Cannot write plain since crypto buffer is not aligned.");
143 }
144 internal_write(b);
145 }
146 123
147 public void writePlain(byte[] b, int off, int len) throws IOException 124 internal_write(b);
148 { 125 }
149 if (pos != 0) 126
150 { 127 public void writePlain(byte[] b, int off, int len) throws IOException {
151 throw new IOException("Cannot write plain since crypto buffer is not aligned."); 128 if (pos != 0) {
152 } 129 throw new IOException("Cannot write plain since crypto buffer is not aligned.");
153 internal_write(b, off, len); 130 }
154 } 131
132 internal_write(b, off, len);
133 }
155 } 134 }