1 package org.apache.maven.archiva.applet;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
23 import java.applet.Applet;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.io.InputStream;
30 import java.security.AccessController;
31 import java.security.MessageDigest;
32 import java.security.NoSuchAlgorithmException;
33 import java.security.PrivilegedAction;
36 * Applet that takes a file on the local filesystem and checksums it for sending to the server.
39 public class ChecksumApplet
42 private static final int CHECKSUM_BUFFER_SIZE = 8192;
44 private static final int BYTE_MASK = 0xFF;
46 private JProgressBar progressBar;
50 setLayout( new BorderLayout() );
51 progressBar = new JProgressBar();
52 progressBar.setStringPainted( true );
53 add( progressBar, BorderLayout.CENTER );
54 JLabel label = new JLabel( "Checksum progress: " );
55 add( label, BorderLayout.WEST );
58 public String generateMd5( final String file )
59 throws IOException, NoSuchAlgorithmException
61 Object o = AccessController.doPrivileged( new PrivilegedAction()
67 return checksumFile( file );
69 catch ( NoSuchAlgorithmException e )
71 return "Error checksumming file: " + e.getMessage();
73 catch ( FileNotFoundException e )
75 return "Couldn't find the file. " + e.getMessage();
77 catch ( IOException e )
79 return "Error reading file: " + e.getMessage();
86 protected String checksumFile( String file )
87 throws NoSuchAlgorithmException, IOException
89 MessageDigest digest = MessageDigest.getInstance( "MD5" );
91 long total = new File( file ).length();
92 InputStream fis = new FileInputStream( file );
96 byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE];
100 numRead = fis.read( buffer );
103 digest.update( buffer, 0, numRead );
104 totalRead += numRead;
105 progressBar.setValue( (int) ( totalRead * progressBar.getMaximum() / total ) );
108 while ( numRead != -1 );
115 return byteArrayToHexStr( digest.digest() );
118 protected static String byteArrayToHexStr( byte[] data )
122 for ( int cnt = 0; cnt < data.length; cnt++ )
124 //Deposit a byte into the 8 lsb of an int.
125 int tempInt = data[cnt] & BYTE_MASK;
127 //Get hex representation of the int as a string.
128 String tempStr = Integer.toHexString( tempInt );
130 //Append a leading 0 if necessary so that each hex string will contain 2 characters.
131 if ( tempStr.length() == 1 )
133 tempStr = "0" + tempStr;
136 //Concatenate the two characters to the output string.
137 output = output + tempStr;
140 return output.toUpperCase();