1 11 package org.eclipse.team.internal.ccvs.ssh; 12 13 import java.io.FilterInputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 17 class ServerPacket extends Packet { 18 private PacketInputStream pis = null; 19 20 private static class PacketInputStream extends FilterInputStream { 21 private static int MAX_BUFFER_SIZE = 1024; 22 23 private byte[] buffer = new byte[MAX_BUFFER_SIZE]; 24 private int bufpos = 0; 25 private int buflen = 0; 26 private int bufrem = 0; 27 28 private long remaining = 0; 29 private Cipher cipher = null; 30 31 private long crc = 0; 32 private boolean closed = false; 33 34 public PacketInputStream(InputStream in, long length, Cipher cipher) { 35 super(in); 36 37 this.remaining = length; 38 this.cipher = cipher; 39 } 40 41 public int available() throws IOException { 42 if (closed) { 43 throw new IOException (CVSSSHMessages.closed); 44 } 45 46 return (int) Math.min(remaining - 4, Integer.MAX_VALUE); 47 } 48 49 public void close() throws IOException { 50 close(true); 51 } 52 53 public void close(boolean doCrcCheck) throws IOException { 54 if (!closed) { 55 try { 56 long toRead = doCrcCheck ? remaining - 4 : remaining; 57 58 try { 59 Misc.skipFully(this, toRead); 60 } catch(IOException e) { 61 } 63 64 if(doCrcCheck) { 65 if ((int) crc != Misc.readInt(buffer, bufpos)) { 66 throw new IOException (CVSSSHMessages.ServerPacket_crc); 67 } 68 } 69 } finally { 70 closed = true; 71 } 72 } 73 } 74 75 private void fill() throws IOException { 76 if (bufrem > 0) { 77 System.arraycopy(buffer, bufpos, buffer, 0, bufrem); 78 } 79 80 int totalBytesRead = bufrem; 81 int read = 0; 82 int toRead = (int)Math.min(remaining - totalBytesRead, MAX_BUFFER_SIZE - totalBytesRead); 83 84 while (toRead > 0) { 85 read = in.read(buffer, totalBytesRead, toRead); 86 87 if (read == -1) { 88 throw new IOException (CVSSSHMessages.stream); 89 } 90 91 totalBytesRead += read; 92 toRead -= read; 93 } 94 95 bufpos = 0; 96 97 buflen = (totalBytesRead / 8) * 8; 98 bufrem = totalBytesRead - buflen; 99 100 if (cipher != null) { 101 cipher.decipher(buffer, 0, buffer, 0, buflen); 102 } 103 104 crc = Misc.crc32(buffer, 0, buflen == remaining ? buflen - 4 : buflen, crc); 105 } 106 107 public int read() throws IOException { 108 if (closed) { 109 throw new IOException (CVSSSHMessages.closed); 110 } 111 112 if (remaining - 4 == 0) { 113 return -1; 114 } 115 116 if (bufpos == buflen) { 117 fill(); 118 } 119 120 int b = buffer[bufpos] & 0xff; 121 122 ++bufpos; 123 --remaining; 124 125 return b; 126 } 127 128 public int read(byte b[], int off, int len) throws IOException { 129 if (closed) { 130 throw new IOException (CVSSSHMessages.closed); 131 } 132 133 if (remaining - 4 == 0) { 134 return -1; 135 } 136 137 if (bufpos == buflen) { 138 fill(); 139 } 140 141 len = Math.min(len, (buflen == remaining + bufpos ? buflen - 4 : buflen) - bufpos); 142 143 System.arraycopy(buffer, bufpos, b, off, len); 144 145 bufpos += len; 146 remaining -= len; 147 148 return len; 149 } 150 } 151 public ServerPacket(InputStream is, Cipher cipher) throws java.io.IOException { 152 packetLength = Misc.readInt(is); 153 paddingLength = 8 - (packetLength % 8); 154 pis = new PacketInputStream(is, packetLength + paddingLength, cipher); 155 Misc.skipFully(pis, paddingLength); 156 packetType = (byte) pis.read(); 157 } 158 public void close(boolean doCrcCheck) throws IOException { 159 pis.close(doCrcCheck); 160 } 161 public InputStream getInputStream() { 162 return pis; 163 } 164 } 165 | Popular Tags |