├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src ├── main └── java │ └── fastdfs │ └── client │ ├── FastdfsClient.java │ ├── FastdfsConstants.java │ ├── FastdfsDataOverflowException.java │ ├── FastdfsException.java │ ├── FastdfsExecutor.java │ ├── FastdfsHandler.java │ ├── FastdfsOperation.java │ ├── FastdfsPool.java │ ├── FastdfsPoolGroup.java │ ├── FastdfsReadTimeoutException.java │ ├── FastdfsScheduler.java │ ├── FastdfsSettings.java │ ├── FastdfsTimeoutException.java │ ├── FastdfsUtils.java │ ├── FileId.java │ ├── FileInfo.java │ ├── FileMetadata.java │ ├── StorageClient.java │ ├── StorageServer.java │ ├── TrackerClient.java │ ├── TrackerMonitor.java │ ├── TrackerNotAvailableException.java │ ├── TrackerSelector.java │ ├── TrackerServer.java │ ├── codec │ ├── ActiveTestReplier.java │ ├── ActiveTestRequestor.java │ ├── DownloadStorageGetEncoder.java │ ├── DownloadStorageListEncoder.java │ ├── FileAppendEncoder.java │ ├── FileDeleteEncoder.java │ ├── FileDownloadEncoder.java │ ├── FileIdDecoder.java │ ├── FileIdOperationEncoder.java │ ├── FileInfoDecoder.java │ ├── FileInfoGetEncoder.java │ ├── FileMetadataDecoder.java │ ├── FileMetadataGetEncoder.java │ ├── FileMetadataSetEncoder.java │ ├── FileModifyEncoder.java │ ├── FileOperationEncoder.java │ ├── FileTruncateEncoder.java │ ├── FileUploadAppenderEncoder.java │ ├── FileUploadEncoder.java │ ├── StorageServerDecoder.java │ ├── StorageServerListDecoder.java │ ├── UpdateStorageGetEncoder.java │ └── UploadStorageGetEncoder.java │ └── exchange │ ├── Replier.java │ ├── ReplierDecoder.java │ ├── ReplierSupport.java │ ├── Requestor.java │ ├── RequestorEncoder.java │ ├── RequestorSupport.java │ └── StreamReplier.java └── test ├── java └── fastdfs │ └── client │ └── FastdfsClientTest.java └── resources └── logback.xml /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | .settings/ 3 | .project 4 | .classpath 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # fastdfs-client 2 | 3 | fastdfs-client is a [FastDFS](https://github.com/happyfish100/fastdfs) java client 4 | based on [Netty 4](http://netty.io) . support multiple tracker server pool and active tracker server health check . 5 | 6 | ## Requirements 7 | 8 | * Java 8+ 9 | 10 | ## Usage 11 | 12 | ```java 13 | 14 | // FastdfsClient is threadsafe and use connection pool. 15 | FastdfsClient client = FastdfsClient.newBuilder() 16 | .connectTimeout(3000) 17 | .readTimeout(100) 18 | .healthCheck(3, 2, 3000, 10000) 19 | .tracker("192.168.1.2", 22222) 20 | .build(); 21 | 22 | // upload file 23 | CompletableFuture promise = client.upload(new File("/tmp/test.dmg")); 24 | FileId fileId = promise.get(); 25 | // do something. 26 | 27 | // download file 28 | OutputStream out = ... 29 | CompletableFuture promise = client.download(fileId, out); 30 | // promise.whenComplete(...); 31 | 32 | // delete file 33 | CompletableFuture promise = client.delete(fileId); 34 | // promise.whenComplete(...); 35 | 36 | // get file info 37 | CompletableFuture promise = client.infoGet(fileId); 38 | FileInfo fileInfo = promise.get(); 39 | // do something. 40 | 41 | // set file metadata 42 | FileMetadata metadata = FileMetadata.newBuilder().put("test", "test1").build(); 43 | CompletableFuture promise = client.metadataSet(fileId, metadata); 44 | // do something. 45 | 46 | // get file metadata 47 | CompletableFuture promise = client.metadataGet(fileId); 48 | FileMetadata metadata = promise.get(); 49 | // do something. 50 | 51 | client.close(); 52 | ``` 53 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | fastdfs 5 | fastdfs-client 6 | 2.0.0-SNAPSHOT 7 | 8 | fastdfs:fastdfs-client 9 | fastdfs-client is a FastDFS java client based on Netty 4. 10 | http://github.com/coding4m/fastdfs-client 11 | 12 | 13 | 14 | The Apache License, Version 2.0 15 | http://www.apache.org/licenses/LICENSE-2.0.txt 16 | 17 | 18 | 19 | 20 | https://gitee.net/workplus/fastdfs-client.git 21 | 22 | 23 | 24 | 1.8 25 | UTF-8 26 | UTF-8 27 | 1.7.18 28 | 29 | 30 | 31 | 32 | dddlib-releases 33 | dddlib release repository 34 | dav:http://nexus.workplus.io:8081/repository/maven-releases 35 | 36 | 37 | dddlib-snapshots 38 | dddlib snapshot repository 39 | dav:http://nexus.workplus.io:8081/repository/maven-snapshots 40 | 41 | 42 | 43 | 44 | 45 | dddlib-releases 46 | http://nexus.workplus.io:8081/repository/maven-releases 47 | 48 | true 49 | 50 | 51 | false 52 | 53 | 54 | 55 | dddlib-snapshots 56 | http://nexus.workplus.io:8081/repository/maven-snapshots 57 | 58 | false 59 | 60 | 61 | always 62 | true 63 | 64 | 65 | 66 | 67 | 68 | 69 | dddlib-releases 70 | http://nexus.workplus.io:8081/repository/maven-releases 71 | 72 | true 73 | 74 | 75 | false 76 | 77 | 78 | 79 | dddlib-snapshots 80 | http://nexus.workplus.io:8081/repository/maven-snapshots 81 | 82 | false 83 | 84 | 85 | true 86 | 87 | 88 | 89 | 90 | 91 | 92 | io.netty 93 | netty-all 94 | 4.1.51.Final 95 | 96 | 97 | org.slf4j 98 | slf4j-api 99 | ${slf4j.version} 100 | 101 | 102 | ch.qos.logback 103 | logback-classic 104 | 1.2.0 105 | test 106 | 107 | 108 | junit 109 | junit 110 | 4.13.1 111 | test 112 | 113 | 114 | 115 | 116 | 117 | org.apache.maven.plugins 118 | maven-resources-plugin 119 | 2.7 120 | 121 | ${project.build.sourceEncoding} 122 | 123 | less 124 | otf 125 | eot 126 | ttf 127 | svg 128 | woff 129 | js 130 | css 131 | jpg 132 | swf 133 | png 134 | xls 135 | xlsx 136 | 137 | 138 | 139 | 140 | org.apache.maven.plugins 141 | maven-deploy-plugin 142 | 2.8.2 143 | 144 | 145 | commons-logging 146 | commons-logging 147 | 1.2 148 | 149 | 150 | org.apache.maven.wagon 151 | wagon-webdav-jackrabbit 152 | 2.8 153 | 154 | 155 | 156 | 157 | org.apache.maven.plugins 158 | maven-compiler-plugin 159 | 3.3 160 | 161 | ${java.version} 162 | ${java.version} 163 | 164 | 165 | 166 | org.apache.maven.plugins 167 | maven-source-plugin 168 | 169 | 170 | attach-sources 171 | 172 | jar-no-fork 173 | 174 | package 175 | 176 | 177 | 178 | 179 | org.apache.maven.plugins 180 | maven-javadoc-plugin 181 | 182 | 183 | javadoc 184 | 185 | jar 186 | 187 | package 188 | 189 | 190 | 191 | 192 | 193 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsClient.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | import java.io.ByteArrayOutputStream; 7 | import java.io.Closeable; 8 | import java.io.File; 9 | import java.io.IOException; 10 | import java.util.HashSet; 11 | import java.util.LinkedList; 12 | import java.util.List; 13 | import java.util.Objects; 14 | import java.util.concurrent.CompletableFuture; 15 | import java.util.concurrent.TimeUnit; 16 | 17 | /** 18 | * @author siuming 19 | */ 20 | public final class FastdfsClient implements Closeable { 21 | 22 | public static final long DEFAULT_CONNECT_TIMEOUT = 3000; 23 | public static final long DEFAULT_READ_TIMEOUT = 60000; 24 | public static final long DEFAULT_IDLE_TIMEOUT = 60000; 25 | 26 | public static final int DEFAULT_MAX_THREADS = 0; 27 | public static final int DEFAULT_MAX_CONN_PER_HOST = 100; 28 | public static final int DEFAULT_MAX_PENDING_REQUESTS = 256; 29 | 30 | public static final int DEFAULT_HEALTH_RISE = 2; 31 | public static final int DEFAULT_HEALTH_FALL = 3; 32 | public static final long DEFAULT_CHECK_TIMEOUT = 3000; 33 | public static final long DEFAULT_CHECK_INTERVAL = 10000; 34 | 35 | private final FastdfsExecutor executor; 36 | private final FastdfsScheduler scheduler; 37 | 38 | private final TrackerClient trackerClient; 39 | private final StorageClient storageClient; 40 | 41 | private FastdfsClient(Builder builder) { 42 | 43 | FastdfsSettings settings = new FastdfsSettings( 44 | builder.connectTimeout, 45 | builder.readTimeout, 46 | builder.idleTimeout, 47 | builder.maxThreads, 48 | builder.maxConnPerHost, 49 | builder.maxPendingRequests 50 | ); 51 | 52 | this.executor = new FastdfsExecutor(settings); 53 | this.scheduler = new FastdfsScheduler(new HashSet<>(builder.trackers).size()); 54 | this.trackerClient = new TrackerClient(executor, scheduler, builder.selector, builder.trackers, builder.fall, builder.rise, builder.checkTimeout, builder.checkInterval); 55 | this.storageClient = new StorageClient(executor); 56 | } 57 | 58 | /** 59 | * @param file 60 | * @return 61 | */ 62 | public CompletableFuture upload(File file) { 63 | return upload(null, file); 64 | } 65 | 66 | 67 | /** 68 | * @param group 69 | * @param file 70 | * @return 71 | */ 72 | public CompletableFuture upload(String group, File file) { 73 | Objects.requireNonNull(file, "file must not be null."); 74 | return trackerClient 75 | .uploadStorageGet(group) 76 | .thenCompose(server -> storageClient.upload(server, file)); 77 | } 78 | 79 | /** 80 | * @param filename 81 | * @param content 82 | * @return 83 | */ 84 | public CompletableFuture upload(String filename, byte[] content) { 85 | return upload(null, filename, content); 86 | } 87 | 88 | /** 89 | * @param filename 90 | * @param content 91 | * @param metadata 92 | * @return 93 | */ 94 | public CompletableFuture upload(String filename, byte[] content, FileMetadata metadata) { 95 | return upload(null, filename, content, metadata); 96 | } 97 | 98 | /** 99 | * @param group 100 | * @param filename 101 | * @param content 102 | * @return 103 | */ 104 | public CompletableFuture upload(String group, String filename, byte[] content) { 105 | return upload(group, content, filename, content.length); 106 | } 107 | 108 | /** 109 | * @param group 110 | * @param filename 111 | * @param content 112 | * @param metadata 113 | * @return 114 | */ 115 | public CompletableFuture upload(String group, String filename, byte[] content, FileMetadata metadata) { 116 | return upload(group, content, filename, content.length, metadata); 117 | } 118 | 119 | 120 | /** 121 | * @param file 122 | * @param metadata 123 | * @return 124 | */ 125 | public CompletableFuture upload(File file, FileMetadata metadata) { 126 | return upload(null, file, metadata); 127 | } 128 | 129 | /** 130 | * @param group 131 | * @param file 132 | * @param metadata 133 | * @return 134 | */ 135 | public CompletableFuture upload(String group, File file, FileMetadata metadata) { 136 | Objects.requireNonNull(file, "file must not be null."); 137 | Objects.requireNonNull(metadata, "metadata must not be null."); 138 | return upload(group, file).thenApply(fileId -> { 139 | metadataSet(fileId, metadata); 140 | return fileId; 141 | }); 142 | } 143 | 144 | /** 145 | * @param content 146 | * @param filename 147 | * @param size 148 | * @return 149 | */ 150 | public CompletableFuture upload(Object content, String filename, long size) { 151 | return upload(null, content, filename, size); 152 | } 153 | 154 | /** 155 | * 上传文件,其中文件内容字段 content 的支持以下类型: 156 | *

157 | *

    158 | *
  • byte[]
  • 159 | *
  • {@link java.io.File}
  • 160 | *
  • {@link java.io.InputStream}
  • 161 | *
  • {@link java.nio.channels.ReadableByteChannel}
  • 162 | *
