KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ssh > ClientPacket


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.team.internal.ccvs.ssh;
12
13 class ClientPacket extends Packet {
14     byte[] packet;
15 public ClientPacket(int type, byte[] data, Cipher cipher) {
16     packetLength = data == null ? 5 : data.length + 5;
17     paddingLength = 8 - (packetLength % 8);
18     packetType = type;
19     packet = new byte[4 + paddingLength + packetLength];
20
21     int packetOff = 0;
22     Misc.writeInt(packetLength, packet, packetOff);
23     packetOff += 4;
24
25     if (cipher == null) {
26         for (int i = 0; i < paddingLength; i++) {
27             packet[packetOff++] = 0;
28         }
29     } else {
30         Misc.random(packet, packetOff, paddingLength, false);
31         packetOff += paddingLength;
32     }
33
34     packet[packetOff++] = (byte) packetType;
35
36     if (data != null) {
37         for (int i = 0; i < data.length; ++i) {
38             packet[packetOff++] = data[i];
39         }
40     }
41
42     long crc = Misc.crc32(packet, 4, packet.length - 8, 0);
43     Misc.writeInt((int) crc, packet, packetOff);
44     packetOff += 4;
45
46     if (cipher != null) {
47         cipher.encipher(packet, 4, packet, 4, packet.length - 4);
48     }
49 }
50 public byte[] getBytes() {
51     return packet;
52 }
53 }
54
Popular Tags