1 12 package org.eclipse.team.internal.ccvs.core.connection; 13 14 15 import java.io.*; 16 17 import org.eclipse.core.runtime.IProgressMonitor; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.osgi.util.NLS; 20 import org.eclipse.team.internal.ccvs.core.*; 21 22 39 public class Connection { 40 private static final byte NEWLINE= 0xA; 41 42 private IServerConnection serverConnection; 43 private ICVSRepositoryLocation fCVSRoot; 44 private boolean fIsEstablished; 45 private InputStream fResponseStream; 46 private String fServerEncoding; 47 private byte[] readLineBuffer = new byte[256]; 48 49 public Connection(ICVSRepositoryLocation cvsroot, IServerConnection serverConnection) { 50 fCVSRoot = cvsroot; 51 this.serverConnection = serverConnection; 52 fServerEncoding = getEncoding(fCVSRoot); 53 } 54 55 private static byte[] append(byte[] buffer, int index, byte b) { 56 if (index >= buffer.length) { 57 byte[] newBuffer= new byte[index * 2]; 58 System.arraycopy(buffer, 0, newBuffer, 0, buffer.length); 59 buffer= newBuffer; 60 } 61 buffer[index]= b; 62 return buffer; 63 } 64 67 public void close() { 68 if (!isEstablished()) 69 return; 70 try { 71 serverConnection.close(); 72 } catch (IOException ex) { 73 if (CVSProviderPlugin.getPlugin().isDebugging()) { 76 CVSProviderPlugin.log(new CVSCommunicationException(CVSMessages.Connection_cannotClose, fCVSRoot, ex)); 77 } 78 } finally { 79 fResponseStream = null; 80 fIsEstablished = false; 81 } 82 } 83 86 public void flush() throws CVSException { 87 if (!isEstablished()) 88 return; 89 try { 90 getOutputStream().flush(); 91 } catch(IOException e) { 92 throw new CVSCommunicationException(fCVSRoot,e); 93 } 94 } 95 96 100 public OutputStream getOutputStream() { 101 if (!isEstablished()) 102 return null; 103 return serverConnection.getOutputStream(); 104 } 105 109 public InputStream getInputStream() { 110 if (!isEstablished()) 111 return null; 112 if (fResponseStream == null) 113 fResponseStream = serverConnection.getInputStream(); 114 return fResponseStream; 115 } 116 117 121 public boolean isEstablished() { 122 return fIsEstablished; 123 } 124 125 128 public void open(IProgressMonitor monitor) throws CVSException { 129 if (isEstablished()) 130 return; 131 try { 132 serverConnection.open(monitor); 133 } catch (IOException e) { 134 throw new CVSCommunicationException(NLS.bind(CVSMessages.Connection_0, new String [] { fCVSRoot.getLocation(true), CVSCommunicationException.getMessageFor(e) }), fCVSRoot, e); 135 } 136 fIsEstablished= true; 137 } 138 141 public String readLine() throws CVSException { 142 if (!isEstablished()) 143 throw new CVSCommunicationException(CVSMessages.Connection_readUnestablishedConnection,fCVSRoot,null); 144 try { 145 InputStream in = getInputStream(); 146 int index = 0; 147 int r; 148 while ((r = in.read()) != -1) { 149 if (r == NEWLINE) break; 150 readLineBuffer = append(readLineBuffer, index++, (byte) r); 151 } 152 153 String result = new String (readLineBuffer, 0, index, fServerEncoding); 154 if (Policy.isDebugProtocol()) Policy.printProtocolLine(result); 155 return result; 156 } catch (IOException e) { 157 throw new CVSCommunicationException(fCVSRoot,e); 158 } 159 } 160 161 static String readLine(ICVSRepositoryLocation location, InputStream in) throws IOException { 162 byte[] buffer = new byte[256]; 163 int index = 0; 164 int r; 165 while ((r = in.read()) != -1) { 166 if (r == NEWLINE) 167 break; 168 buffer = append(buffer, index++, (byte) r); 169 } 170 171 String result = new String (buffer, 0, index, getEncoding(location)); 172 if (Policy.isDebugProtocol()) 173 Policy.printProtocolLine(result); 174 return result; 175 } 176 177 179 182 public void write(String s) throws CVSException { 183 try { 184 write(s.getBytes(fServerEncoding), false); 185 } catch (UnsupportedEncodingException e) { 186 IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.SERVER_ERROR, e.getMessage(), e, fCVSRoot); 187 throw new CVSException (status); 188 } 189 } 190 191 195 public static String getEncoding(ICVSRepositoryLocation location) { 196 return location.getEncoding(); 197 } 198 199 202 public void writeLine(String s) throws CVSException { 203 try { 204 write(s.getBytes(fServerEncoding), true); 205 } catch (UnsupportedEncodingException e) { 206 IStatus status = new CVSStatus(IStatus.ERROR, CVSStatus.SERVER_ERROR, e.getMessage(), e, fCVSRoot); 207 throw new CVSException (status); 208 } 209 } 210 211 void write (byte[] bytes, boolean newLine) throws CVSException { 212 write(bytes, 0, bytes.length, newLine); 213 } 214 215 219 void write(byte[] b, int off, int len, boolean newline) throws CVSException { 220 if (!isEstablished()) 221 throw new CVSCommunicationException(CVSMessages.Connection_writeUnestablishedConnection,fCVSRoot,null); 222 223 if (Policy.isDebugProtocol()) 224 Policy.printProtocol(new String (b, off, len), newline); 225 226 try { 227 OutputStream out= getOutputStream(); 228 out.write(b, off, len); 229 if (newline) 230 out.write(NEWLINE); 231 232 } catch (IOException e) { 233 throw new CVSCommunicationException(fCVSRoot,e); 234 } 235 } 236 } 237 | Popular Tags |