publicclassSimpleHttpServer{publicstaticvoidmain(String[]args)throwsException{HttpServerserver=HttpServer.create(newInetSocketAddress(8000),0);server.createContext("/test",newMyHandler());server.setExecutor(null);// creates a default executor
server.start();}staticclassMyHandlerimplementsHttpHandler{@Overridepublicvoidhandle(HttpExchanget)throwsIOException{Stringresponse="This is the response";Headersheaders=t.getResponseHeaders();headers.add("Connection","keep-alive");t.sendResponseHeaders(200,response.length());OutputStreamos=t.getResponseBody();os.write(response.getBytes());os.close();}}}
Socketsocket=newSocket("localhost",9000);InputStreamin=socket.getInputStream();while(true){intread=in.read();if(read==-1){// 这个 -1 就是 server close 连接后发出的
in.close();break;}System.out.println(read);}
也就是说, 主动关闭方如果 close 了, 那么被动关闭方这边状态会变为 close_wait, 同时收到 -1, 然后我们需要手动调用 socket.close().
publicclassRSAEncryptionimplementsEncryption{privatestaticfinalStringALGORITHM="RSA";privatePublicKeypublicKey;privatePrivateKeyprivateKey;publicRSAEncryption(StringpublicKeyPath,StringprivateKeyPath)throwsIOException,ClassNotFoundException,InvalidKeyException,NoSuchPaddingException,NoSuchAlgorithmException,InvalidKeySpecException{this.publicKey=EncryptionUtils.loadRSAPublicKey(publicKeyPath);this.privateKey=EncryptionUtils.loadRSAPrivateKey(privateKeyPath);// fail fast
Ciphercipher=Cipher.getInstance(ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE,publicKey);cipher=Cipher.getInstance(ALGORITHM);cipher.init(Cipher.DECRYPT_MODE,privateKey);}@OverridepublicStringencrypt(Stringsource)throwsEncryptException{Ciphercipher;try{cipher=Cipher.getInstance(ALGORITHM);cipher.init(Cipher.ENCRYPT_MODE,publicKey);byte[]bytes=source.getBytes(Charsets.UTF_8);returnBase64.encode(cipher.doFinal(bytes));}catch(Exceptione){thrownewEncryptException(e);}}@OverridepublicStringdecrypt(Stringsource)throwsDecryptException{Ciphercipher;try{cipher=Cipher.getInstance(ALGORITHM);cipher.init(Cipher.DECRYPT_MODE,privateKey);returnnewString(cipher.doFinal(Base64.decode(source)),Charsets.UTF_8);}catch(Exceptione){thrownewDecryptException(e);}}}
<?xml version="1.0"?><settingsxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0http://maven.apache.org/xsd/settings-1.0.0.xsd"><servers><server><id>snapshots</id><username>{username}</username><password>{passwd}</password><filePermissions>664</filePermissions><directoryPermissions>775</directoryPermissions></server></servers><profiles><profile><id>MyProfile</id><repositories><repository><id>Nexus</id><url>http://test.com/nexus/content/groups/public</url><releases><enabled>true</enabled><!-- always , daily (default), interval:X (where X is an integer in minutes) or never.--><updatePolicy>daily</updatePolicy><checksumPolicy>warn</checksumPolicy></releases><snapshots><updatePolicy>always</updatePolicy></snapshots></repository></repositories><pluginRepositories><pluginRepository><id>Nexus</id><url>http://test.com/nexus/content/groups/public</url><releases><enabled>true</enabled><checksumPolicy>warn</checksumPolicy></releases><snapshots><updatePolicy>always</updatePolicy></snapshots></pluginRepository></pluginRepositories></profile></profiles><activeProfiles><activeProfile>MyProfile</activeProfile></activeProfiles></settings>
If S is a subtype of T, then objects of type T may be replaced with objects of type S (i.e., objects of type S may substitute objects of type T) without altering any of the desirable properties of that program (correctness, task performed, etc.)
Humanh=newHuman("Jack");Manm=newMan("Jack",20);Set<Human>hSet=newHashSet<Human>();hSet.add(h);System.out.println("Set contains Human : "+hSet.contains(h));System.out.println("Set contains Man : "+hSet.contains(m));
Set contains Human : true
Set contains Man : false
容易发现这里 h.equals(m) 为 true, 但是却不能用 m 替换掉 h 来做查询.
这个道理用在 compareTo 上也是一样的, 如果子类的 compareTo 覆盖了父类的 compareTo, 并且加入了新的元素作为比较元素, 则也会违反对称性原则, 导致 a.compareTo(b) > 0, b.compareTo(a) < 0. 同时也会违反里氏代换原则, 导致在 TreeSet 中插入 a 或 a 的子类的时候顺序发生变化.