77import java .io .*;
88import java .net .*;
99import java .util .*;
10- import java .util .function .Consumer ;
10+ import java .util .function .BiConsumer ;
1111import java .util .regex .Matcher ;
1212import java .util .regex .Pattern ;
1313import static java .lang .Math .min ;
@@ -171,29 +171,61 @@ private void checkFile(String filePath) throws IOException {
171171 }
172172 }
173173
174- public static void onProgress (long value ){
175- System .out .println (value + "%" );
174+ private static void displayProgressBar (long bytesReceived , long fileSize , char ch , double scale ) {
175+ int columns = getTerminalWidth ();
176+ int maxWidth = (int ) (columns * scale );
177+
178+ int filled = (int ) Math .round (maxWidth * ((double ) bytesReceived / fileSize ));
179+ int remaining = maxWidth - filled ;
180+
181+ String bar = String .valueOf (ch ).repeat (Math .max (0 , filled )) +
182+ " " .repeat (Math .max (0 , remaining ));
183+
184+ double percent = 100.0 * bytesReceived / fileSize ;
185+
186+ PrintStream out = System .out ;
187+ out .printf ("-|%s| %.1f%%\r " , bar , percent );
188+ out .flush ();
189+ }
190+
191+ private static void displayProgressBar (long bytesReceived , long fileSize ) {
192+ displayProgressBar (bytesReceived , fileSize , '#' , 0.55 );
193+ }
194+
195+ private static int getTerminalWidth () {
196+ return 80 ;
197+ }
198+
199+ public void download () throws Exception {
200+ download ("./" , title , Stream ::displayProgressBar );
176201 }
202+
177203 public void download (String path ) throws Exception {
178- startDownload (path , title , Stream ::onProgress );
204+ download (path , title , Stream ::displayProgressBar );
179205 }
180- public void download (String path , Consumer <Long > progress ) throws Exception {
181- startDownload (path , title , progress );
206+
207+ public void download (BiConsumer <Long , Long > progress ) throws Exception {
208+ download ("./" , title , progress );
209+ }
210+
211+ public void download (String path , BiConsumer <Long , Long > progress ) throws Exception {
212+ download (path , title , progress );
182213 }
214+
183215 public void download (String path , String fileName ) throws Exception {
184- startDownload (path , fileName , Stream ::onProgress );
216+ download (path , fileName , Stream ::displayProgressBar );
185217 }
186- public void download (String path , String fileName , Consumer <Long > progress ) throws Exception {
218+
219+ public void download (String path , String fileName , BiConsumer <Long , Long > progress ) throws Exception {
187220 startDownload (path , fileName , progress );
188221 }
189- private void startDownload (String path , String fileName , Consumer <Long > progress ) throws Exception {
222+
223+ private void startDownload (String path , String fileName , BiConsumer <Long , Long > progress ) throws Exception {
190224 String savePath = path + safeFileName (fileName ) + "." + subType ;
191225 if (!isOtf ){
192226 long startSize = 0 ;
193227 long stopPos ;
194228 int defaultRange = 1048576 ;
195- long progressPercentage ;
196- long lastPrintedProgress = 0 ;
197229 byte [] chunkReceived ;
198230
199231 checkFile (savePath );
@@ -205,12 +237,8 @@ private void startDownload(String path, String fileName, Consumer<Long> progress
205237 String chunk = url + "&range=" + startSize + "-" + stopPos ;
206238 chunkReceived = Request .get (chunk ).toByteArray ();
207239
208- progressPercentage = (stopPos * 100L ) / ( fileSize );
240+ progress . accept (stopPos , fileSize );
209241
210- if (progressPercentage != lastPrintedProgress ) {
211- lastPrintedProgress = progressPercentage ;
212- progress .accept (progressPercentage );
213- }
214242 startSize = startSize + chunkReceived .length ;
215243 try (FileOutputStream fos = new FileOutputStream (savePath , true )) {
216244 fos .write (chunkReceived );
@@ -221,7 +249,7 @@ private void startDownload(String path, String fileName, Consumer<Long> progress
221249 }
222250 }
223251
224- private void downloadOtf (String savePath , Consumer < Long > progress ) throws Exception {
252+ private void downloadOtf (String savePath , BiConsumer < Long , Long > progress ) throws Exception {
225253 int countChunk = 0 ;
226254 byte [] chunkReceived ;
227255 int lastChunk = 0 ;
@@ -241,7 +269,7 @@ private void downloadOtf(String savePath, Consumer<Long> progress) throws Except
241269 throw new RegexMatchError ("downloadOtf: " + pattern );
242270 }
243271 }
244- progress .accept (( countChunk * 100L ) / ( lastChunk ));
272+ progress .accept (Long . parseLong ( String . valueOf ( countChunk )), Long . parseLong ( String . valueOf ( lastChunk ) ));
245273 countChunk = countChunk + 1 ;
246274 try (FileOutputStream fos = new FileOutputStream (savePath , true )) {
247275 fos .write (chunkReceived );
0 commit comments