日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

spring源码分析-core.io包里面的类

發布時間:2023/12/10 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring源码分析-core.io包里面的类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前些日子看《深入理解javaweb開發》時,看到第一章java的io流,發覺自己對io流真的不是很熟悉。然后看了下JDK1.7中io包的一點點代碼,又看了org.springframework.core.io包的一些類和組織方式,當作是學習吧。總結一下。

先掛下spring.core.io包的類圖,其中接口是方框表示,抽象類帶了abstract前綴,剩下那個兩個框重貼的則代表實現類。沒怎么劃過類圖,如果有好的畫類圖工具請推薦給我。

畫得不好的地方就見諒了。注:以下源碼匹配的是spring-core-4.1.6Release.jar里面的org.springframework.core.io包。

先看處于最上層的接口,叫InputStreamSource,里面只有一個抽象方法

接下是resources,這個接口我們一般都會用到,貼源碼:

public interface Resource extends InputStreamSource {boolean exists();//文件是否存在boolean isReadable();//是否可讀boolean isOpen();//資源是否被一個inputstream打開,如果已被打開,則不允許其他流再打開URL getURL() throws IOException;//獲取資源url路徑,當不能以url描述時拋出ioException異常URI getURI() throws IOException;//獲取資源uri路徑,當不能以url描述時拋出ioException異常File getFile() throws IOException;//獲取file,file在IO流中僅僅是一個指向作用long contentLength() throws IOException;//資源的字節長度,可以拿來算資源的大小long lastModified() throws IOException;//資源最后修改時間Resource createRelative(String relativePath) throws IOException;//根據資源相對路徑創建資源String getFilename();//返回文件名String getDescription();//資源描述}

后面是abstractResource,這是個挺重要的類,主要是對resource接口的基本實現,

public abstract class AbstractResource implements Resource {@Overridepublic boolean exists() {//看是否能在硬盤上找到try {return getFile().exists();}catch (IOException ex) {// 試一下能不能打開輸出流try {InputStream is = getInputStream();is.close();return true;}catch (Throwable isEx) {return false;}}}/*** 一般都是可讀的,所以默認方法是true*/@Overridepublic boolean isReadable() {return true;}/*** 默認值是false,沒有inputStream來讀時的默認狀態*/@Overridepublic boolean isOpen() {return false;}/*** url這個屬性一般只出現在web的IO資源中,網絡resource需要override這個方法,其他類型資源敢訪問這個方法,拋個異常給他*/@Overridepublic URL getURL() throws IOException {throw new FileNotFoundException(getDescription() + " cannot be resolved to URL");}@Overridepublic URI getURI() throws IOException {URL url = getURL();try {//url可以轉成uri,uri不能轉urlreturn ResourceUtils.toURI(url);}catch (URISyntaxException ex) {throw new NestedIOException("Invalid URI [" + url + "]", ex);}}/*** JDK的File類都是返回絕對路徑的File,當一個資源沒有絕對路徑時,拋個異常給它沒毛病*/@Overridepublic File getFile() throws IOException {throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path");}/*** inputStream讀取byte[]字節流,并返回該數組的長度,相當于把文件讀了一次*/@Overridepublic long contentLength() throws IOException {InputStream is = this.getInputStream();//Assert應該是叫斷言Assert.state(is != null, "resource input stream must not be null");try {long size = 0;byte[] buf = new byte[255];int read;while ((read = is.read(buf)) != -1) {size += read;}return size;}finally {try {//一定要在finally中關閉流is.close();}catch (IOException ex) {}}}/*** 返回getFileForLastModifiedCheck().lastModified()的值,該職為0L,拋個異常給他沒毛病*/@Overridepublic long lastModified() throws IOException {long lastModified = getFileForLastModifiedCheck().lastModified();if (lastModified == 0L) {throw new FileNotFoundException(getDescription() +" cannot be resolved in the file system for resolving its last-modified timestamp");}return lastModified;}/*** 調用getFile方法*/protected File getFileForLastModifiedCheck() throws IOException {return getFile();}/*** 直接假定相對資源創建不了,再拋個異常給他*/@Overridepublic Resource createRelative(String relativePath) throws IOException {throw new FileNotFoundException("Cannot create a relative resource for " + getDescription());}/*** 又是一個假定,假定filename文件名為null*/@Overridepublic String getFilename() {return null;}/*** 很簡單,不解釋*/@Overridepublic String toString() {return getDescription();}/*** 判斷兩文件是否相等*/@Overridepublic boolean equals(Object obj) {return (obj == this ||(obj instanceof Resource && ((Resource) obj).getDescription().equals(getDescription())));}/*** 返回hashCode*/@Overridepublic int hashCode() {return getDescription().hashCode();}}

至于其他實現類其實都是在接口和抽象類的基礎上去拓展,所以我并不是讀得很仔細,先這樣子吧。

小總結:其實為什么要分好幾層去繼承剛開始我是很不懂的,后面看了源代碼和小伙伴的博文后便豁然開朗了。

1.resources是高度抽象的接口,里面是對所有資源文件的具體方法抽象,但是并不是每個資源都有這個抽象里面的所有方法,所以abstractResource對其進行了一般的實現,

對于一些并不是所有的Resources都會有方法,例如非網絡資源沒有url和uri屬性,默認方法就直接拋異常了,簡單粗暴。舉個栗子,不是所有的動物都會游泳,一只豬想游泳,先拋個異常給豬接著先。

2.resources里面有isOpen(),isReadAble()接口,這是個小技巧吧,類似與模版方法模式的鉤子方法,也是很值得我們學習的。

總結

以上是生活随笔為你收集整理的spring源码分析-core.io包里面的类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。