1 19 package com.mysql.jdbc; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.io.RandomAccessFile ; 25 26 import java.net.Socket ; 27 import java.net.SocketException ; 28 29 import java.util.Properties ; 30 31 32 37 public class NamedPipeSocketFactory implements SocketFactory { 38 private static final String NAMED_PIPE_PROP_NAME = "namedPipePath"; 39 private Socket namedPipeSocket; 40 41 44 public NamedPipeSocketFactory() { 45 super(); 46 } 47 48 51 public Socket afterHandshake() throws SocketException , IOException { 52 return this.namedPipeSocket; 53 } 54 55 58 public Socket beforeHandshake() throws SocketException , IOException { 59 return this.namedPipeSocket; 60 } 61 62 65 public Socket connect(String host, Properties props) 66 throws SocketException , IOException { 67 String namedPipePath = props.getProperty(NAMED_PIPE_PROP_NAME); 68 69 if (namedPipePath == null) { 70 namedPipePath = "\\\\.\\pipe\\MySQL"; 71 } else if (namedPipePath.length() == 0) { 72 throw new SocketException ( 73 "Can not specify NULL or empty value for property '" 74 + NAMED_PIPE_PROP_NAME + "'."); 75 } 76 77 this.namedPipeSocket = new NamedPipeSocket(namedPipePath); 78 79 return this.namedPipeSocket; 80 } 81 82 85 class NamedPipeSocket extends Socket { 86 private RandomAccessFile namedPipeFile; 87 private boolean isClosed = false; 88 89 NamedPipeSocket(String filePath) throws IOException { 90 if ((filePath == null) || (filePath.length() == 0)) { 91 throw new IOException ( 92 "Named pipe path can not be null or empty"); 93 } 94 95 this.namedPipeFile = new RandomAccessFile (filePath, "rw"); 96 } 97 98 101 public boolean isClosed() { 102 return this.isClosed; 103 } 104 105 108 public InputStream getInputStream() throws IOException { 109 return new RandomAccessFileInputStream(this.namedPipeFile); 110 } 111 112 115 public OutputStream getOutputStream() throws IOException { 116 return new RandomAccessFileOutputStream(this.namedPipeFile); 117 } 118 119 122 public synchronized void close() throws IOException { 123 this.namedPipeFile.close(); 124 this.isClosed = true; 125 } 126 } 127 128 131 class RandomAccessFileInputStream extends InputStream { 132 RandomAccessFile raFile; 133 134 RandomAccessFileInputStream(RandomAccessFile file) { 135 this.raFile = file; 136 } 137 138 141 public int available() throws IOException { 142 return -1; 143 } 144 145 148 public void close() throws IOException { 149 this.raFile.close(); 150 } 151 152 155 public int read() throws IOException { 156 return this.raFile.read(); 157 } 158 159 162 public int read(byte[] b, int off, int len) throws IOException { 163 return this.raFile.read(b, off, len); 164 } 165 166 169 public int read(byte[] b) throws IOException { 170 return this.raFile.read(b); 171 } 172 } 173 174 177 class RandomAccessFileOutputStream extends OutputStream { 178 RandomAccessFile raFile; 179 180 RandomAccessFileOutputStream(RandomAccessFile file) { 181 this.raFile = file; 182 } 183 184 187 public void close() throws IOException { 188 this.raFile.close(); 189 } 190 191 194 public void write(byte[] b, int off, int len) throws IOException { 195 this.raFile.write(b, off, len); 196 } 197 198 201 public void write(byte[] b) throws IOException { 202 this.raFile.write(b); 203 } 204 205 208 public void write(int b) throws IOException { 209 } 210 } 211 } 212 | Popular Tags |