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 File f = new File( file );
92 long total = f.length();
93 InputStream fis = new FileInputStream( f );
97 byte[] buffer = new byte[CHECKSUM_BUFFER_SIZE];
101 numRead = fis.read( buffer );
104 digest.update( buffer, 0, numRead );
105 totalRead += numRead;
106 progressBar.setValue( (int) ( totalRead * progressBar.getMaximum() / total ) );
109 while ( numRead != -1 );
116 return byteArrayToHexStr( digest.digest() );
119 protected static String byteArrayToHexStr( byte[] data )
123 for ( int cnt = 0; cnt < data.length; cnt++ )
125 //Deposit a byte into the 8 lsb of an int.
126 int tempInt = data[cnt] & BYTE_MASK;
128 //Get hex representation of the int as a string.
129 String tempStr = Integer.toHexString( tempInt );
131 //Append a leading 0 if necessary so that each hex string will contain 2 characters.
132 if ( tempStr.length() == 1 )
134 tempStr = "0" + tempStr;
137 //Concatenate the two characters to the output string.
138 output = output + tempStr;
141 return output.toUpperCase();