View Javadoc

1   package org.apache.maven.cli;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.PrintStream;
23  import java.util.Map;
24  import java.util.concurrent.ConcurrentHashMap;
25  
26  import org.sonatype.aether.transfer.TransferCancelledException;
27  import org.sonatype.aether.transfer.TransferEvent;
28  import org.sonatype.aether.transfer.TransferResource;
29  
30  /**
31   * Console download progress meter.
32   * 
33   * @author <a href="mailto:brett@apache.org">Brett Porter</a>
34   */
35  class ConsoleMavenTransferListener
36      extends AbstractMavenTransferListener
37  {
38  
39      private Map<TransferResource, Long> downloads = new ConcurrentHashMap<TransferResource, Long>();
40  
41      private int lastLength;
42  
43      public ConsoleMavenTransferListener( PrintStream out )
44      {
45          super( out );
46      }
47  
48      @Override
49      public void transferProgressed( TransferEvent event )
50          throws TransferCancelledException
51      {
52          TransferResource resource = event.getResource();
53          downloads.put( resource, Long.valueOf( event.getTransferredBytes() ) );
54  
55          StringBuilder buffer = new StringBuilder( 64 );
56  
57          for ( Map.Entry<TransferResource, Long> entry : downloads.entrySet() )
58          {
59              long total = entry.getKey().getContentLength();
60              long complete = entry.getValue().longValue();
61  
62              buffer.append( getStatus( complete, total ) ).append( "  " );
63          }
64  
65          int pad = lastLength - buffer.length();
66          lastLength = buffer.length();
67          pad( buffer, pad );
68          buffer.append( '\r' );
69  
70          out.print( buffer );
71      }
72  
73      private String getStatus( long complete, long total )
74      {
75          if ( total >= 1024 )
76          {
77              return toKB( complete ) + "/" + toKB( total ) + " KB ";
78          }
79          else if ( total >= 0 )
80          {
81              return complete + "/" + total + " B ";
82          }
83          else if ( complete >= 1024 )
84          {
85              return toKB( complete ) + " KB ";
86          }
87          else
88          {
89              return complete + " B ";
90          }
91      }
92  
93      private void pad( StringBuilder buffer, int spaces )
94      {
95          String block = "                                        ";
96          while ( spaces > 0 )
97          {
98              int n = Math.min( spaces, block.length() );
99              buffer.append( block, 0, n );
100             spaces -= n;
101         }
102     }
103 
104     @Override
105     public void transferSucceeded( TransferEvent event )
106     {
107         transferCompleted( event );
108 
109         super.transferSucceeded( event );
110     }
111 
112     @Override
113     public void transferFailed( TransferEvent event )
114     {
115         transferCompleted( event );
116 
117         super.transferFailed( event );
118     }
119 
120     private void transferCompleted( TransferEvent event )
121     {
122         downloads.remove( event.getResource() );
123 
124         StringBuilder buffer = new StringBuilder( 64 );
125         pad( buffer, lastLength );
126         buffer.append( '\r' );
127         out.print( buffer );
128     }
129 
130 }