163 | * 164 | * @param group 分组 165 | * @param content 上传内容 166 | * @param size 内容长度 167 | * @param filename 扩展名 168 | * @return 169 | */ 170 | public CompletableFuture upload(String group, Object content, String filename, long size) { 171 | Objects.requireNonNull(content, "content must not be null."); 172 | Objects.requireNonNull(filename, "filename must not be null."); 173 | return trackerClient 174 | .uploadStorageGet(group) 175 | .thenCompose(server -> storageClient.upload(server, content, filename, size)); 176 | } 177 | 178 | /** 179 | * @param content 180 | * @param filename 181 | * @param size 182 | * @param metadata 183 | * @return 184 | */ 185 | public CompletableFuture upload(Object content, String filename, long size, FileMetadata metadata) { 186 | return upload(null, content, filename, size, metadata); 187 | } 188 | 189 | /** 190 | * @param group 191 | * @param content 192 | * @param filename 193 | * @param size 194 | * @param metadata 195 | * @return 196 | */ 197 | public CompletableFuture upload(String group, Object content, String filename, long size, FileMetadata metadata) { 198 | Objects.requireNonNull(content, "content must not be null."); 199 | Objects.requireNonNull(filename, "filename must not be null."); 200 | Objects.requireNonNull(metadata, "metadata must not be null."); 201 | return upload(group, content, filename, size).thenApply(fileId -> { 202 | metadataSet(fileId, metadata); 203 | return fileId; 204 | }); 205 | } 206 | 207 | /** 208 | * @param file 209 | * @return 210 | */ 211 | public CompletableFuture uploadAppender(File file) { 212 | return uploadAppender(null, file); 213 | } 214 | 215 | /** 216 | * @param file 217 | * @param metadata 218 | * @return 219 | */ 220 | public CompletableFuture uploadAppender(File file, FileMetadata metadata) { 221 | return uploadAppender(null, file, metadata); 222 | } 223 | 224 | /** 225 | * @param group 226 | * @param file 227 | * @return 228 | */ 229 | public CompletableFuture uploadAppender(String group, File file) { 230 | Objects.requireNonNull(file, "file must not be null."); 231 | return trackerClient 232 | .uploadStorageGet(group) 233 | .thenCompose(server -> storageClient.uploadAppender(server, file)); 234 | } 235 | 236 | /** 237 | * @param group 238 | * @param file 239 | * @return 240 | */ 241 | public CompletableFuture uploadAppender(String group, File file, FileMetadata metadata) { 242 | return uploadAppender(group, file) 243 | .thenApply(fileId -> { 244 | metadataSet(fileId, metadata); 245 | return fileId; 246 | }); 247 | } 248 | 249 | /** 250 | * @param filename 251 | * @param content 252 | * @return 253 | */ 254 | public CompletableFuture uploadAppender(String filename, byte[] content) { 255 | return uploadAppender(null, content, filename, content.length); 256 | } 257 | 258 | /** 259 | * @param group 260 | * @param filename 261 | * @param content 262 | * @return 263 | */ 264 | public CompletableFuture uploadAppender(String group, String filename, byte[] content) { 265 | return uploadAppender(group, content, filename, content.length); 266 | } 267 | 268 | /** 269 | * @param group 270 | * @param filename 271 | * @param content 272 | * @param metadata 273 | * @return 274 | */ 275 | public CompletableFuture uploadAppender(String group, String filename, byte[] content, FileMetadata metadata) { 276 | return uploadAppender(group, content, filename, content.length, metadata); 277 | } 278 | 279 | /** 280 | * @param content 281 | * @param filename 282 | * @param size 283 | * @return 284 | */ 285 | public CompletableFuture uploadAppender(Object content, String filename, long size) { 286 | return uploadAppender(null, content, filename, size); 287 | } 288 | 289 | 290 | /** 291 | * @param content 292 | * @param filename 293 | * @param size 294 | * @param metadata 295 | * @return 296 | */ 297 | public CompletableFuture uploadAppender(Object content, String filename, long size, FileMetadata metadata) { 298 | return uploadAppender(null, content, filename, size, metadata); 299 | } 300 | 301 | /** 302 | * 上传可追加文件,其中文件内容字段 content 的支持以下类型: 303 | *

304 | *

    305 | *
  • byte[]
  • 306 | *
  • {@link java.io.File}
  • 307 | *
  • {@link java.io.InputStream}
  • 308 | *
  • {@link java.nio.channels.ReadableByteChannel}
  • 309 | *
310 | * 311 | * @param group 分组 312 | * @param content 上传内容 313 | * @param filename 文件名 314 | * @param size 内容长度 315 | * @return 316 | */ 317 | public CompletableFuture uploadAppender(String group, Object content, String filename, long size) { 318 | Objects.requireNonNull(content, "content must not be null."); 319 | Objects.requireNonNull(filename, "filename must not be null."); 320 | return trackerClient 321 | .uploadStorageGet(group) 322 | .thenCompose(server -> storageClient.uploadAppender(server, content, filename, size)); 323 | } 324 | 325 | /** 326 | * @param group 327 | * @param content 328 | * @param filename 329 | * @param size 330 | * @param metadata 331 | * @return 332 | */ 333 | public CompletableFuture uploadAppender(String group, Object content, String filename, long size, FileMetadata metadata) { 334 | return uploadAppender(group, content, filename, size) 335 | .thenApply(fileId -> { 336 | metadataSet(fileId, metadata); 337 | return fileId; 338 | }); 339 | } 340 | 341 | public CompletableFuture download(String fileId) { 342 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 343 | return download(fileId, out).thenApply(res -> out.toByteArray()); 344 | } 345 | 346 | /** 347 | * @param fileId 348 | * @param out 349 | * @return 350 | */ 351 | public CompletableFuture download(String fileId, Object out) { 352 | return download(FileId.fromString(fileId), out); 353 | } 354 | 355 | 356 | /** 357 | * @param fileId 358 | * @return 359 | */ 360 | public CompletableFuture download(FileId fileId) { 361 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 362 | return download(fileId, out).thenApply(res -> out.toByteArray()); 363 | } 364 | 365 | /** 366 | * 下载文件,其输出 output 参数支持以下类型 367 | *

368 | *

    369 | *
  • {@link java.io.OutputStream}
  • 370 | *
  • {@link java.nio.channels.GatheringByteChannel}
  • 371 | *
372 | * 373 | * @param fileId 服务器存储路径 374 | * @param out 输出流 375 | * @return 376 | */ 377 | public CompletableFuture download(FileId fileId, Object out) { 378 | Objects.requireNonNull(fileId, "fileId must not be null."); 379 | Objects.requireNonNull(out, "out must not be null."); 380 | return trackerClient 381 | .downloadStorageGet(fileId) 382 | .thenCompose(server -> storageClient.download(server, fileId, out)); 383 | } 384 | 385 | /** 386 | * @param fileId 387 | * @param offset 388 | * @param size 389 | * @return 390 | */ 391 | public CompletableFuture download(String fileId, long offset, long size) { 392 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 393 | return download(fileId, out, offset, size).thenApply(res -> out.toByteArray()); 394 | } 395 | 396 | /** 397 | * @param fileId 398 | * @param out 399 | * @param offset 400 | * @param size 401 | * @return 402 | */ 403 | public CompletableFuture download(String fileId, Object out, long offset, long size) { 404 | return download(FileId.fromString(fileId), out, offset, size); 405 | } 406 | 407 | /** 408 | * @param fileId 409 | * @param offset 410 | * @param size 411 | * @return 412 | */ 413 | public CompletableFuture download(FileId fileId, long offset, long size) { 414 | ByteArrayOutputStream out = new ByteArrayOutputStream(); 415 | return download(fileId, out, offset, size).thenApply(res -> out.toByteArray()); 416 | } 417 | 418 | /** 419 | * @param fileId 420 | * @param out 421 | * @param offset 422 | * @param size 423 | * @return 424 | */ 425 | public CompletableFuture download(FileId fileId, Object out, long offset, long size) { 426 | Objects.requireNonNull(fileId, "fileId must not be null."); 427 | Objects.requireNonNull(out, "out must not be null."); 428 | return trackerClient 429 | .downloadStorageGet(fileId) 430 | .thenCompose(server -> storageClient.download(server, fileId, out, offset, size)); 431 | } 432 | 433 | /** 434 | * @param fileId 435 | * @return 436 | */ 437 | public CompletableFuture delete(String fileId) { 438 | return delete(FileId.fromString(fileId)); 439 | } 440 | 441 | /** 442 | * @param fileId 443 | * @return 444 | */ 445 | public CompletableFuture delete(FileId fileId) { 446 | Objects.requireNonNull(fileId, "fileId must not be null."); 447 | return trackerClient 448 | .updateStorageGet(fileId) 449 | .thenCompose(server -> storageClient.delete(server, fileId)); 450 | } 451 | 452 | /** 453 | * @param fileId 454 | * @param file 455 | * @return 456 | */ 457 | public CompletableFuture append(String fileId, File file) { 458 | return append(FileId.fromString(fileId), file); 459 | } 460 | 461 | /** 462 | * 追加文件 463 | * 464 | * @param fileId 服务器存储路径 465 | * @param file 内容 466 | * @return 467 | */ 468 | public CompletableFuture append(FileId fileId, File file) { 469 | Objects.requireNonNull(fileId, "fileId must not be null."); 470 | Objects.requireNonNull(file, "file must not be null."); 471 | return trackerClient 472 | .updateStorageGet(fileId) 473 | .thenCompose(server -> storageClient.append(server, fileId, file)); 474 | } 475 | 476 | /** 477 | * @param fileId 478 | * @param bytes 479 | * @return 480 | */ 481 | public CompletableFuture append(String fileId, byte[] bytes) { 482 | return append(FileId.fromString(fileId), bytes); 483 | } 484 | 485 | /** 486 | * 追加文件 487 | * 488 | * @param fileId 服务器存储路径 489 | * @param bytes 内容 490 | * @return 491 | */ 492 | public CompletableFuture append(FileId fileId, byte[] bytes) { 493 | return append(fileId, bytes, bytes.length); 494 | } 495 | 496 | /** 497 | * @param fileId 498 | * @param content 499 | * @param size 500 | * @return 501 | */ 502 | public CompletableFuture append(String fileId, Object content, long size) { 503 | return append(FileId.fromString(fileId), content, size); 504 | } 505 | 506 | /** 507 | * @param fileId 508 | * @param content 509 | * @param size 510 | * @return 511 | */ 512 | public CompletableFuture append(FileId fileId, Object content, long size) { 513 | Objects.requireNonNull(fileId, "fileId must not be null."); 514 | Objects.requireNonNull(content, "content must not be null."); 515 | return trackerClient 516 | .updateStorageGet(fileId) 517 | .thenCompose(server -> storageClient.append(server, fileId, content, size)); 518 | } 519 | 520 | /** 521 | * @param fileId 522 | * @param file 523 | * @param offset 524 | * @return 525 | */ 526 | public CompletableFuture modify(String fileId, File file, long offset) { 527 | return modify(FileId.fromString(fileId), file, offset); 528 | } 529 | 530 | /** 531 | * @param fileId 532 | * @param file 533 | * @param offset 534 | * @return 535 | */ 536 | public CompletableFuture modify(FileId fileId, File file, long offset) { 537 | Objects.requireNonNull(fileId, "fileId must not be null."); 538 | Objects.requireNonNull(file, "file must not be null."); 539 | return trackerClient 540 | .updateStorageGet(fileId) 541 | .thenCompose(server -> storageClient.modify(server, fileId, file, offset)); 542 | } 543 | 544 | /** 545 | * @param fileId 546 | * @param bytes 547 | * @param offset 548 | * @return 549 | */ 550 | public CompletableFuture modify(String fileId, byte[] bytes, long offset) { 551 | return modify(FileId.fromString(fileId), bytes, offset); 552 | } 553 | 554 | /** 555 | * @param fileId 556 | * @param bytes 557 | * @param offset 558 | * @return 559 | */ 560 | public CompletableFuture modify(FileId fileId, byte[] bytes, long offset) { 561 | return modify(fileId, bytes, bytes.length, offset); 562 | } 563 | 564 | /** 565 | * @param fileId 566 | * @param content 567 | * @param size 568 | * @param offset 569 | * @return 570 | */ 571 | public CompletableFuture modify(String fileId, Object content, long size, long offset) { 572 | return modify(FileId.fromString(fileId), content, size, offset); 573 | } 574 | 575 | /** 576 | * @param fileId 577 | * @param content 578 | * @param size 579 | * @param offset 580 | * @return 581 | */ 582 | public CompletableFuture modify(FileId fileId, Object content, long size, long offset) { 583 | Objects.requireNonNull(fileId, "fileId must not be null."); 584 | Objects.requireNonNull(content, "content must not be null."); 585 | return trackerClient 586 | .updateStorageGet(fileId) 587 | .thenCompose(server -> storageClient.modify(server, fileId, content, size, offset)); 588 | } 589 | 590 | /** 591 | * @param fileId 592 | * @return 593 | */ 594 | public CompletableFuture truncate(String fileId) { 595 | return truncate(FileId.fromString(fileId)); 596 | } 597 | 598 | /** 599 | * 截取文件 600 | * 601 | * @param fileId 服务器存储路径 602 | * @return 603 | */ 604 | public CompletableFuture truncate(FileId fileId) { 605 | return truncate(fileId, 0); 606 | } 607 | 608 | /** 609 | * @param fileId 610 | * @param truncatedSize 611 | * @return 612 | */ 613 | public CompletableFuture truncate(String fileId, long truncatedSize) { 614 | return truncate(FileId.fromString(fileId), truncatedSize); 615 | } 616 | 617 | /** 618 | * 截取文件 619 | * 620 | * @param fileId 服务器存储路径 621 | * @param truncatedSize 截取字节数 622 | * @return 623 | */ 624 | public CompletableFuture truncate(FileId fileId, long truncatedSize) { 625 | Objects.requireNonNull(fileId, "fileId must not be null."); 626 | return trackerClient 627 | .updateStorageGet(fileId) 628 | .thenCompose(server -> storageClient.truncate(server, fileId, truncatedSize)); 629 | } 630 | 631 | /** 632 | * @param fileId 633 | * @param metadata 634 | * @return 635 | */ 636 | public CompletableFuture metadataSet(String fileId, FileMetadata metadata) { 637 | return metadataSet(FileId.fromString(fileId), metadata); 638 | } 639 | 640 | /** 641 | * 设置文件元数据 642 | * 643 | * @param fileId 服务器存储路径 644 | * @param metadata 元数据 645 | */ 646 | public CompletableFuture metadataSet(FileId fileId, FileMetadata metadata) { 647 | return metadataSet(fileId, metadata, FileMetadata.OVERWRITE_FLAG); 648 | } 649 | 650 | /** 651 | * @param fileId 652 | * @param metadata 653 | * @param flag 654 | * @return 655 | */ 656 | public CompletableFuture metadataSet(String fileId, FileMetadata metadata, byte flag) { 657 | return metadataSet(FileId.fromString(fileId), metadata, flag); 658 | } 659 | 660 | /** 661 | * 设置文件元数据 662 | * 663 | * @param fileId 服务器存储路径 664 | * @param metadata 元数据 665 | * @param flag 设置标识 666 | */ 667 | public CompletableFuture metadataSet(FileId fileId, FileMetadata metadata, byte flag) { 668 | Objects.requireNonNull(fileId, "fileId must not be null."); 669 | Objects.requireNonNull(metadata, "metadata must not be null."); 670 | return trackerClient 671 | .updateStorageGet(fileId) 672 | .thenCompose(server -> storageClient.setMetadata(server, fileId, metadata, flag)); 673 | } 674 | 675 | /** 676 | * @param fileId 677 | * @return 678 | */ 679 | public CompletableFuture metadataGet(String fileId) { 680 | return metadataGet(FileId.fromString(fileId)); 681 | } 682 | 683 | /** 684 | * 获取文件元数据 685 | * 686 | * @param fileId 687 | * @return 688 | */ 689 | public CompletableFuture metadataGet(FileId fileId) { 690 | Objects.requireNonNull(fileId, "fileId must not be null."); 691 | return trackerClient 692 | .updateStorageGet(fileId) 693 | .thenCompose(server -> storageClient.getMetadata(server, fileId)); 694 | } 695 | 696 | /** 697 | * @param fileId 698 | * @return 699 | */ 700 | public CompletableFuture infoGet(String fileId) { 701 | return infoGet(FileId.fromString(fileId)); 702 | } 703 | 704 | /** 705 | * 获取文件信息 706 | * 707 | * @param fileId 708 | * @return 709 | */ 710 | public CompletableFuture infoGet(FileId fileId) { 711 | Objects.requireNonNull(fileId, "fileId must not be null."); 712 | return trackerClient 713 | .updateStorageGet(fileId) 714 | .thenCompose(server -> storageClient.getInfo(server, fileId)); 715 | } 716 | 717 | @Override 718 | public void close() throws IOException { 719 | try { 720 | executor.close(); 721 | } catch (Exception e) { 722 | // do nothing; 723 | } 724 | 725 | try { 726 | scheduler.close(); 727 | } catch (Exception e) { 728 | // do nothing; 729 | } 730 | 731 | trackerClient.close(); 732 | } 733 | 734 | /** 735 | * @return 736 | */ 737 | public static Builder newBuilder() { 738 | return new Builder(); 739 | } 740 | 741 | public static class Builder { 742 | 743 | long connectTimeout = DEFAULT_CONNECT_TIMEOUT; // 连接超时时间(毫秒) 744 | long readTimeout = DEFAULT_READ_TIMEOUT;// 读超时时间(毫秒) 745 | long idleTimeout = DEFAULT_IDLE_TIMEOUT;// 连接闲置时间(毫秒) 746 | 747 | int maxThreads = DEFAULT_MAX_THREADS; // 线程数量 748 | int maxConnPerHost = DEFAULT_MAX_CONN_PER_HOST; // 每个IP最大连接数 749 | int maxPendingRequests = DEFAULT_MAX_PENDING_REQUESTS; 750 | 751 | int rise = DEFAULT_HEALTH_RISE; 752 | int fall = DEFAULT_HEALTH_FALL; 753 | long checkTimeout = DEFAULT_CHECK_TIMEOUT; 754 | long checkInterval = DEFAULT_CHECK_INTERVAL; 755 | 756 | // 757 | TrackerSelector selector = TrackerSelector.FIRST; 758 | List trackers = new LinkedList<>(); 759 | 760 | Builder() { 761 | } 762 | 763 | public Builder connectTimeout(long connectTimeout) { 764 | this.connectTimeout = connectTimeout; 765 | return this; 766 | } 767 | 768 | public Builder readTimeout(long readTimeout) { 769 | this.readTimeout = readTimeout; 770 | return this; 771 | } 772 | 773 | public Builder idleTimeout(long idleTimeout) { 774 | this.idleTimeout = idleTimeout; 775 | return this; 776 | } 777 | 778 | public Builder maxThreads(int maxThreads) { 779 | this.maxThreads = maxThreads; 780 | return this; 781 | } 782 | 783 | public Builder maxConnPerHost(int maxConnPerHost) { 784 | this.maxConnPerHost = maxConnPerHost; 785 | return this; 786 | } 787 | 788 | public Builder maxPendingRequests(int maxPendingRequests) { 789 | this.maxPendingRequests = maxPendingRequests; 790 | return this; 791 | } 792 | 793 | @Deprecated 794 | public Builder maxIdleSeconds(int maxIdleSeconds) { 795 | this.idleTimeout = TimeUnit.SECONDS.toMillis(maxIdleSeconds); 796 | return this; 797 | } 798 | 799 | /** 800 | * 801 | * @param fall times after check failure, the server is marked down. 802 | * @param rise times after check success, the server is marked up. 803 | * @param checkTimeout 804 | * @param checkInterval 805 | * @return 806 | */ 807 | public Builder healthCheck(int fall, int rise, long checkTimeout, long checkInterval) { 808 | this.fall = fall; 809 | this.rise = rise; 810 | this.checkTimeout = checkTimeout; 811 | this.checkInterval = checkInterval; 812 | return this; 813 | } 814 | 815 | public Builder healthCheck(long checkTimeout, long checkInterval, TimeUnit unit) { 816 | this.checkTimeout = unit.toMillis(checkTimeout); 817 | this.checkInterval = unit.toMillis(checkInterval); 818 | return this; 819 | } 820 | 821 | /** 822 | * @param selector 823 | * @return 824 | */ 825 | public Builder selector(TrackerSelector selector) { 826 | this.selector = Objects.requireNonNull(selector, "selector must not be null."); 827 | return this; 828 | } 829 | 830 | /** 831 | * @param servers 832 | * @return 833 | */ 834 | public Builder trackers(List servers) { 835 | this.trackers = new LinkedList<>(Objects.requireNonNull(servers, "servers must not be null.")); 836 | return this; 837 | } 838 | 839 | /** 840 | * @param server 841 | * @return 842 | */ 843 | public Builder tracker(TrackerServer server) { 844 | this.trackers.add(Objects.requireNonNull(server, "server must not be null.")); 845 | return this; 846 | } 847 | 848 | /** 849 | * @param host 850 | * @param port 851 | * @return 852 | */ 853 | public Builder tracker(String host, int port) { 854 | return tracker(new TrackerServer(host, port)); 855 | } 856 | 857 | /** 858 | * @param host 859 | * @param port 860 | * @param weight 861 | * @return 862 | */ 863 | public Builder tracker(String host, int port, int weight) { 864 | return tracker(new TrackerServer(host, port, weight)); 865 | } 866 | 867 | public FastdfsClient build() { 868 | return new FastdfsClient(this); 869 | } 870 | } 871 | } 872 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsConstants.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | import java.nio.charset.Charset; 7 | import java.nio.charset.StandardCharsets; 8 | 9 | /** 10 | * 常量定义 11 | * 12 | * @author liulongbiao 13 | */ 14 | public interface FastdfsConstants { 15 | 16 | Charset UTF_8 = StandardCharsets.UTF_8; 17 | 18 | int FDFS_HEAD_LEN = 10; // 头部长度 19 | int FDFS_LONG_LEN = 8; // long 所占字节数 20 | int FDFS_PROTO_PKG_LEN_SIZE = FDFS_LONG_LEN; // 包长字节数 21 | int FDFS_FILE_EXT_LEN = 6; // 文件扩展名字节数 22 | int FDFS_GROUP_LEN = 16; // 文件分组字节数 23 | int FDFS_HOST_LEN = 15; // 主机地址字节数 24 | int FDFS_PORT_LEN = FDFS_LONG_LEN; // 端口 字节数 25 | int FDFS_STORE_PATH_INDEX_LEN = 1; // pathIdx 字节数 26 | int FDFS_STORAGE_LEN = FDFS_GROUP_LEN + FDFS_HOST_LEN + FDFS_PORT_LEN; // 存储服务器信息字节数 27 | int FDFS_STORAGE_STORE_LEN = FDFS_STORAGE_LEN + FDFS_STORE_PATH_INDEX_LEN; // 存储服务器信息及存储路径索引字节数 28 | 29 | @Deprecated 30 | byte METADATA_OVERWRITE = 'O'; 31 | @Deprecated 32 | byte METADATA_MERGE = 'M'; 33 | 34 | String FDFS_RECORD_SEPERATOR = "\u0001"; 35 | String FDFS_FIELD_SEPERATOR = "\u0002"; 36 | byte ERRNO_OK = 0; 37 | 38 | /** 39 | * Fastdfs 用到的命令代码 40 | * 41 | * @author liulongbiao 42 | */ 43 | interface Commands { 44 | 45 | byte QUIT = 82; 46 | byte RESP = 100; 47 | byte ACTIVE_TEST = 111; 48 | 49 | byte SERVER_LIST_GROUP = 91; 50 | byte SERVER_LIST_STORAGE = 92; 51 | byte SERVER_DELETE_STORAGE = 93; 52 | 53 | byte SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE = 101; 54 | byte SERVICE_QUERY_FETCH_ONE = 102; 55 | byte SERVICE_QUERY_UPDATE = 103; 56 | byte SERVICE_QUERY_STORE_WITH_GROUP_ONE = 104; 57 | byte SERVICE_QUERY_FETCH_ALL = 105; 58 | byte SERVICE_QUERY_STORE_WITHOUT_GROUP_ALL = 106; 59 | byte SERVICE_QUERY_STORE_WITH_GROUP_ALL = 107; 60 | 61 | byte FILE_UPLOAD = 11; 62 | byte FILE_DELETE = 12; 63 | byte FILE_DOWNLOAD = 14; 64 | byte FILE_QUERY = 22; 65 | byte FILE_APPEND = 24; // append file 66 | byte FILE_MODIFY = 34; // modify appender file 67 | byte FILE_TRUNCATE = 36; // truncate appender file 68 | 69 | byte SLAVE_FILE_UPLOAD = 21; 70 | byte APPENDER_FILE_UPLOAD = 23; // stream appender file 71 | 72 | byte METADATA_SET = 13; 73 | byte METADATA_GET = 15; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsDataOverflowException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | /** 7 | * @author siuming 8 | */ 9 | public class FastdfsDataOverflowException extends FastdfsException { 10 | 11 | private static final long serialVersionUID = -3517480052882048862L; 12 | 13 | FastdfsDataOverflowException(String message) { 14 | super(message); 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | /** 7 | * Fastdfs 异常 8 | * 9 | * @author liulongbiao 10 | */ 11 | public class FastdfsException extends RuntimeException { 12 | 13 | /** 14 | * 15 | */ 16 | private static final long serialVersionUID = -8274679139300220262L; 17 | 18 | public FastdfsException() { 19 | } 20 | 21 | public FastdfsException(String message, Throwable cause) { 22 | super(message, cause); 23 | } 24 | 25 | public FastdfsException(String message) { 26 | super(message); 27 | } 28 | 29 | public FastdfsException(Throwable cause) { 30 | super(cause); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsExecutor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | import fastdfs.client.exchange.Replier; 7 | import fastdfs.client.exchange.ReplierDecoder; 8 | import fastdfs.client.exchange.Requestor; 9 | import fastdfs.client.exchange.RequestorEncoder; 10 | import io.netty.channel.Channel; 11 | import io.netty.channel.EventLoopGroup; 12 | import io.netty.channel.nio.NioEventLoopGroup; 13 | import io.netty.util.concurrent.Future; 14 | import io.netty.util.concurrent.FutureListener; 15 | import org.slf4j.Logger; 16 | import org.slf4j.LoggerFactory; 17 | 18 | import javax.annotation.PreDestroy; 19 | import java.io.Closeable; 20 | import java.io.IOException; 21 | import java.net.InetSocketAddress; 22 | import java.util.concurrent.CompletableFuture; 23 | import java.util.concurrent.ThreadFactory; 24 | import java.util.concurrent.atomic.AtomicInteger; 25 | 26 | /** 27 | * FastdfsExecutor 28 | * 29 | * @author liulongbiao 30 | */ 31 | final class FastdfsExecutor implements Closeable { 32 | 33 | private static final Logger LOG = LoggerFactory.getLogger(FastdfsExecutor.class); 34 | 35 | private final EventLoopGroup loopGroup; 36 | private final FastdfsPoolGroup poolGroup; 37 | 38 | FastdfsExecutor(FastdfsSettings settings) { 39 | loopGroup = new NioEventLoopGroup(settings.maxThreads(), new ThreadFactory() { 40 | final String threadPrefix = "fastdfs-"; 41 | final AtomicInteger threadNumber = new AtomicInteger(1); 42 | 43 | @Override 44 | public Thread newThread(Runnable r) { 45 | return new Thread(null, r, threadPrefix + threadNumber.getAndIncrement()); 46 | } 47 | }); 48 | poolGroup = new FastdfsPoolGroup( 49 | loopGroup, 50 | settings.connectTimeout(), 51 | settings.readTimeout(), 52 | settings.idleTimeout(), 53 | settings.maxConnPerHost(), 54 | settings.maxPendingRequests() 55 | ); 56 | } 57 | 58 | /** 59 | * 访问 Fastdfs 服务器 60 | * 61 | * @param addr 62 | * @param encoder 63 | * @param decoder 64 | * @return 65 | */ 66 | CompletableFuture execute(InetSocketAddress addr, Requestor.Encoder encoder, Replier.Decoder decoder) { 67 | return execute(addr, new RequestorEncoder(encoder), new ReplierDecoder<>(decoder)); 68 | } 69 | 70 | /** 71 | * @param addr 72 | * @param encoder 73 | * @param replier 74 | * @param 75 | * @return 76 | */ 77 | CompletableFuture execute(InetSocketAddress addr, Requestor.Encoder encoder, Replier replier) { 78 | return execute(addr, new RequestorEncoder(encoder), replier); 79 | } 80 | 81 | /** 82 | * @param addr 83 | * @param requestor 84 | * @param replier 85 | * @param 86 | * @return 87 | */ 88 | CompletableFuture execute(InetSocketAddress addr, Requestor requestor, Replier replier) { 89 | CompletableFuture promise = new CompletableFuture<>(); 90 | execute(addr, requestor, replier, promise); 91 | return promise; 92 | } 93 | 94 | private void execute(InetSocketAddress addr, Requestor requestor, Replier replier, CompletableFuture promise) { 95 | FastdfsPool pool = poolGroup.get(addr); 96 | pool.acquire().addListener(new FastdfsChannelListener<>(pool, requestor, replier, promise)); 97 | } 98 | 99 | @PreDestroy 100 | public void close() throws IOException { 101 | if (null != poolGroup) { 102 | try { 103 | poolGroup.close(); 104 | } catch (Exception e) { 105 | // ignore 106 | } 107 | } 108 | if (null != loopGroup) { 109 | loopGroup.shutdownGracefully(); 110 | } 111 | } 112 | 113 | private static class FastdfsChannelListener implements FutureListener { 114 | 115 | final FastdfsPool pool; 116 | final Requestor requestor; 117 | final Replier replier; 118 | final CompletableFuture promise; 119 | 120 | FastdfsChannelListener(FastdfsPool pool, 121 | Requestor requestor, 122 | Replier replier, 123 | CompletableFuture promise) { 124 | this.pool = pool; 125 | this.requestor = requestor; 126 | this.replier = replier; 127 | this.promise = promise; 128 | } 129 | 130 | @Override 131 | public void operationComplete(Future cf) throws Exception { 132 | 133 | if (cf.isCancelled()) { 134 | promise.cancel(true); 135 | return; 136 | } 137 | 138 | if (!cf.isSuccess()) { 139 | promise.completeExceptionally(cf.cause()); 140 | return; 141 | } 142 | 143 | Channel channel = cf.getNow(); 144 | promise.whenComplete((result, error) -> { 145 | if (null != error) { 146 | channel.close().addListener(ignore -> pool.release(channel)); 147 | return; 148 | } 149 | 150 | pool.release(channel); 151 | }); 152 | 153 | try { 154 | 155 | FastdfsOperation fastdfsOperation = new FastdfsOperation<>(channel, requestor, replier, promise); 156 | if (LOG.isDebugEnabled()) { 157 | LOG.debug("execute {}", fastdfsOperation); 158 | } 159 | 160 | fastdfsOperation.execute(); 161 | } catch (Exception e) { 162 | promise.completeExceptionally(e); 163 | } 164 | } 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsHandler.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | import io.netty.buffer.ByteBuf; 7 | import io.netty.channel.ChannelHandlerContext; 8 | import io.netty.handler.codec.ByteToMessageDecoder; 9 | import io.netty.handler.timeout.IdleStateEvent; 10 | import io.netty.util.AttributeKey; 11 | 12 | import java.lang.reflect.InvocationTargetException; 13 | import java.lang.reflect.UndeclaredThrowableException; 14 | import java.util.List; 15 | 16 | final class FastdfsHandler extends ByteToMessageDecoder { 17 | static final AttributeKey> OPERATION_KEY = AttributeKey.newInstance("fastdfsOperation"); 18 | 19 | @Override 20 | protected void decode(ChannelHandlerContext ctx, ByteBuf in, List out) throws Exception { 21 | FastdfsOperation operation = ctx.channel().attr(OPERATION_KEY).get(); 22 | 23 | if (null != operation) { 24 | operation.await(in); 25 | return; 26 | } 27 | 28 | if (in.readableBytes() <= 0) { 29 | return; 30 | } 31 | 32 | throw new FastdfsDataOverflowException( 33 | String.format( 34 | "fastdfs channel %s remain %s data bytes, but there is not operation await.", 35 | ctx.channel(), 36 | in.readableBytes() 37 | ) 38 | ); 39 | } 40 | 41 | @Override 42 | public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { 43 | FastdfsOperation operation = ctx.channel().attr(OPERATION_KEY).get(); 44 | // read idle event. 45 | if (evt == IdleStateEvent.FIRST_READER_IDLE_STATE_EVENT 46 | || evt == IdleStateEvent.READER_IDLE_STATE_EVENT) { 47 | 48 | if (null != operation) { 49 | throw new FastdfsReadTimeoutException( 50 | String.format( 51 | "execute %s read timeout.", 52 | operation 53 | ) 54 | ); 55 | } 56 | } 57 | 58 | // all idle event. 59 | if (evt == IdleStateEvent.FIRST_ALL_IDLE_STATE_EVENT 60 | || evt == IdleStateEvent.ALL_IDLE_STATE_EVENT) { 61 | throw new FastdfsTimeoutException("fastdfs channel was idle timeout."); 62 | } 63 | } 64 | 65 | @Override 66 | public void channelInactive(ChannelHandlerContext ctx) throws Exception { 67 | super.channelInactive(ctx); 68 | FastdfsOperation operation = ctx.channel().attr(OPERATION_KEY).get(); 69 | if (null == operation) { 70 | return; 71 | } 72 | 73 | operation.caught(new FastdfsException("channel closed.")); 74 | } 75 | 76 | @Override 77 | public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { 78 | FastdfsOperation operation = ctx.channel().attr(OPERATION_KEY).get(); 79 | if (null != operation) { 80 | operation.caught(translateException(cause)); 81 | } else ctx.close(); 82 | } 83 | 84 | private Throwable translateException(Throwable cause) { 85 | if (cause instanceof FastdfsException) { 86 | return cause; 87 | } 88 | 89 | Throwable unwrap = cause; 90 | for (; ; ) { 91 | 92 | if (unwrap instanceof InvocationTargetException) { 93 | unwrap = ((InvocationTargetException) unwrap).getTargetException(); 94 | continue; 95 | } 96 | 97 | if (unwrap instanceof UndeclaredThrowableException) { 98 | unwrap = ((UndeclaredThrowableException) unwrap).getUndeclaredThrowable(); 99 | continue; 100 | } 101 | 102 | break; 103 | } 104 | 105 | return new FastdfsException("fastdfs operation error.", unwrap); 106 | } 107 | } 108 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsOperation.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client; 2 | 3 | import fastdfs.client.exchange.Replier; 4 | import fastdfs.client.exchange.Requestor; 5 | import io.netty.buffer.ByteBuf; 6 | import io.netty.channel.Channel; 7 | 8 | import java.util.concurrent.CompletableFuture; 9 | 10 | /** 11 | * @author siuming 12 | */ 13 | final class FastdfsOperation { 14 | 15 | private final Channel channel; 16 | private final Requestor requestor; 17 | private final Replier replier; 18 | private final CompletableFuture promise; 19 | 20 | FastdfsOperation(Channel channel, Requestor requestor, Replier replier, CompletableFuture promise) { 21 | this.channel = channel; 22 | this.requestor = requestor; 23 | this.replier = replier; 24 | this.promise = promise; 25 | } 26 | 27 | void execute() { 28 | 29 | channel.attr(FastdfsHandler.OPERATION_KEY).set(this); 30 | try { 31 | 32 | requestor.request(channel); 33 | } catch (Exception e) { 34 | caught(e); 35 | } 36 | } 37 | 38 | boolean isDone() { 39 | return promise.isDone(); 40 | } 41 | 42 | void await(ByteBuf in) { 43 | try { 44 | 45 | replier.reply(in, promise); 46 | } catch (Exception e) { 47 | caught(e); 48 | } 49 | } 50 | 51 | void caught(Throwable cause) { 52 | promise.completeExceptionally(cause); 53 | } 54 | 55 | @Override 56 | public String toString() { 57 | return "FastdfsOperation{" + 58 | "channel=" + channel + 59 | ", replier=" + replier + 60 | ", requestor=" + requestor + 61 | '}'; 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsPool.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client; 2 | 3 | import io.netty.bootstrap.Bootstrap; 4 | import io.netty.channel.Channel; 5 | import io.netty.channel.ChannelPipeline; 6 | import io.netty.channel.pool.ChannelPool; 7 | import io.netty.channel.pool.ChannelPoolHandler; 8 | import io.netty.channel.pool.FixedChannelPool; 9 | import io.netty.handler.stream.ChunkedWriteHandler; 10 | import io.netty.handler.timeout.IdleStateHandler; 11 | import io.netty.util.concurrent.Future; 12 | import io.netty.util.concurrent.Promise; 13 | import org.slf4j.Logger; 14 | import org.slf4j.LoggerFactory; 15 | 16 | import java.util.concurrent.TimeUnit; 17 | 18 | /** 19 | * @author siuming 20 | */ 21 | final class FastdfsPool implements ChannelPool { 22 | 23 | private static final Logger LOG = LoggerFactory.getLogger(FastdfsPoolGroup.class); 24 | private final ChannelPool channelPool; 25 | 26 | FastdfsPool(Bootstrap bootstrap, long readTimeout, long idleTimeout, int maxConnPerHost, int maxPendingRequests) { 27 | this.channelPool = new FixedChannelPool( 28 | bootstrap, 29 | new FastdfsPoolHandler(readTimeout, idleTimeout), 30 | maxConnPerHost, 31 | maxPendingRequests 32 | ); 33 | } 34 | 35 | public Future acquire() { 36 | return channelPool.acquire(); 37 | } 38 | 39 | public Future release(Channel channel, Promise promise) { 40 | return channelPool.release(channel, promise); 41 | } 42 | 43 | public Future acquire(Promise promise) { 44 | return channelPool.acquire(promise); 45 | } 46 | 47 | public void close() { 48 | channelPool.close(); 49 | } 50 | 51 | public Future release(Channel channel) { 52 | return channelPool.release(channel); 53 | } 54 | 55 | private static class FastdfsPoolHandler implements ChannelPoolHandler { 56 | final long readTimeout; 57 | final long idleTimeout; // 最大闲置时间(秒) 58 | 59 | FastdfsPoolHandler(long readTimeout, long idleTimeout) { 60 | this.readTimeout = readTimeout; 61 | this.idleTimeout = idleTimeout; 62 | } 63 | 64 | public void channelReleased(Channel channel) throws Exception { 65 | if (LOG.isDebugEnabled()) { 66 | LOG.debug("channel released : {}", channel.toString()); 67 | } 68 | 69 | channel.attr(FastdfsHandler.OPERATION_KEY).set(null); 70 | } 71 | 72 | public void channelAcquired(Channel channel) throws Exception { 73 | if (LOG.isDebugEnabled()) { 74 | LOG.debug("channel acquired : {}", channel.toString()); 75 | } 76 | 77 | channel.attr(FastdfsHandler.OPERATION_KEY).set(null); 78 | } 79 | 80 | public void channelCreated(Channel channel) throws Exception { 81 | if (LOG.isInfoEnabled()) { 82 | LOG.info("channel created : {}", channel.toString()); 83 | } 84 | 85 | ChannelPipeline pipeline = channel.pipeline(); 86 | pipeline.addLast(new IdleStateHandler(readTimeout, 0, idleTimeout, TimeUnit.MILLISECONDS)); 87 | pipeline.addLast(new ChunkedWriteHandler()); 88 | pipeline.addLast(new FastdfsHandler()); 89 | } 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsPoolGroup.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | import io.netty.bootstrap.Bootstrap; 7 | import io.netty.channel.ChannelOption; 8 | import io.netty.channel.EventLoopGroup; 9 | import io.netty.channel.pool.AbstractChannelPoolMap; 10 | import io.netty.channel.socket.nio.NioSocketChannel; 11 | import org.slf4j.Logger; 12 | import org.slf4j.LoggerFactory; 13 | 14 | import java.net.InetSocketAddress; 15 | 16 | /** 17 | * Fastdfs 连接池组 18 | * 19 | * @author liulongbiao 20 | */ 21 | final class FastdfsPoolGroup extends AbstractChannelPoolMap { 22 | 23 | private static final Logger LOG = LoggerFactory.getLogger(FastdfsPoolGroup.class); 24 | 25 | private final EventLoopGroup loopGroup; 26 | 27 | private final long connectTimeout; 28 | private final long readTimeout; 29 | private final long idleTimeout; 30 | private final int maxConnPerHost; 31 | private final int maxPendingRequests; 32 | 33 | FastdfsPoolGroup(EventLoopGroup loopGroup, 34 | long connectTimeout, 35 | long readTimeout, 36 | long idleTimeout, 37 | int maxConnPerHost, 38 | int maxPendingRequests) { 39 | this.loopGroup = loopGroup; 40 | this.connectTimeout = connectTimeout; 41 | this.readTimeout = readTimeout; 42 | this.idleTimeout = idleTimeout; 43 | this.maxConnPerHost = maxConnPerHost; 44 | this.maxPendingRequests = maxPendingRequests; 45 | } 46 | 47 | @Override 48 | protected FastdfsPool newPool(InetSocketAddress addr) { 49 | if (LOG.isDebugEnabled()) { 50 | LOG.debug("channel pool created : {}", addr); 51 | } 52 | 53 | Bootstrap bootstrap = new Bootstrap().channel(NioSocketChannel.class).group(loopGroup); 54 | bootstrap.remoteAddress(addr); 55 | bootstrap.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, (int) connectTimeout); 56 | bootstrap.option(ChannelOption.TCP_NODELAY, true); 57 | bootstrap.option(ChannelOption.SO_KEEPALIVE, true); 58 | return new FastdfsPool( 59 | bootstrap, 60 | readTimeout, 61 | idleTimeout, 62 | maxConnPerHost, 63 | maxPendingRequests 64 | ); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsReadTimeoutException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | 7 | public class FastdfsReadTimeoutException extends FastdfsException { 8 | 9 | private static final long serialVersionUID = -4345410185778108716L; 10 | 11 | FastdfsReadTimeoutException(String message) { 12 | super(message); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsScheduler.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client; 2 | 3 | import java.io.Closeable; 4 | import java.io.IOException; 5 | import java.util.concurrent.*; 6 | import java.util.concurrent.atomic.AtomicInteger; 7 | 8 | class FastdfsScheduler implements Closeable { 9 | private final ScheduledExecutorService loop; 10 | 11 | FastdfsScheduler(int threads) { 12 | this.loop = Executors.newScheduledThreadPool(threads, new ThreadFactory() { 13 | final String threadPrefix = "fastdfs-scheduler-"; 14 | final AtomicInteger threadNumber = new AtomicInteger(1); 15 | 16 | @Override 17 | public Thread newThread(Runnable r) { 18 | return new Thread(null, r, threadPrefix + threadNumber.getAndIncrement()); 19 | } 20 | }); 21 | } 22 | 23 | ScheduledFuture schedule(Runnable command, long delay, TimeUnit unit) { 24 | return loop.schedule(command, delay, unit); 25 | } 26 | 27 | ScheduledFuture schedule(Callable callable, long delay, TimeUnit unit) { 28 | return loop.schedule(callable, delay, unit); 29 | } 30 | 31 | ScheduledFuture scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) { 32 | return loop.scheduleAtFixedRate(command, initialDelay, period, unit); 33 | } 34 | 35 | ScheduledFuture scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) { 36 | return loop.scheduleWithFixedDelay(command, initialDelay, delay, unit); 37 | } 38 | 39 | @Override 40 | public void close() throws IOException { 41 | try { 42 | loop.shutdown(); 43 | } catch (Exception e) { 44 | // do nothing. 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsSettings.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | 7 | /** 8 | * 连接设置 9 | * 10 | * @author liulongbiao 11 | */ 12 | final class FastdfsSettings { 13 | 14 | private long connectTimeout; 15 | private long readTimeout; 16 | private long idleTimeout; 17 | 18 | private int maxThreads; 19 | private int maxConnPerHost; 20 | private int maxPendingRequests; 21 | 22 | FastdfsSettings(long connectTimeout, 23 | long readTimeout, 24 | long idleTimeout, 25 | int maxThreads, 26 | int maxConnPerHost, 27 | int maxPendingRequests) { 28 | 29 | this.connectTimeout = connectTimeout; 30 | this.readTimeout = readTimeout; 31 | this.idleTimeout = idleTimeout; 32 | 33 | this.maxThreads = maxThreads; 34 | this.maxConnPerHost = maxConnPerHost; 35 | this.maxPendingRequests = maxPendingRequests; 36 | } 37 | 38 | long connectTimeout() { 39 | return connectTimeout; 40 | } 41 | 42 | long readTimeout() { 43 | return readTimeout; 44 | } 45 | 46 | long idleTimeout() { 47 | return idleTimeout; 48 | } 49 | 50 | int maxThreads() { 51 | return maxThreads; 52 | } 53 | 54 | int maxConnPerHost() { 55 | return maxConnPerHost; 56 | } 57 | 58 | int maxPendingRequests() { 59 | return maxPendingRequests; 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsTimeoutException.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | /** 7 | * Fastdfs 超时异常 8 | * 9 | * @author liulongbiao 10 | */ 11 | public class FastdfsTimeoutException extends FastdfsException { 12 | 13 | /** 14 | * 15 | */ 16 | private static final long serialVersionUID = -5384713284430243210L; 17 | 18 | FastdfsTimeoutException(String message) { 19 | super(message); 20 | } 21 | 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FastdfsUtils.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | import io.netty.buffer.ByteBuf; 7 | 8 | import java.util.Collection; 9 | import java.util.List; 10 | 11 | /** 12 | * 帮助类 13 | * 14 | * @author liulongbiao 15 | */ 16 | public final class FastdfsUtils { 17 | 18 | private final static String WORDS = "[a-zA-Z0-9]{1,6}"; 19 | 20 | private FastdfsUtils() { 21 | } 22 | 23 | /** 24 | * 判断字符串为空 25 | * 26 | * @param content 27 | * @return 28 | */ 29 | public static boolean isEmpty(String content) { 30 | return content == null || content.isEmpty(); 31 | } 32 | 33 | /** 34 | * 判断集合是否为空 35 | * 36 | * @param coll 37 | * @return 38 | */ 39 | public static boolean isEmpty(Collection coll) { 40 | return coll == null || coll.isEmpty(); 41 | } 42 | 43 | /** 44 | * 获取列表头元素 45 | * 46 | * @param list 47 | * @return 48 | */ 49 | public static T first(List list) { 50 | return isEmpty(list) ? null : list.get(0); 51 | } 52 | 53 | /** 54 | * 给 ByteBuf 写入定长字符串 55 | *

56 | * 若字符串长度大于定长,则截取定长字节;若小于定长,则补零 57 | * 58 | * @param buf 59 | * @param content 60 | * @param length 61 | */ 62 | public static void writeFixLength(ByteBuf buf, String content, int length) { 63 | byte[] bytes = content.getBytes(FastdfsConstants.UTF_8); 64 | int blen = bytes.length; 65 | int wlen = Math.min(blen, length); 66 | buf.writeBytes(bytes, 0, wlen); 67 | if (wlen < length) { 68 | buf.writeZero(length - wlen); 69 | } 70 | } 71 | 72 | /** 73 | * 读取固定长度的字符串(修剪掉补零的字节) 74 | * 75 | * @param in 76 | * @param length 77 | * @return 78 | */ 79 | public static String readString(ByteBuf in, int length) { 80 | byte[] bytes = new byte[length]; 81 | in.readBytes(bytes); 82 | return new String(bytes, FastdfsConstants.UTF_8).trim(); 83 | } 84 | 85 | /** 86 | * 读取字符串(修剪掉补零的字节) 87 | * 88 | * @param in 89 | * @return 90 | */ 91 | public static String readString(ByteBuf in) { 92 | return in.toString(FastdfsConstants.UTF_8); 93 | } 94 | 95 | /** 96 | * 获取文件扩展名 97 | * 98 | * @param filename 99 | * @return 100 | */ 101 | public static String getFileExt(String filename, String defaultExt) { 102 | String fileExt = getFileExt(filename); 103 | return isEmpty(fileExt) || !fileExt.matches(WORDS) ? defaultExt : fileExt; 104 | } 105 | 106 | /** 107 | * 获取文件扩展名 108 | * 109 | * @param filename 110 | * @return 111 | */ 112 | private static String getFileExt(String filename) { 113 | if (filename == null) { 114 | return ""; 115 | } 116 | int idx = filename.lastIndexOf('.'); 117 | return idx == -1 ? "" : filename.substring(idx + 1).toLowerCase(); 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FileId.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | 7 | import java.util.Base64; 8 | import java.util.Objects; 9 | 10 | import static fastdfs.client.FastdfsConstants.UTF_8; 11 | 12 | /** 13 | * @author siuming 14 | */ 15 | public class FileId { 16 | 17 | private static final char SEPARATER = '/'; 18 | 19 | private final String group; 20 | private final String path; 21 | 22 | /** 23 | * @param group 24 | * @param path 25 | */ 26 | public FileId(String group, String path) { 27 | this.group = Objects.requireNonNull(group, "group must not be null."); 28 | this.path = Objects.requireNonNull(path, "path must not be null."); 29 | } 30 | 31 | /** 32 | * @return 33 | */ 34 | public String group() { 35 | return group; 36 | } 37 | 38 | /** 39 | * @return 40 | */ 41 | public byte[] groupBytes() { 42 | return group.getBytes(UTF_8); 43 | } 44 | 45 | /** 46 | * @return 47 | */ 48 | public String path() { 49 | return path; 50 | } 51 | 52 | /** 53 | * @return 54 | */ 55 | public byte[] pathBytes() { 56 | return path.getBytes(UTF_8); 57 | } 58 | 59 | /** 60 | * @return 61 | */ 62 | public byte[] toBytes() { 63 | return toString().getBytes(UTF_8); 64 | } 65 | 66 | /** 67 | * @return 68 | */ 69 | @Override 70 | public String toString() { 71 | return group + SEPARATER + path; 72 | } 73 | 74 | /** 75 | * @return 76 | */ 77 | public String toBase64String() { 78 | return Base64.getUrlEncoder().encodeToString(toBytes()); 79 | } 80 | 81 | /** 82 | * 从全路径构造存储路径 83 | * 84 | * @param fullPath 85 | * @return 86 | */ 87 | public static FileId fromString(String fullPath) { 88 | if (fullPath == null || fullPath.length() == 0) { 89 | throw new IllegalArgumentException("fullPath should not be empty."); 90 | } 91 | int idx = fullPath.indexOf(SEPARATER); 92 | if (idx < 0) { 93 | throw new IllegalArgumentException("fullPath cannot find path separater."); 94 | } 95 | 96 | String group = fullPath.substring(0, idx); 97 | String path = fullPath.substring(idx + 1); 98 | return new FileId(group, path); 99 | } 100 | 101 | /** 102 | * @param base64 103 | * @return 104 | */ 105 | public static FileId fromBase64String(String base64) { 106 | byte[] bytes = Base64.getUrlDecoder().decode(base64.getBytes(UTF_8)); 107 | return fromString(new String(bytes)); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FileInfo.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client; 2 | 3 | /** 4 | * @author siuming 5 | */ 6 | public class FileInfo { 7 | 8 | private long fileSize; 9 | private long createTime; 10 | private long crc32; 11 | private String address; 12 | 13 | /** 14 | * @param builder 15 | */ 16 | private FileInfo(Builder builder) { 17 | this.fileSize = builder.fileSize; 18 | this.createTime = builder.createTime; 19 | this.crc32 = builder.crc32; 20 | this.address = builder.address; 21 | } 22 | 23 | public long fileSize() { 24 | return fileSize; 25 | } 26 | 27 | public long createTime() { 28 | return createTime; 29 | } 30 | 31 | public long crc32() { 32 | return crc32; 33 | } 34 | 35 | public String address() { 36 | return address; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return "FileInfo{" + 42 | "fileSize=" + fileSize + 43 | ", createTime=" + createTime + 44 | ", crc32=" + crc32 + 45 | ", address='" + address + '\'' + 46 | '}'; 47 | } 48 | 49 | /** 50 | * @return 51 | */ 52 | public static Builder newBuilder() { 53 | return new Builder(); 54 | } 55 | 56 | public static class Builder { 57 | long fileSize; 58 | long createTime; 59 | long crc32; 60 | String address; 61 | 62 | Builder() { 63 | } 64 | 65 | public Builder fileSize(long fileSize) { 66 | this.fileSize = fileSize; 67 | return this; 68 | } 69 | 70 | public Builder createTime(long createTime) { 71 | this.createTime = createTime; 72 | return this; 73 | } 74 | 75 | public Builder crc32(long crc32) { 76 | this.crc32 = crc32; 77 | return this; 78 | } 79 | 80 | public Builder address(String address) { 81 | this.address = address; 82 | return this; 83 | } 84 | 85 | public FileInfo build() { 86 | return new FileInfo(this); 87 | } 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/FileMetadata.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | import java.nio.charset.Charset; 7 | import java.util.Collections; 8 | import java.util.HashMap; 9 | import java.util.Map; 10 | 11 | import static fastdfs.client.FastdfsConstants.FDFS_FIELD_SEPERATOR; 12 | import static fastdfs.client.FastdfsConstants.FDFS_RECORD_SEPERATOR; 13 | 14 | 15 | public class FileMetadata { 16 | 17 | public static byte OVERWRITE_FLAG = 'O'; 18 | public static byte MERGE_FLAG = 'M'; 19 | 20 | private final Map values; 21 | 22 | /** 23 | * @param builder 24 | */ 25 | private FileMetadata(Builder builder) { 26 | this.values = builder.values; 27 | } 28 | 29 | /** 30 | * @param values 31 | */ 32 | public FileMetadata(Map values) { 33 | this.values = new HashMap<>(values); 34 | } 35 | 36 | /** 37 | * @return 38 | */ 39 | public Map values() { 40 | return Collections.unmodifiableMap(values); 41 | } 42 | 43 | @Override 44 | public String toString() { 45 | return "FileMetadata{" + 46 | "values=" + values + 47 | '}'; 48 | } 49 | 50 | /** 51 | * @param charset 52 | * @return 53 | */ 54 | public byte[] toBytes(Charset charset) { 55 | 56 | if (values.isEmpty()) { 57 | return new byte[0]; 58 | } 59 | 60 | StringBuilder sb = new StringBuilder(); 61 | boolean first = true; 62 | for (Map.Entry entry : values.entrySet()) { 63 | if (!first) { 64 | sb.append(FDFS_RECORD_SEPERATOR); 65 | } 66 | sb.append(entry.getKey()); 67 | sb.append(FDFS_FIELD_SEPERATOR); 68 | sb.append(entry.getValue()); 69 | first = false; 70 | } 71 | return sb.toString().getBytes(charset); 72 | } 73 | 74 | /** 75 | * @return 76 | */ 77 | public static Builder newBuilder() { 78 | return new Builder(); 79 | } 80 | 81 | public static class Builder { 82 | Map values = new HashMap<>(); 83 | 84 | Builder() { 85 | } 86 | 87 | public Builder put(String name, String value) { 88 | this.values.put(name, value); 89 | return this; 90 | } 91 | 92 | public Builder putAll(Map values) { 93 | this.values.putAll(values); 94 | return this; 95 | } 96 | 97 | public FileMetadata build() { 98 | return new FileMetadata(this); 99 | } 100 | } 101 | 102 | } 103 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/StorageClient.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | import fastdfs.client.codec.*; 7 | import fastdfs.client.exchange.Replier; 8 | import fastdfs.client.exchange.StreamReplier; 9 | 10 | import java.io.File; 11 | import java.util.concurrent.CompletableFuture; 12 | 13 | 14 | final class StorageClient { 15 | 16 | private final FastdfsExecutor executor; 17 | 18 | StorageClient(FastdfsExecutor executor) { 19 | this.executor = executor; 20 | } 21 | 22 | /** 23 | * 上传文件 24 | * 25 | * @param server 存储服务器信息,应该由 tracker 查询得到 26 | * @param file 文件 27 | * @return 28 | */ 29 | CompletableFuture upload(StorageServer server, File file) { 30 | return executor.execute( 31 | server.toInetAddress(), 32 | new FileUploadEncoder(file, server.pathIdx()), 33 | FileIdDecoder.INSTANCE 34 | ); 35 | } 36 | 37 | /** 38 | * 上传文件,其中文件内容字段 content 的支持以下类型: 39 | *

40 | *

    41 | *
  • {@link java.io.File}
  • 42 | *
  • {@link java.io.InputStream}
  • 43 | *
  • byte[]
  • 44 | *
  • {@link java.nio.channels.ReadableByteChannel}
  • 45 | *
46 | * 47 | * @param server 存储服务器信息,应该由 tracker 查询得到 48 | * @param content 上传内容 49 | * @param filename 扩展名 50 | * @param size 内容长度 51 | */ 52 | CompletableFuture upload(StorageServer server, Object content, String filename, long size) { 53 | return executor.execute( 54 | server.toInetAddress(), 55 | new FileUploadEncoder(content, filename, size, server.pathIdx()), 56 | FileIdDecoder.INSTANCE 57 | ); 58 | } 59 | 60 | /** 61 | * 上传可追加文件内容 62 | * 63 | * @param server 存储服务器信息,应该由 tracker 查询得到 64 | * @param file 文件 65 | */ 66 | CompletableFuture uploadAppender(StorageServer server, File file) { 67 | return executor.execute( 68 | server.toInetAddress(), 69 | new FileUploadAppenderEncoder(file, server.pathIdx()), 70 | FileIdDecoder.INSTANCE 71 | ); 72 | } 73 | 74 | /** 75 | * 上传可追加文件内容,其中文件内容字段 content 的支持以下类型: 76 | *

77 | *

    78 | *
  • {@link java.io.File}
  • 79 | *
  • {@link java.io.InputStream}
  • 80 | *
  • byte[]
  • 81 | *
  • {@link java.nio.channels.ReadableByteChannel}
  • 82 | *
83 | * 84 | * @param server 存储服务器信息,应该由 tracker 查询得到 85 | * @param content 上传内容 86 | * @param size 内容长度 87 | * @param filename 扩展名 88 | */ 89 | CompletableFuture uploadAppender(StorageServer server, Object content, String filename, long size) { 90 | return executor.execute( 91 | server.toInetAddress(), 92 | new FileUploadAppenderEncoder(content, filename, size, server.pathIdx()), 93 | FileIdDecoder.INSTANCE 94 | ); 95 | } 96 | 97 | /** 98 | * @param server 99 | * @param fileId 100 | * @param file 101 | * @return 102 | */ 103 | CompletableFuture append(StorageServer server, FileId fileId, File file) { 104 | return executor.execute( 105 | server.toInetAddress(), 106 | new FileAppendEncoder(fileId, file), 107 | Replier.NOPDecoder.INSTANCE 108 | ); 109 | } 110 | 111 | /** 112 | * 追加文件内容 113 | * 114 | * @param server 存储服务器信息,应该由 tracker 查询得到 115 | * @param fileId 服务器存储路径 116 | * @param bytes 内容字节数组 117 | * @return 118 | */ 119 | CompletableFuture append(StorageServer server, FileId fileId, byte[] bytes) { 120 | return executor.execute( 121 | server.toInetAddress(), 122 | new FileAppendEncoder(fileId, bytes, bytes.length), 123 | Replier.NOPDecoder.INSTANCE 124 | ); 125 | } 126 | 127 | /** 128 | * @param server 129 | * @param fileId 130 | * @param content 131 | * @param size 132 | * @return 133 | */ 134 | CompletableFuture append(StorageServer server, FileId fileId, Object content, long size) { 135 | return executor.execute( 136 | server.toInetAddress(), 137 | new FileAppendEncoder(fileId, content, size), 138 | Replier.NOPDecoder.INSTANCE 139 | ); 140 | } 141 | 142 | /** 143 | * @param server 144 | * @param fileId 145 | * @param file 146 | * @param offset 147 | * @return 148 | */ 149 | CompletableFuture modify(StorageServer server, FileId fileId, File file, long offset) { 150 | return executor.execute( 151 | server.toInetAddress(), 152 | new FileModifyEncoder(fileId, file, offset), 153 | Replier.NOPDecoder.INSTANCE 154 | ); 155 | } 156 | 157 | /** 158 | * 修改文件内容 159 | * 160 | * @param server 存储服务器信息,应该由 tracker 查询得到 161 | * @param fileId 服务器存储路径 162 | * @param offset 偏移量 163 | * @param bytes 内容字节数组 164 | * @return 165 | */ 166 | CompletableFuture modify(StorageServer server, FileId fileId, byte[] bytes, long offset) { 167 | return executor.execute( 168 | server.toInetAddress(), 169 | new FileModifyEncoder(fileId, bytes, bytes.length, offset), 170 | Replier.NOPDecoder.INSTANCE 171 | ); 172 | } 173 | 174 | /** 175 | * @param server 176 | * @param fileId 177 | * @param content 178 | * @param size 179 | * @param offset 180 | * @return 181 | */ 182 | CompletableFuture modify(StorageServer server, FileId fileId, Object content, long size, long offset) { 183 | return executor.execute( 184 | server.toInetAddress(), 185 | new FileModifyEncoder(fileId, content, size, offset), 186 | Replier.NOPDecoder.INSTANCE 187 | ); 188 | } 189 | 190 | /** 191 | * 删除文件 192 | * 193 | * @param server 存储服务器信息,应该由 tracker 查询得到 194 | * @param fileId 服务器存储路径 195 | */ 196 | CompletableFuture delete(StorageServer server, FileId fileId) { 197 | return executor.execute( 198 | server.toInetAddress(), 199 | new FileDeleteEncoder(fileId), 200 | Replier.NOPDecoder.INSTANCE 201 | ); 202 | } 203 | 204 | /** 205 | * 截取文件 206 | * 207 | * @param server 存储服务器信息,应该由 tracker 查询得到 208 | * @param fileId 服务器存储路径 209 | */ 210 | CompletableFuture truncate(StorageServer server, FileId fileId) { 211 | return truncate(server, fileId, 0L); 212 | } 213 | 214 | /** 215 | * 截取文件 216 | * 217 | * @param server 存储服务器信息,应该由 tracker 查询得到 218 | * @param fileId 服务器存储路径 219 | * @param truncatedSize 截取文件大小 220 | */ 221 | CompletableFuture truncate(StorageServer server, FileId fileId, long truncatedSize) { 222 | return executor.execute( 223 | server.toInetAddress(), 224 | new FileTruncateEncoder(fileId, truncatedSize), 225 | Replier.NOPDecoder.INSTANCE 226 | ); 227 | } 228 | 229 | /** 230 | * 下载文件,其输出 output 参数支持以下类型 231 | *

232 | *

    233 | *
  • {@link java.io.OutputStream}
  • 234 | *
  • {@link java.nio.channels.GatheringByteChannel}
  • 235 | *
236 | * 237 | * @param server 存储服务器信息,应该由 tracker 查询得到 238 | * @param fileId 服务器存储路径 239 | * @param output 输出流 240 | * @return 下载进度 241 | */ 242 | CompletableFuture download(StorageServer server, FileId fileId, Object output) { 243 | return executor.execute( 244 | server.toInetAddress(), 245 | new FileDownloadEncoder(fileId), 246 | StreamReplier.stream(output) 247 | ); 248 | } 249 | 250 | /** 251 | * 下载文件内容 252 | * 253 | * @param server 存储服务器信息,应该由 tracker 查询得到 254 | * @param fileId 服务器存储路径 255 | * @param offset 字节偏移量 256 | * @param size 下载字节数 257 | * @param output 输出流 258 | * @return 下载进度 259 | */ 260 | CompletableFuture download(StorageServer server, FileId fileId, Object output, long offset, long size) { 261 | return executor.execute( 262 | server.toInetAddress(), 263 | new FileDownloadEncoder(fileId, offset, size), 264 | StreamReplier.stream(output) 265 | ); 266 | } 267 | 268 | /** 269 | * 设置文件元数据 270 | * 271 | * @param server 存储服务器信息,应该由 tracker 查询得到 272 | * @param fileId 服务器存储路径 273 | * @param metadata 元数据 274 | * @param flag 设置标识 275 | * @return 276 | */ 277 | CompletableFuture setMetadata(StorageServer server, FileId fileId, FileMetadata metadata, byte flag) { 278 | return executor.execute( 279 | server.toInetAddress(), 280 | new FileMetadataSetEncoder(fileId, metadata, flag), 281 | Replier.NOPDecoder.INSTANCE 282 | ); 283 | } 284 | 285 | /** 286 | * 获取文件元数据 287 | * 288 | * @param server 存储服务器信息,应该由 tracker 查询得到 289 | * @param fileId 服务器存储路径 290 | * @return 291 | */ 292 | CompletableFuture getMetadata(StorageServer server, FileId fileId) { 293 | return executor.execute( 294 | server.toInetAddress(), 295 | new FileMetadataGetEncoder(fileId), 296 | FileMetadataDecoder.INSTANCE 297 | ); 298 | } 299 | 300 | /** 301 | * @param server 302 | * @param fileId 303 | * @return 304 | */ 305 | CompletableFuture getInfo(StorageServer server, FileId fileId) { 306 | return executor.execute( 307 | server.toInetAddress(), 308 | new FileInfoGetEncoder(fileId), 309 | FileInfoDecoder.INSTANCE 310 | ); 311 | } 312 | } 313 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/StorageServer.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | import java.net.InetSocketAddress; 7 | 8 | 9 | /** 10 | * 存储服务器信息 11 | * 12 | * @author liulongbiao 13 | */ 14 | public class StorageServer { 15 | 16 | private final String group; 17 | private final String host; 18 | private final int port; 19 | private final byte pathIdx; 20 | 21 | /** 22 | * @param group 23 | * @param host 24 | * @param port 25 | * @param pathIdx 26 | */ 27 | public StorageServer(String group, String host, int port, byte pathIdx) { 28 | this.group = group; 29 | this.host = host; 30 | this.port = port; 31 | this.pathIdx = pathIdx; 32 | } 33 | 34 | /** 35 | * @param group 36 | * @param host 37 | * @param port 38 | */ 39 | public StorageServer(String group, String host, int port) { 40 | this(group, host, port, (byte) 0); 41 | } 42 | 43 | /** 44 | * @return 45 | */ 46 | public String group() { 47 | return group; 48 | } 49 | 50 | /** 51 | * @return 52 | */ 53 | public String host() { 54 | return host; 55 | } 56 | 57 | /** 58 | * @return 59 | */ 60 | public int port() { 61 | return port; 62 | } 63 | 64 | /** 65 | * @return 66 | */ 67 | public byte pathIdx() { 68 | return pathIdx; 69 | } 70 | 71 | @Override 72 | public String toString() { 73 | return '[' + group + ']' + host + ':' + port + '/' + pathIdx; 74 | } 75 | 76 | /** 77 | * 获取通信地址 78 | * 79 | * @return 80 | */ 81 | public InetSocketAddress toInetAddress() { 82 | return new InetSocketAddress(host, port); 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/TrackerClient.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client; 5 | 6 | import fastdfs.client.codec.*; 7 | 8 | import java.io.Closeable; 9 | import java.io.IOException; 10 | import java.net.InetSocketAddress; 11 | import java.util.List; 12 | import java.util.concurrent.CompletableFuture; 13 | 14 | 15 | final class TrackerClient implements Closeable { 16 | 17 | private final FastdfsExecutor executor; 18 | private final TrackerSelector selector; 19 | private final TrackerMonitor monitor; 20 | 21 | TrackerClient(FastdfsExecutor executor, FastdfsScheduler scheduler, TrackerSelector selector, List servers, int fall, int rise, long checkTimeout, long checkInterval) { 22 | this.executor = executor; 23 | this.selector = servers.size() == 1 ? TrackerSelector.FIRST : selector; 24 | this.monitor = new TrackerMonitor(executor, scheduler, servers, fall, rise, checkTimeout, checkInterval); 25 | } 26 | 27 | private CompletableFuture trackerSelect() { 28 | CompletableFuture promise = new CompletableFuture<>(); 29 | try { 30 | promise.complete(monitor.trackerSelect(selector).toInetAddress()); 31 | } catch (Exception e) { 32 | promise.completeExceptionally(e); 33 | } 34 | return promise; 35 | } 36 | 37 | /** 38 | * @return 39 | */ 40 | CompletableFuture uploadStorageGet() { 41 | return uploadStorageGet(null); 42 | } 43 | 44 | /** 45 | * @param group 46 | * @return 47 | */ 48 | CompletableFuture uploadStorageGet(String group) { 49 | return trackerSelect().thenCompose(addr -> executor.execute(addr, new UploadStorageGetEncoder(group), StorageServerDecoder.INSTANCE)); 50 | } 51 | 52 | /** 53 | * @param fileId 54 | * @return 55 | */ 56 | CompletableFuture downloadStorageGet(FileId fileId) { 57 | CompletableFuture> result = trackerSelect().thenCompose((addr -> executor.execute(addr, new DownloadStorageGetEncoder(fileId), StorageServerListDecoder.INSTANCE))); 58 | return result.thenApply(FastdfsUtils::first); 59 | } 60 | 61 | /** 62 | * 获取更新存储服务器地址 63 | * 64 | * @param fileId 65 | */ 66 | CompletableFuture updateStorageGet(FileId fileId) { 67 | CompletableFuture> result = trackerSelect().thenCompose(addr -> executor.execute(addr, new UpdateStorageGetEncoder(fileId), StorageServerListDecoder.INSTANCE)); 68 | return result.thenApply(FastdfsUtils::first); 69 | } 70 | 71 | /** 72 | * @param fileId 73 | * @return 74 | */ 75 | CompletableFuture> downloadStorageList(FileId fileId) { 76 | return trackerSelect().thenCompose(addr -> executor.execute(addr, new DownloadStorageListEncoder(fileId), StorageServerListDecoder.INSTANCE)); 77 | } 78 | 79 | @Override 80 | public void close() throws IOException { 81 | try { 82 | monitor.close(); 83 | } catch (Exception e) { 84 | // do nothing. 85 | } 86 | } 87 | } 88 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/TrackerMonitor.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client; 2 | 3 | import fastdfs.client.codec.ActiveTestReplier; 4 | import fastdfs.client.codec.ActiveTestRequestor; 5 | import org.slf4j.Logger; 6 | import org.slf4j.LoggerFactory; 7 | 8 | import java.io.Closeable; 9 | import java.io.IOException; 10 | import java.util.*; 11 | import java.util.concurrent.CompletableFuture; 12 | import java.util.concurrent.ExecutionException; 13 | import java.util.concurrent.ScheduledFuture; 14 | import java.util.concurrent.TimeUnit; 15 | import java.util.concurrent.locks.ReentrantReadWriteLock; 16 | import java.util.stream.Collectors; 17 | 18 | class TrackerMonitor implements Closeable { 19 | 20 | private final int fall; 21 | private final int rise; 22 | private final List aliveServers; 23 | private final List> aliveTasks; 24 | 25 | private final Map ariseServers = new HashMap<>(); 26 | private final Map alivenessServers = new HashMap<>(); 27 | 28 | private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock(); 29 | private final Logger logger = LoggerFactory.getLogger(getClass()); 30 | 31 | TrackerMonitor(FastdfsExecutor executor, FastdfsScheduler scheduler, List servers, int fall, int rise, long checkTimeout, long checkInterval) { 32 | this.fall = fall; 33 | this.rise = rise; 34 | 35 | this.aliveServers = new LinkedList<>(servers); 36 | this.aliveTasks = new HashSet<>(servers).stream().map(server -> scheduler.scheduleAtFixedRate(() -> { 37 | CompletableFuture promise = null; 38 | try { 39 | promise = executor.execute(server.toInetAddress(), new ActiveTestRequestor(), new ActiveTestReplier()); 40 | if (promise.get(checkTimeout, TimeUnit.MILLISECONDS)) { 41 | trackerReachable(server); 42 | return; 43 | } 44 | trackerUnreachable(server, null); 45 | } catch (ExecutionException e) { 46 | trackerUnreachable(server, e.getCause()); 47 | } catch (Exception e) { 48 | trackerUnreachable(server, e); 49 | } finally { 50 | if (null != promise) { 51 | promise.cancel(true); 52 | } 53 | } 54 | }, checkInterval, checkInterval, TimeUnit.MILLISECONDS)).collect(Collectors.toList()); 55 | } 56 | 57 | TrackerServer trackerSelect(TrackerSelector selector) { 58 | try { 59 | lock.readLock().lock(); 60 | if (aliveServers.isEmpty()) { 61 | throw new TrackerNotAvailableException(); 62 | } 63 | 64 | return selector.select(aliveServers); 65 | } finally { 66 | lock.readLock().unlock(); 67 | } 68 | } 69 | 70 | private void trackerReachable(TrackerServer server) { 71 | try { 72 | lock.writeLock().lock(); 73 | if (aliveServers.contains(server)) { 74 | ariseServers.remove(server); 75 | return; 76 | } 77 | 78 | Integer ariseTimes = ariseServers.getOrDefault(server, 0); 79 | if (ariseTimes >= rise - 1) { 80 | trackerUp(server); 81 | return; 82 | } 83 | ariseServers.put(server, ariseTimes + 1); 84 | } finally { 85 | lock.writeLock().unlock(); 86 | } 87 | } 88 | 89 | private void trackerUp(TrackerServer server) { 90 | if (logger.isInfoEnabled()) { 91 | logger.info("Server[host={}, port={}] Up.", server.host(), server.port()); 92 | } 93 | 94 | ariseServers.remove(server); 95 | alivenessServers.remove(server); 96 | 97 | aliveServers.remove(server); 98 | aliveServers.add(server); 99 | } 100 | 101 | private void trackerUnreachable(TrackerServer server, Throwable e) { 102 | try { 103 | lock.writeLock().lock(); 104 | if (!aliveServers.contains(server)) { 105 | ariseServers.remove(server); 106 | return; 107 | } 108 | 109 | Integer alivenessTimes = alivenessServers.getOrDefault(server, 0); 110 | if (alivenessTimes >= fall - 1) { 111 | trackerDown(server, e); 112 | return; 113 | } 114 | alivenessServers.put(server, alivenessTimes + 1); 115 | } finally { 116 | lock.writeLock().unlock(); 117 | } 118 | } 119 | 120 | private void trackerDown(TrackerServer server, Throwable e) { 121 | if (logger.isInfoEnabled()) { 122 | logger.info("Server[host={}, port={}] Down. Cause by: {}.", server.host(), server.port(), e == null ? "" : e.getMessage()); 123 | } 124 | aliveServers.remove(server); 125 | ariseServers.remove(server); 126 | alivenessServers.remove(server); 127 | } 128 | 129 | @Override 130 | public void close() throws IOException { 131 | aliveTasks.forEach(task -> { 132 | try { 133 | task.cancel(true); 134 | } catch (Exception e) { 135 | // do nothing. 136 | } 137 | }); 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/TrackerNotAvailableException.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client; 2 | 3 | public class TrackerNotAvailableException extends FastdfsException { 4 | } 5 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/TrackerSelector.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client; 2 | 3 | import java.util.List; 4 | import java.util.Random; 5 | 6 | 7 | public enum TrackerSelector { 8 | 9 | ROUND_ROBIN { 10 | private int idx; 11 | 12 | @Override 13 | public TrackerServer select(List list) { 14 | idx %= list.size(); 15 | return list.get(idx++); 16 | } 17 | 18 | }, 19 | RANDOM { 20 | private final Random random = new Random(); 21 | 22 | @Override 23 | public TrackerServer select(List list) { 24 | return list.get(random.nextInt(list.size())); 25 | } 26 | 27 | }, 28 | FIRST { 29 | @Override 30 | TrackerServer select(List list) { 31 | return list.get(0); 32 | } 33 | 34 | }; 35 | 36 | abstract TrackerServer select(List list); 37 | } -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/TrackerServer.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client; 2 | 3 | import java.net.InetSocketAddress; 4 | import java.util.Objects; 5 | 6 | /** 7 | * @author siuming 8 | */ 9 | public class TrackerServer { 10 | 11 | private String host; 12 | private int port; 13 | private int weight; 14 | 15 | /** 16 | * @param host 17 | * @param port 18 | */ 19 | public TrackerServer(String host, int port) { 20 | this(host, port, -1); 21 | } 22 | 23 | /** 24 | * @param host 25 | * @param port 26 | * @param weight 27 | */ 28 | public TrackerServer(String host, int port, int weight) { 29 | this.host = host; 30 | this.port = port; 31 | this.weight = weight; 32 | } 33 | 34 | /** 35 | * @return 36 | */ 37 | public String host() { 38 | return host; 39 | } 40 | 41 | /** 42 | * @return 43 | */ 44 | public int port() { 45 | return port; 46 | } 47 | 48 | /** 49 | * @return 50 | */ 51 | public int weight() { 52 | return weight; 53 | } 54 | 55 | /** 56 | * @return 57 | */ 58 | public InetSocketAddress toInetAddress() { 59 | return new InetSocketAddress(host, port); 60 | } 61 | 62 | @Override 63 | public boolean equals(Object o) { 64 | if (this == o) return true; 65 | if (o == null || getClass() != o.getClass()) return false; 66 | TrackerServer that = (TrackerServer) o; 67 | return port == that.port && 68 | weight == that.weight && 69 | Objects.equals(host, that.host); 70 | } 71 | 72 | @Override 73 | public int hashCode() { 74 | return Objects.hash(host, port, weight); 75 | } 76 | 77 | @Override 78 | public String toString() { 79 | return "TrackerServer{" + 80 | "host='" + host + '\'' + 81 | ", port=" + port + 82 | ", weight=" + weight + 83 | '}'; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/ActiveTestReplier.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client.codec; 2 | 3 | import fastdfs.client.FastdfsConstants; 4 | import fastdfs.client.exchange.Replier; 5 | import io.netty.buffer.ByteBuf; 6 | 7 | import java.util.concurrent.CompletableFuture; 8 | 9 | public class ActiveTestReplier implements Replier { 10 | @Override 11 | public void reply(ByteBuf in, CompletableFuture promise) { 12 | if (in.readableBytes() < FastdfsConstants.FDFS_HEAD_LEN) { 13 | return; 14 | } 15 | byte errno = in.skipBytes(9).readByte(); 16 | promise.complete(errno == 0); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/ActiveTestRequestor.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client.codec; 2 | 3 | import fastdfs.client.exchange.Requestor; 4 | import io.netty.buffer.ByteBuf; 5 | import io.netty.channel.Channel; 6 | 7 | import static fastdfs.client.FastdfsConstants.*; 8 | 9 | public class ActiveTestRequestor implements Requestor { 10 | 11 | @Override 12 | public void request(Channel channel) { 13 | ByteBuf buf = channel.alloc().buffer(FDFS_HEAD_LEN); 14 | buf.writeLong(0L); 15 | buf.writeByte(Commands.ACTIVE_TEST); 16 | buf.writeByte(ERRNO_OK); 17 | channel.writeAndFlush(buf); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/DownloadStorageGetEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FileId; 7 | 8 | import static fastdfs.client.FastdfsConstants.Commands.SERVICE_QUERY_FETCH_ONE; 9 | 10 | /** 11 | * 获取可下载的存储服务器 12 | * 13 | * @author liulongbiao 14 | */ 15 | public class DownloadStorageGetEncoder extends FileIdOperationEncoder { 16 | 17 | public DownloadStorageGetEncoder(FileId fileId) { 18 | super(fileId); 19 | } 20 | 21 | @Override 22 | protected byte cmd() { 23 | return SERVICE_QUERY_FETCH_ONE; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/DownloadStorageListEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FileId; 7 | 8 | import static fastdfs.client.FastdfsConstants.Commands.SERVICE_QUERY_FETCH_ALL; 9 | 10 | /** 11 | * 获取可下载的存储服务器列表 12 | * 13 | * @author liulongbiao 14 | */ 15 | public class DownloadStorageListEncoder extends FileIdOperationEncoder { 16 | 17 | public DownloadStorageListEncoder(FileId fileId) { 18 | super(fileId); 19 | } 20 | 21 | @Override 22 | protected byte cmd() { 23 | return SERVICE_QUERY_FETCH_ALL; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileAppendEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FastdfsConstants; 7 | import fastdfs.client.FileId; 8 | import io.netty.buffer.ByteBuf; 9 | import io.netty.buffer.ByteBufAllocator; 10 | 11 | import java.io.File; 12 | 13 | /** 14 | * @author liulongbiao 15 | */ 16 | public class FileAppendEncoder extends FileOperationEncoder { 17 | 18 | private final FileId fileId; 19 | 20 | /** 21 | * @param fileId 22 | * @param file 23 | */ 24 | public FileAppendEncoder(FileId fileId, File file) { 25 | super(file); 26 | this.fileId = fileId; 27 | } 28 | 29 | /** 30 | * @param fileId 31 | * @param content 32 | * @param size 33 | */ 34 | public FileAppendEncoder(FileId fileId, Object content, long size) { 35 | super(content, size); 36 | this.fileId = fileId; 37 | } 38 | 39 | @Override 40 | protected byte cmd() { 41 | return FastdfsConstants.Commands.FILE_APPEND; 42 | } 43 | 44 | @Override 45 | protected ByteBuf metadata(ByteBufAllocator alloc) { 46 | byte[] pathBytes = fileId.pathBytes(); 47 | 48 | int metaSize = 2 * FastdfsConstants.FDFS_PROTO_PKG_LEN_SIZE + pathBytes.length; 49 | ByteBuf buf = alloc.buffer(metaSize); 50 | buf.writeLong(pathBytes.length); 51 | buf.writeLong(size()); 52 | buf.writeBytes(pathBytes); 53 | return buf; 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileDeleteEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FastdfsConstants; 7 | import fastdfs.client.FileId; 8 | 9 | /** 10 | * 删除请求 11 | * 12 | * @author liulongbiao 13 | */ 14 | public class FileDeleteEncoder extends FileIdOperationEncoder { 15 | 16 | public FileDeleteEncoder(FileId fileId) { 17 | super(fileId); 18 | } 19 | 20 | @Override 21 | protected byte cmd() { 22 | return FastdfsConstants.Commands.FILE_DELETE; 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileDownloadEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FastdfsConstants; 7 | import fastdfs.client.FileId; 8 | import fastdfs.client.exchange.Requestor; 9 | import io.netty.buffer.ByteBuf; 10 | import io.netty.buffer.ByteBufAllocator; 11 | import io.netty.buffer.ByteBufUtil; 12 | 13 | import java.util.Collections; 14 | import java.util.List; 15 | 16 | import static fastdfs.client.FastdfsConstants.*; 17 | import static fastdfs.client.FastdfsUtils.writeFixLength; 18 | 19 | /** 20 | * 下载请求 21 | * 22 | * @author liulongbiao 23 | */ 24 | public class FileDownloadEncoder implements Requestor.Encoder { 25 | 26 | private static final int DEFAULT_OFFSET = 0; 27 | private static final int DEFAULT_SIZE = 0; 28 | 29 | private final FileId fileId; 30 | private final long offset; 31 | private final long size; 32 | 33 | /** 34 | * @param fileId 35 | */ 36 | public FileDownloadEncoder(FileId fileId) { 37 | this(fileId, DEFAULT_OFFSET, DEFAULT_SIZE); 38 | } 39 | 40 | /** 41 | * @param fileId 42 | * @param offset 43 | * @param size 44 | */ 45 | public FileDownloadEncoder(FileId fileId, long offset, long size) { 46 | this.fileId = fileId; 47 | this.offset = offset; 48 | this.size = size; 49 | } 50 | 51 | @Override 52 | public List encode(ByteBufAllocator alloc) { 53 | byte[] pathBytes = fileId.pathBytes(); 54 | int length = 2 * FDFS_LONG_LEN + FDFS_GROUP_LEN + pathBytes.length; 55 | byte cmd = FastdfsConstants.Commands.FILE_DOWNLOAD; 56 | 57 | ByteBuf buf = alloc.buffer(length + FDFS_HEAD_LEN); 58 | buf.writeLong(length); 59 | buf.writeByte(cmd); 60 | buf.writeByte(ERRNO_OK); 61 | 62 | buf.writeLong(offset); 63 | buf.writeLong(size); 64 | writeFixLength(buf, fileId.group(), FDFS_GROUP_LEN); 65 | ByteBufUtil.writeUtf8(buf, fileId.path()); 66 | return Collections.singletonList(buf); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileIdDecoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FastdfsException; 7 | import fastdfs.client.FileId; 8 | import fastdfs.client.exchange.Replier; 9 | import io.netty.buffer.ByteBuf; 10 | 11 | import static fastdfs.client.FastdfsConstants.FDFS_GROUP_LEN; 12 | import static fastdfs.client.FastdfsUtils.readString; 13 | 14 | /** 15 | * 存储路径解码器 16 | * 17 | * @author liulongbiao 18 | */ 19 | public enum FileIdDecoder implements Replier.Decoder { 20 | 21 | INSTANCE; 22 | 23 | @Override 24 | public FileId decode(ByteBuf in) { 25 | int length = in.readableBytes(); 26 | if (length <= FDFS_GROUP_LEN) { 27 | throw new FastdfsException("body length : " + length + ", is lte required group name length 16."); 28 | } 29 | String group = readString(in, FDFS_GROUP_LEN); 30 | String path = readString(in); 31 | return new FileId(group, path); 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileIdOperationEncoder.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client.codec; 2 | 3 | import fastdfs.client.FileId; 4 | import fastdfs.client.exchange.Requestor; 5 | import io.netty.buffer.ByteBuf; 6 | import io.netty.buffer.ByteBufAllocator; 7 | import io.netty.buffer.ByteBufUtil; 8 | 9 | import java.util.Collections; 10 | import java.util.List; 11 | 12 | import static fastdfs.client.FastdfsConstants.*; 13 | import static fastdfs.client.FastdfsUtils.writeFixLength; 14 | 15 | /** 16 | * @author siuming 17 | */ 18 | public abstract class FileIdOperationEncoder implements Requestor.Encoder { 19 | 20 | private final FileId fileId; 21 | 22 | /** 23 | * @param fileId 24 | */ 25 | protected FileIdOperationEncoder(FileId fileId) { 26 | this.fileId = fileId; 27 | } 28 | 29 | @Override 30 | public List encode(ByteBufAllocator alloc) { 31 | byte cmd = cmd(); 32 | int length = FDFS_GROUP_LEN + fileId.pathBytes().length; 33 | ByteBuf buf = alloc.buffer(length + FDFS_HEAD_LEN); 34 | buf.writeLong(length); 35 | buf.writeByte(cmd); 36 | buf.writeByte(ERRNO_OK); 37 | writeFixLength(buf, fileId.group(), FDFS_GROUP_LEN); 38 | ByteBufUtil.writeUtf8(buf, fileId.path()); 39 | return Collections.singletonList(buf); 40 | } 41 | 42 | /** 43 | * @return 44 | */ 45 | protected abstract byte cmd(); 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileInfoDecoder.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client.codec; 2 | 3 | import fastdfs.client.FastdfsUtils; 4 | import fastdfs.client.FileInfo; 5 | import fastdfs.client.exchange.Replier; 6 | import io.netty.buffer.ByteBuf; 7 | 8 | /** 9 | * @author siuming 10 | */ 11 | public enum FileInfoDecoder implements Replier.Decoder { 12 | 13 | INSTANCE; 14 | 15 | @Override 16 | public FileInfo decode(ByteBuf buf) { 17 | long fileSize = buf.readLong(); 18 | long createTime = buf.readLong(); 19 | long crc32 = buf.readLong(); 20 | String address = FastdfsUtils.readString(buf, 16); 21 | return FileInfo.newBuilder() 22 | .fileSize(fileSize) 23 | .createTime(createTime) 24 | .crc32(crc32) 25 | .address(address) 26 | .build(); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileInfoGetEncoder.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client.codec; 2 | 3 | import fastdfs.client.FastdfsConstants; 4 | import fastdfs.client.FileId; 5 | 6 | /** 7 | * @author siuming 8 | */ 9 | public class FileInfoGetEncoder extends FileIdOperationEncoder { 10 | 11 | /** 12 | * @param fileId 13 | */ 14 | public FileInfoGetEncoder(FileId fileId) { 15 | super(fileId); 16 | } 17 | 18 | @Override 19 | protected byte cmd() { 20 | return FastdfsConstants.Commands.FILE_QUERY; 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileMetadataDecoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FileMetadata; 7 | import fastdfs.client.exchange.Replier; 8 | import io.netty.buffer.ByteBuf; 9 | 10 | import java.util.HashMap; 11 | import java.util.Map; 12 | 13 | import static fastdfs.client.FastdfsConstants.FDFS_FIELD_SEPERATOR; 14 | import static fastdfs.client.FastdfsConstants.FDFS_RECORD_SEPERATOR; 15 | import static fastdfs.client.FastdfsUtils.readString; 16 | 17 | /** 18 | * 文件属性解码器 19 | * 20 | * @author liulongbiao 21 | */ 22 | public enum FileMetadataDecoder implements Replier.Decoder { 23 | INSTANCE; 24 | 25 | @Override 26 | public FileMetadata decode(ByteBuf buf) { 27 | String content = readString(buf); 28 | 29 | Map values = new HashMap<>(); 30 | String[] pairs = content.split(FDFS_RECORD_SEPERATOR); 31 | for (String pair : pairs) { 32 | String[] kv = pair.split(FDFS_FIELD_SEPERATOR, 2); 33 | if (kv.length == 2) { 34 | values.put(kv[0], kv[1]); 35 | } 36 | } 37 | return new FileMetadata(values); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileMetadataGetEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FastdfsConstants; 7 | import fastdfs.client.FileId; 8 | 9 | /** 10 | * 获取文件属性请求 11 | * 12 | * @author liulongbiao 13 | */ 14 | public class FileMetadataGetEncoder extends FileIdOperationEncoder { 15 | 16 | public FileMetadataGetEncoder(FileId fileId) { 17 | super(fileId); 18 | } 19 | 20 | @Override 21 | public byte cmd() { 22 | return FastdfsConstants.Commands.METADATA_GET; 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileMetadataSetEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FastdfsConstants; 7 | import fastdfs.client.FileId; 8 | import fastdfs.client.FileMetadata; 9 | import fastdfs.client.exchange.Requestor; 10 | import io.netty.buffer.ByteBuf; 11 | import io.netty.buffer.ByteBufAllocator; 12 | 13 | import java.util.Collections; 14 | import java.util.List; 15 | import java.util.Objects; 16 | 17 | import static fastdfs.client.FastdfsConstants.*; 18 | import static fastdfs.client.FastdfsUtils.writeFixLength; 19 | import static io.netty.util.CharsetUtil.UTF_8; 20 | 21 | /** 22 | * 设置文件属性请求 23 | * 24 | * @author liulongbiao 25 | */ 26 | public class FileMetadataSetEncoder implements Requestor.Encoder { 27 | 28 | private final FileId fileId; 29 | private final FileMetadata metadata; 30 | private final byte flag; 31 | 32 | /** 33 | * @param fileId 34 | * @param metadata 35 | * @param flag 36 | */ 37 | public FileMetadataSetEncoder(FileId fileId, FileMetadata metadata, byte flag) { 38 | this.fileId = Objects.requireNonNull(fileId); 39 | this.metadata = metadata; 40 | this.flag = flag; 41 | } 42 | 43 | @Override 44 | public List encode(ByteBufAllocator alloc) { 45 | byte[] pathBytes = fileId.pathBytes(); 46 | byte[] metadatas = metadata.toBytes(UTF_8); 47 | int length = 2 * FDFS_LONG_LEN + 1 + FDFS_GROUP_LEN + pathBytes.length + metadatas.length; 48 | byte cmd = FastdfsConstants.Commands.METADATA_SET; 49 | 50 | ByteBuf buf = alloc.buffer(length + FDFS_HEAD_LEN); 51 | buf.writeLong(length); 52 | buf.writeByte(cmd); 53 | buf.writeByte(ERRNO_OK); 54 | 55 | buf.writeLong(pathBytes.length); 56 | buf.writeLong(metadatas.length); 57 | buf.writeByte(flag); 58 | writeFixLength(buf, fileId.group(), FDFS_GROUP_LEN); 59 | buf.writeBytes(pathBytes); 60 | buf.writeBytes(metadatas); 61 | return Collections.singletonList(buf); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileModifyEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FastdfsConstants; 7 | import fastdfs.client.FileId; 8 | import io.netty.buffer.ByteBuf; 9 | import io.netty.buffer.ByteBufAllocator; 10 | 11 | import java.io.File; 12 | 13 | /** 14 | * 修改文件请求 15 | * 16 | * @author liulongbiao 17 | */ 18 | public class FileModifyEncoder extends FileOperationEncoder { 19 | 20 | private final FileId fileId; 21 | private final long offset; 22 | 23 | public FileModifyEncoder(FileId fileId, File file, long offset) { 24 | super(file); 25 | this.fileId = fileId; 26 | this.offset = offset; 27 | } 28 | 29 | public FileModifyEncoder(FileId fileId, Object content, long size, long offset) { 30 | super(content, size); 31 | this.fileId = fileId; 32 | this.offset = offset; 33 | } 34 | 35 | @Override 36 | protected byte cmd() { 37 | return FastdfsConstants.Commands.FILE_MODIFY; 38 | } 39 | 40 | @Override 41 | protected ByteBuf metadata(ByteBufAllocator alloc) { 42 | byte[] pathBytes = fileId.pathBytes(); 43 | int metaLen = 3 * FastdfsConstants.FDFS_PROTO_PKG_LEN_SIZE + pathBytes.length; 44 | ByteBuf buf = alloc.buffer(metaLen); 45 | buf.writeLong(pathBytes.length); 46 | buf.writeLong(offset); 47 | buf.writeLong(size()); 48 | buf.writeBytes(pathBytes); 49 | return buf; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileOperationEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.exchange.Requestor; 7 | import io.netty.buffer.ByteBuf; 8 | import io.netty.buffer.ByteBufAllocator; 9 | import io.netty.buffer.CompositeByteBuf; 10 | import io.netty.buffer.Unpooled; 11 | import io.netty.channel.DefaultFileRegion; 12 | import io.netty.handler.stream.ChunkedNioStream; 13 | import io.netty.handler.stream.ChunkedStream; 14 | 15 | import java.io.File; 16 | import java.io.InputStream; 17 | import java.nio.channels.ReadableByteChannel; 18 | import java.util.LinkedList; 19 | import java.util.List; 20 | 21 | import static fastdfs.client.FastdfsConstants.ERRNO_OK; 22 | import static fastdfs.client.FastdfsConstants.FDFS_HEAD_LEN; 23 | 24 | /** 25 | * 抽象文件请求 26 | * 27 | * @author liulongbiao 28 | */ 29 | abstract class FileOperationEncoder implements Requestor.Encoder { 30 | 31 | private final Object content; 32 | private final long size; 33 | 34 | FileOperationEncoder(File file) { 35 | long length = file.length(); 36 | this.content = new DefaultFileRegion(file, 0, length); 37 | this.size = length; 38 | } 39 | 40 | FileOperationEncoder(Object content, long size) { 41 | this.content = toContent(content); 42 | this.size = size; 43 | } 44 | 45 | private static Object toContent(Object content) { 46 | 47 | if (content instanceof File) { 48 | File file = (File) content; 49 | return new DefaultFileRegion(file, 0, file.length()); 50 | } 51 | 52 | if (content instanceof InputStream) { 53 | return new ChunkedStream((InputStream) content); 54 | } 55 | 56 | if (content instanceof ReadableByteChannel) { 57 | return new ChunkedNioStream((ReadableByteChannel) content); 58 | } 59 | 60 | if (content instanceof byte[]) { 61 | return Unpooled.wrappedBuffer((byte[]) content); 62 | } 63 | 64 | throw new IllegalArgumentException("unknown content type : " + content.getClass().getName()); 65 | } 66 | 67 | @Override 68 | public List encode(ByteBufAllocator alloc) { 69 | ByteBuf meta = metadata(alloc); 70 | 71 | ByteBuf head = alloc.buffer(FDFS_HEAD_LEN); 72 | head.writeLong(meta.readableBytes() + size); 73 | head.writeByte(cmd()); 74 | head.writeByte(ERRNO_OK); 75 | 76 | CompositeByteBuf cbb = alloc.compositeBuffer(); 77 | cbb.addComponents(head, meta); 78 | cbb.writerIndex(head.readableBytes() + meta.readableBytes()); 79 | 80 | List requests = new LinkedList<>(); 81 | requests.add(cbb); 82 | requests.add(content); 83 | return requests; 84 | } 85 | 86 | /** 87 | * @return 88 | */ 89 | protected Object content() { 90 | return content; 91 | } 92 | 93 | /** 94 | * @return 95 | */ 96 | protected long size() { 97 | return size; 98 | } 99 | 100 | /** 101 | * @return 102 | */ 103 | protected abstract byte cmd(); 104 | 105 | /** 106 | * @param alloc 107 | * @return 108 | */ 109 | protected abstract ByteBuf metadata(ByteBufAllocator alloc); 110 | } 111 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileTruncateEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FastdfsConstants; 7 | import fastdfs.client.FileId; 8 | import fastdfs.client.exchange.Requestor; 9 | import io.netty.buffer.ByteBuf; 10 | import io.netty.buffer.ByteBufAllocator; 11 | 12 | import java.util.Collections; 13 | import java.util.List; 14 | 15 | import static fastdfs.client.FastdfsConstants.*; 16 | import static io.netty.util.CharsetUtil.UTF_8; 17 | 18 | /** 19 | * 截取请求 20 | * 21 | * @author liulongbiao 22 | */ 23 | public class FileTruncateEncoder implements Requestor.Encoder { 24 | 25 | private final FileId fileId; 26 | private final long truncatedSize; 27 | 28 | public FileTruncateEncoder(FileId fileId, long truncatedSize) { 29 | this.fileId = fileId; 30 | this.truncatedSize = truncatedSize; 31 | } 32 | 33 | @Override 34 | public List encode(ByteBufAllocator alloc) { 35 | byte[] pathBytes = fileId.path().getBytes(UTF_8); 36 | int length = 2 * FDFS_PROTO_PKG_LEN_SIZE + pathBytes.length; 37 | byte cmd = FastdfsConstants.Commands.FILE_TRUNCATE; 38 | 39 | ByteBuf buf = alloc.buffer(length + FDFS_HEAD_LEN); 40 | buf.writeLong(length); 41 | buf.writeByte(cmd); 42 | buf.writeByte(ERRNO_OK); 43 | buf.writeLong(pathBytes.length); 44 | buf.writeLong(truncatedSize); 45 | buf.writeBytes(pathBytes); 46 | return Collections.singletonList(buf); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileUploadAppenderEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FastdfsConstants; 7 | 8 | import java.io.File; 9 | 10 | /** 11 | * 上传追加文件请求 12 | * 13 | * @author liulongbiao 14 | */ 15 | public class FileUploadAppenderEncoder extends FileUploadEncoder { 16 | 17 | /** 18 | * @param file 19 | * @param pathIdx 20 | */ 21 | public FileUploadAppenderEncoder(File file, byte pathIdx) { 22 | super(file, pathIdx); 23 | } 24 | 25 | /** 26 | * @param content 27 | * @param filename 28 | * @param length 29 | * @param pathIdx 30 | */ 31 | public FileUploadAppenderEncoder(Object content, String filename, long length, byte pathIdx) { 32 | super(content, filename, length, pathIdx); 33 | } 34 | 35 | @Override 36 | protected byte cmd() { 37 | return FastdfsConstants.Commands.APPENDER_FILE_UPLOAD; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/FileUploadEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FastdfsConstants; 7 | import fastdfs.client.FastdfsUtils; 8 | import io.netty.buffer.ByteBuf; 9 | import io.netty.buffer.ByteBufAllocator; 10 | 11 | import java.io.File; 12 | 13 | import static fastdfs.client.FastdfsConstants.*; 14 | import static fastdfs.client.FastdfsUtils.writeFixLength; 15 | 16 | /** 17 | * 上传请求 18 | * 19 | * @author liulongbiao 20 | */ 21 | public class FileUploadEncoder extends FileOperationEncoder { 22 | 23 | private final String ext; 24 | private final byte pathIdx; 25 | 26 | public FileUploadEncoder(File file, byte pathIdx) { 27 | super(file); 28 | this.ext = FastdfsUtils.getFileExt(file.getName(), "tmp"); 29 | this.pathIdx = pathIdx; 30 | } 31 | 32 | public FileUploadEncoder(Object content, String filename, long size, byte pathIdx) { 33 | super(content, size); 34 | this.ext = FastdfsUtils.getFileExt(filename, "tmp"); 35 | this.pathIdx = pathIdx; 36 | } 37 | 38 | @Override 39 | protected ByteBuf metadata(ByteBufAllocator alloc) { 40 | int metaLen = FDFS_STORE_PATH_INDEX_LEN + FDFS_PROTO_PKG_LEN_SIZE + FDFS_FILE_EXT_LEN; 41 | ByteBuf buf = alloc.buffer(metaLen); 42 | buf.writeByte(pathIdx); 43 | buf.writeLong(size()); 44 | writeFixLength(buf, ext, FDFS_FILE_EXT_LEN); 45 | return buf; 46 | } 47 | 48 | protected byte cmd() { 49 | return FastdfsConstants.Commands.FILE_UPLOAD; 50 | } 51 | 52 | } 53 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/StorageServerDecoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.StorageServer; 7 | import fastdfs.client.exchange.Replier; 8 | import io.netty.buffer.ByteBuf; 9 | 10 | import static fastdfs.client.FastdfsConstants.*; 11 | import static fastdfs.client.FastdfsUtils.readString; 12 | 13 | /** 14 | * 存储服务器信息解码器 15 | * 16 | * @author liulongbiao 17 | */ 18 | public enum StorageServerDecoder implements Replier.Decoder { 19 | 20 | INSTANCE; 21 | 22 | @Override 23 | public long expectLength() { 24 | return FDFS_STORAGE_STORE_LEN; 25 | } 26 | 27 | @Override 28 | public StorageServer decode(ByteBuf in) { 29 | String group = readString(in, FDFS_GROUP_LEN); 30 | if (group.indexOf('\0') > 0) { 31 | group = group.substring(0, group.indexOf('\0')); 32 | } 33 | String host = readString(in, FDFS_HOST_LEN); 34 | if (host.indexOf('\0') > 0) { 35 | host = host.substring(0, host.indexOf('\0')); 36 | } 37 | int port = (int) in.readLong(); 38 | byte idx = in.readByte(); 39 | return new StorageServer(group, host, port, idx); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/StorageServerListDecoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FastdfsException; 7 | import fastdfs.client.StorageServer; 8 | import fastdfs.client.exchange.Replier; 9 | import io.netty.buffer.ByteBuf; 10 | 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | import static fastdfs.client.FastdfsConstants.*; 15 | import static fastdfs.client.FastdfsUtils.readString; 16 | 17 | /** 18 | * 存储服务器信息列表解码器 19 | * 20 | * @author liulongbiao 21 | */ 22 | public enum StorageServerListDecoder implements Replier.Decoder> { 23 | INSTANCE; 24 | 25 | @Override 26 | public List decode(ByteBuf in) { 27 | int size = in.readableBytes(); 28 | if (size < FDFS_STORAGE_LEN) { 29 | throw new FastdfsException("body length : " + size + " is less than required length " + FDFS_STORAGE_LEN); 30 | } 31 | if ((size % FDFS_STORAGE_LEN) != 0) { 32 | throw new FastdfsException("body length : " + size + " is invalidate. "); 33 | } 34 | 35 | int count = size / FDFS_STORAGE_LEN; 36 | List results = new ArrayList(count); 37 | for (int i = 0; i < count; i++) { 38 | String group = readString(in, FDFS_GROUP_LEN); 39 | if (group.indexOf('\0') > 0) { 40 | group = group.substring(0, group.indexOf('\0')); 41 | } 42 | String host = readString(in, FDFS_HOST_LEN); 43 | if (host.indexOf('\0') > 0) { 44 | host = host.substring(0, host.indexOf('\0')); 45 | } 46 | int port = (int) in.readLong(); 47 | results.add(new StorageServer(group, host, port)); 48 | } 49 | 50 | return results; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/UpdateStorageGetEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.FileId; 7 | 8 | import static fastdfs.client.FastdfsConstants.Commands.SERVICE_QUERY_UPDATE; 9 | 10 | /** 11 | * 获取可更新的存储服务器 12 | * 13 | * @author liulongbiao 14 | */ 15 | public class UpdateStorageGetEncoder extends FileIdOperationEncoder { 16 | 17 | public UpdateStorageGetEncoder(FileId fileId) { 18 | super(fileId); 19 | } 20 | 21 | @Override 22 | protected byte cmd() { 23 | return SERVICE_QUERY_UPDATE; 24 | } 25 | 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/codec/UploadStorageGetEncoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.codec; 5 | 6 | import fastdfs.client.exchange.Requestor; 7 | import io.netty.buffer.ByteBuf; 8 | import io.netty.buffer.ByteBufAllocator; 9 | 10 | import java.util.Collections; 11 | import java.util.List; 12 | 13 | import static fastdfs.client.FastdfsConstants.Commands.SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE; 14 | import static fastdfs.client.FastdfsConstants.Commands.SERVICE_QUERY_STORE_WITH_GROUP_ONE; 15 | import static fastdfs.client.FastdfsConstants.*; 16 | import static fastdfs.client.FastdfsUtils.isEmpty; 17 | import static fastdfs.client.FastdfsUtils.writeFixLength; 18 | 19 | /** 20 | * 获取可上传的存储服务器 21 | * 22 | * @author liulongbiao 23 | */ 24 | public class UploadStorageGetEncoder implements Requestor.Encoder { 25 | 26 | private String group; 27 | 28 | public UploadStorageGetEncoder(String group) { 29 | this.group = group; 30 | } 31 | 32 | @Override 33 | public List encode(ByteBufAllocator alloc) { 34 | int length = isEmpty(group) ? 0 : FDFS_GROUP_LEN; 35 | byte cmd = isEmpty(group) ? SERVICE_QUERY_STORE_WITHOUT_GROUP_ONE : SERVICE_QUERY_STORE_WITH_GROUP_ONE; 36 | 37 | ByteBuf buf = alloc.buffer(length + FDFS_HEAD_LEN); 38 | buf.writeLong(length); 39 | buf.writeByte(cmd); 40 | buf.writeByte(ERRNO_OK); 41 | if (!isEmpty(group)) { 42 | writeFixLength(buf, group, FDFS_GROUP_LEN); 43 | } 44 | return Collections.singletonList(buf); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/exchange/Replier.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.exchange; 5 | 6 | import io.netty.buffer.ByteBuf; 7 | 8 | import java.util.concurrent.CompletableFuture; 9 | 10 | /** 11 | * 接收处理 12 | * 13 | * @author liulongbiao 14 | */ 15 | public interface Replier { 16 | 17 | /** 18 | * @param in 19 | * @param promise 20 | */ 21 | void reply(ByteBuf in, CompletableFuture promise); 22 | 23 | /** 24 | * 响应解码器 25 | * 26 | * @author liulongbiao 27 | */ 28 | interface Decoder { 29 | 30 | /** 31 | * 期待的长度值,小于 0 时不验证 32 | * 33 | * @return 34 | */ 35 | default long expectLength() { 36 | return -1; 37 | } 38 | 39 | /** 40 | * 解码响应 41 | * 42 | * @param buf 43 | * @return 44 | */ 45 | T decode(ByteBuf buf); 46 | } 47 | 48 | /** 49 | * 空响应解码器 50 | * 51 | * @author liulongbiao 52 | */ 53 | enum NOPDecoder implements Decoder { 54 | 55 | INSTANCE; 56 | 57 | @Override 58 | public long expectLength() { 59 | return 0; 60 | } 61 | 62 | @Override 63 | public Void decode(ByteBuf buf) { 64 | return null; 65 | } 66 | 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/exchange/ReplierDecoder.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.exchange; 5 | 6 | import io.netty.buffer.ByteBuf; 7 | 8 | import java.util.concurrent.CompletableFuture; 9 | 10 | /** 11 | * 默认接收器实现 12 | * 13 | * @author liulongbiao 14 | */ 15 | public class ReplierDecoder extends ReplierSupport { 16 | 17 | private Decoder decoder; 18 | 19 | public ReplierDecoder(Decoder decoder) { 20 | this.decoder = decoder; 21 | } 22 | 23 | protected long expectLength() { 24 | return decoder.expectLength(); 25 | } 26 | 27 | protected void readContent(ByteBuf in, CompletableFuture promise) { 28 | 29 | if (in.readableBytes() < length) { 30 | return; 31 | } 32 | 33 | ByteBuf buf = in.readSlice((int) length); 34 | T result = decoder.decode(buf); 35 | promise.complete(result); 36 | atHead = true; 37 | } 38 | 39 | @Override 40 | public String toString() { 41 | return "ReplierDecoder{" + 42 | "decoder=" + decoder + 43 | '}'; 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/exchange/ReplierSupport.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.exchange; 5 | 6 | import fastdfs.client.FastdfsConstants; 7 | import fastdfs.client.FastdfsException; 8 | import io.netty.buffer.ByteBuf; 9 | 10 | import java.util.concurrent.CompletableFuture; 11 | 12 | import static fastdfs.client.FastdfsConstants.Commands.RESP; 13 | 14 | /** 15 | * 抽象 Replier 基类 16 | * 17 | * @author liulongbiao 18 | */ 19 | public abstract class ReplierSupport implements Replier { 20 | 21 | protected boolean atHead = true; 22 | protected long length; 23 | 24 | @Override 25 | public void reply(ByteBuf in, CompletableFuture promise) { 26 | if (atHead) { 27 | readHead(in); 28 | } 29 | readContent(in, promise); 30 | } 31 | 32 | private void readHead(ByteBuf in) { 33 | if (in.readableBytes() < FastdfsConstants.FDFS_HEAD_LEN) { 34 | return; 35 | } 36 | length = in.readLong(); 37 | byte cmd = in.readByte(); 38 | byte errno = in.readByte(); 39 | if (errno != 0) { 40 | throw new FastdfsException("Fastdfs responsed with an error, errno is " + errno); 41 | } 42 | if (cmd != RESP) { 43 | throw new FastdfsException("Expect response command code error : " + cmd); 44 | } 45 | long expectLength = expectLength(); 46 | if (expectLength >= 0 && length != expectLength) { 47 | throw new FastdfsException("Expect response length : " + expectLength + " , but reply length : " + length); 48 | } 49 | atHead = false; 50 | } 51 | 52 | protected long expectLength() { 53 | return -1; 54 | } 55 | 56 | protected abstract void readContent(ByteBuf in, CompletableFuture promise); 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/exchange/Requestor.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.exchange; 5 | 6 | import io.netty.buffer.ByteBufAllocator; 7 | import io.netty.channel.Channel; 8 | 9 | import java.util.List; 10 | 11 | /** 12 | * 发送器 13 | * 14 | * @author liulongbiao 15 | */ 16 | public interface Requestor { 17 | 18 | /** 19 | * @param channel 20 | */ 21 | void request(Channel channel); 22 | 23 | /** 24 | * 25 | */ 26 | interface Encoder { 27 | 28 | /** 29 | * @param alloc 30 | */ 31 | List encode(ByteBufAllocator alloc); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/exchange/RequestorEncoder.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client.exchange; 2 | 3 | import io.netty.buffer.ByteBufAllocator; 4 | 5 | import java.util.List; 6 | 7 | /** 8 | * @author siuming 9 | */ 10 | public class RequestorEncoder extends RequestorSupport { 11 | 12 | private final Encoder encoder; 13 | 14 | /** 15 | * @param encoder 16 | */ 17 | public RequestorEncoder(Encoder encoder) { 18 | this.encoder = encoder; 19 | } 20 | 21 | @Override 22 | protected List writeRequests(ByteBufAllocator alloc) { 23 | return encoder.encode(alloc); 24 | } 25 | 26 | @Override 27 | public String toString() { 28 | return "RequestorEncoder{" + 29 | "encoder=" + encoder + 30 | '}'; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/exchange/RequestorSupport.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client.exchange; 2 | 3 | import io.netty.buffer.ByteBufAllocator; 4 | import io.netty.channel.Channel; 5 | 6 | import java.util.List; 7 | 8 | /** 9 | * @author siuming 10 | */ 11 | public abstract class RequestorSupport implements Requestor { 12 | 13 | @Override 14 | public void request(Channel channel) { 15 | List requests = writeRequests(channel.alloc()); 16 | requests.forEach(channel::write); 17 | channel.flush(); 18 | } 19 | 20 | /** 21 | * @param alloc 22 | */ 23 | protected abstract List writeRequests(ByteBufAllocator alloc); 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/fastdfs/client/exchange/StreamReplier.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package fastdfs.client.exchange; 5 | 6 | import fastdfs.client.FastdfsException; 7 | import io.netty.buffer.ByteBuf; 8 | 9 | import java.io.IOException; 10 | import java.io.OutputStream; 11 | import java.nio.channels.GatheringByteChannel; 12 | import java.util.Objects; 13 | import java.util.concurrent.CompletableFuture; 14 | 15 | /** 16 | * 流接收处理器 17 | * 18 | * @author liulongbiao 19 | */ 20 | public class StreamReplier extends ReplierSupport { 21 | 22 | /** 23 | * @param out 24 | * @return 25 | */ 26 | public static StreamReplier stream(Object out) { 27 | Objects.requireNonNull(out); 28 | return new StreamReplier(newSink(out)); 29 | } 30 | 31 | private static Sink newSink(Object out) { 32 | if (out instanceof OutputStream) { 33 | return new OioSink((OutputStream) out); 34 | } 35 | if (out instanceof GatheringByteChannel) { 36 | return new NioSink((GatheringByteChannel) out); 37 | } 38 | throw new FastdfsException("unknown sink output type " + out.getClass().getName()); 39 | } 40 | 41 | private final Sink sink; 42 | private long readed = 0; 43 | 44 | private StreamReplier(Sink sink) { 45 | this.sink = sink; 46 | } 47 | 48 | @Override 49 | protected void readContent(ByteBuf in, CompletableFuture promise) { 50 | try { 51 | int before = in.readableBytes(); 52 | sink.write(in); 53 | int after = in.readableBytes(); 54 | readed += before - after; 55 | 56 | if (readed >= length) { 57 | promise.complete(null); 58 | } 59 | } catch (IOException e) { 60 | throw new FastdfsException("write response to output error.", e); 61 | } 62 | } 63 | 64 | @Override 65 | public String toString() { 66 | return "StreamReplier{" + 67 | "sink=" + sink + 68 | ", readed=" + readed + 69 | '}'; 70 | } 71 | 72 | private interface Sink { 73 | void write(ByteBuf buf) throws IOException; 74 | } 75 | 76 | private static class OioSink implements Sink { 77 | private OutputStream out; 78 | 79 | OioSink(OutputStream out) { 80 | this.out = out; 81 | } 82 | 83 | @Override 84 | public void write(ByteBuf buf) throws IOException { 85 | buf.readBytes(out, buf.readableBytes()); 86 | } 87 | } 88 | 89 | private static class NioSink implements Sink { 90 | private GatheringByteChannel out; 91 | 92 | NioSink(GatheringByteChannel out) { 93 | this.out = out; 94 | } 95 | 96 | @Override 97 | public void write(ByteBuf buf) throws IOException { 98 | buf.readBytes(out, buf.readableBytes()); 99 | } 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /src/test/java/fastdfs/client/FastdfsClientTest.java: -------------------------------------------------------------------------------- 1 | package fastdfs.client; 2 | 3 | import org.junit.After; 4 | import org.junit.Before; 5 | import org.junit.Test; 6 | 7 | import java.io.File; 8 | import java.io.FileOutputStream; 9 | import java.util.concurrent.CompletableFuture; 10 | 11 | /** 12 | * @author siuming 13 | */ 14 | public class FastdfsClientTest { 15 | 16 | FastdfsClient client; 17 | 18 | @Before 19 | public void setUp() throws Exception { 20 | client = FastdfsClient.newBuilder() 21 | .connectTimeout(3000) 22 | .readTimeout(100) 23 | .maxThreads(100) 24 | .tracker("172.16.1.25", 22222) 25 | .build(); 26 | 27 | } 28 | 29 | @Test 30 | public void testUpload() throws Exception { 31 | long current = System.currentTimeMillis(); 32 | CompletableFuture path = client.upload(null, new File("/tmp/test.dmg")); 33 | System.out.println(path.get()); 34 | System.out.println("=========="); 35 | System.out.println(System.currentTimeMillis() - current + " ms"); 36 | } 37 | 38 | @Test 39 | public void testUploadAppend() throws Exception { 40 | } 41 | 42 | @Test 43 | public void testDownload() throws Exception { 44 | 45 | long current = System.currentTimeMillis(); 46 | for (int i = 0; i < 100; i++) { 47 | File file = new File("/tmp/logo2.png"); 48 | FileId path = FileId.fromString("group1/M00/00/00/ZfvfZlbz1EyAC4FPAAAWNZ1l3ec600.png"); 49 | try (FileOutputStream outputStream = new FileOutputStream(file)) { 50 | CompletableFuture promise = client.download(path, outputStream); 51 | promise.get(); 52 | } 53 | } 54 | System.out.println(System.currentTimeMillis() - current + " ms"); 55 | } 56 | 57 | @Test 58 | public void testSetMetadata() throws Exception { 59 | FileId path = FileId.fromString("group1/M00/00/00/ZfvfZlbz1EyAC4FPAAAWNZ1l3ec600.png"); 60 | FileMetadata metadata = FileMetadata.newBuilder().put("test", "test1").build(); 61 | CompletableFuture promise = client.metadataSet(path, metadata); 62 | promise.get(); 63 | } 64 | 65 | @Test 66 | public void testGetMetadata() throws Exception { 67 | FileId path = FileId.fromString("group1/M00/00/00/ZfvfZlbz1EyAC4FPAAAWNZ1l3ec600.png"); 68 | CompletableFuture promise = client.metadataGet(path); 69 | System.out.println(promise.get().values()); 70 | } 71 | 72 | @Test 73 | public void testGetInfo() throws Exception { 74 | FileId path = FileId.fromString("group1/M00/00/00/ZfvfZlbz6VuAPdosAARXBcPHPhU268.log"); 75 | CompletableFuture promise = client.infoGet(path); 76 | System.out.println(promise.get()); 77 | } 78 | 79 | @Test 80 | public void testDelete() throws Exception { 81 | FileId path = FileId.fromString("group1/M00/00/00/ZfvfZlbz7TaAeyUeAeJOH39coH0381.dmg"); 82 | client.delete(path).get(); 83 | } 84 | 85 | @After 86 | public void tearDown() throws Exception { 87 | client.close(); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /src/test/resources/logback.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | UTF-8 7 | %d [%thread] %-5level %logger{24} - %msg%n 8 | 9 | 10 | 11 | 12 | 13 | --------------------------------------------------------------------------------