Skip to content
This repository has been archived by the owner on Dec 4, 2024. It is now read-only.

Commit

Permalink
Reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
slachiewicz committed Dec 10, 2023
1 parent 3e38a37 commit 2f49c4f
Show file tree
Hide file tree
Showing 18 changed files with 274 additions and 346 deletions.
11 changes: 7 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-components</artifactId>
<version>14.2</version>
<relativePath/>
<relativePath />
</parent>

<artifactId>plexus-digest</artifactId>
Expand All @@ -16,8 +17,8 @@
<scm>
<connection>scm:git:[email protected]:codehaus-plexus/plexus-digest.git</connection>
<developerConnection>scm:git:[email protected]:codehaus-plexus/plexus-digest.git</developerConnection>
<url>http://github.com/codehaus-plexus/plexus-digest</url>
<tag>HEAD</tag>
<url>http://github.com/codehaus-plexus/plexus-digest</url>
</scm>
<issueManagement>
<system>github</system>
Expand Down Expand Up @@ -65,15 +66,17 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-publish-plugin</artifactId>
<configuration>
<content>${project.reporting.outputDirectory}</content><!-- mono-module doesn't require site:stage -->
<content>${project.reporting.outputDirectory}</content>
<!-- mono-module doesn't require site:stage -->
</configuration>
<executions>
<execution>
<id>scm-publish</id>
<phase>site-deploy</phase><!-- deploy site with maven-scm-publish-plugin -->
<!-- deploy site with maven-scm-publish-plugin -->
<goals>
<goal>publish-scm</goal>
</goals>
<phase>site-deploy</phase>
</execution>
</executions>
</plugin>
Expand Down
62 changes: 24 additions & 38 deletions src/main/java/org/codehaus/plexus/digest/AbstractDigester.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,27 @@
* limitations under the License.
*/

import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import org.codehaus.plexus.util.IOUtil;
import org.codehaus.plexus.util.StringUtils;

/**
* Create a digest for a file.
*
* @author <a href="mailto:[email protected]">Brett Porter</a>
*/
public abstract class AbstractDigester
implements Digester
{
public abstract class AbstractDigester implements Digester {
private final StreamingDigester streamingDigester;

/**
* <p>Constructor for AbstractDigester.</p>
*
* @param streamingDigester a {@link org.codehaus.plexus.digest.StreamingDigester} object.
*/
protected AbstractDigester( StreamingDigester streamingDigester )
{
protected AbstractDigester(StreamingDigester streamingDigester) {
this.streamingDigester = streamingDigester;
}

Expand All @@ -48,46 +45,36 @@ protected AbstractDigester( StreamingDigester streamingDigester )
*
* @return a {@link java.lang.String} object.
*/
public String getAlgorithm()
{
public String getAlgorithm() {
return streamingDigester.getAlgorithm();
}

/** {@inheritDoc} */
public String calc( File file )
throws DigesterException
{
public String calc(File file) throws DigesterException {
FileInputStream fis = null;
try
{
fis = new FileInputStream( file );
try {
fis = new FileInputStream(file);
streamingDigester.reset();
streamingDigester.update( fis );
streamingDigester.update(fis);
return streamingDigester.calc();
}
catch ( IOException e )
{
throw new DigesterException( "Unable to calculate the " + streamingDigester.getAlgorithm() +
" hashcode for " + file.getAbsolutePath() + ": " + e.getMessage(), e );
}
finally
{
IOUtil.close( fis );
} catch (IOException e) {
throw new DigesterException(
"Unable to calculate the " + streamingDigester.getAlgorithm() + " hashcode for "
+ file.getAbsolutePath() + ": " + e.getMessage(),
e);
} finally {
IOUtil.close(fis);
}
}

/** {@inheritDoc} */
public void verify( File file, String checksum )
throws DigesterException
{
String trimmedChecksum =
DigestUtils.cleanChecksum( checksum, streamingDigester.getAlgorithm(), file.getName() );
public void verify(File file, String checksum) throws DigesterException {
String trimmedChecksum = DigestUtils.cleanChecksum(checksum, streamingDigester.getAlgorithm(), file.getName());

//Create checksum for file
String sum = calc( file );
if ( !StringUtils.equalsIgnoreCase( trimmedChecksum, sum ) )
{
throw new DigesterException( "Checksum failed (expected=" + trimmedChecksum + ", actual=" + sum + ")" );
// Create checksum for file
String sum = calc(file);
if (!StringUtils.equalsIgnoreCase(trimmedChecksum, sum)) {
throw new DigesterException("Checksum failed (expected=" + trimmedChecksum + ", actual=" + sum + ")");
}
}

Expand All @@ -96,8 +83,7 @@ public void verify( File file, String checksum )
*
* @return a {@link java.lang.String} object.
*/
public String toString()
{
public String toString() {
return "[Digester:" + streamingDigester.getAlgorithm() + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@
*
* @author <a href="mailto:[email protected]">Brett Porter</a>
*/
public abstract class AbstractStreamingDigester
implements StreamingDigester
{
public abstract class AbstractStreamingDigester implements StreamingDigester {
protected final MessageDigest md;

private static final int BUFFER_SIZE = 32768;
Expand All @@ -38,16 +36,12 @@ public abstract class AbstractStreamingDigester
*
* @param algorithm a {@link java.lang.String} object.
*/
protected AbstractStreamingDigester( String algorithm )
{
try
{
md = MessageDigest.getInstance( algorithm );
}
catch ( NoSuchAlgorithmException e )
{
throw new IllegalStateException( "Unable to initialize digest algorithm " + algorithm + " : "
+ e.getMessage() );
protected AbstractStreamingDigester(String algorithm) {
try {
md = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
throw new IllegalStateException(
"Unable to initialize digest algorithm " + algorithm + " : " + e.getMessage());
}
}

Expand All @@ -56,8 +50,7 @@ protected AbstractStreamingDigester( String algorithm )
*
* @return a {@link java.lang.String} object.
*/
public String getAlgorithm()
{
public String getAlgorithm() {
return md.getAlgorithm();
}

Expand All @@ -67,28 +60,22 @@ public String getAlgorithm()
* @return a {@link java.lang.String} object.
* @throws org.codehaus.plexus.digest.DigesterException if any.
*/
public String calc()
throws DigesterException
{
return calc( this.md );
public String calc() throws DigesterException {
return calc(this.md);
}

/**
* <p>reset.</p>
*
* @throws org.codehaus.plexus.digest.DigesterException if any.
*/
public void reset()
throws DigesterException
{
public void reset() throws DigesterException {
md.reset();
}

/** {@inheritDoc} */
public void update( InputStream is )
throws DigesterException
{
update( is, md );
public void update(InputStream is) throws DigesterException {
update(is, md);
}

/**
Expand All @@ -97,9 +84,8 @@ public void update( InputStream is )
* @param md a {@link java.security.MessageDigest} object.
* @return a {@link java.lang.String} object.
*/
protected static String calc( MessageDigest md )
{
return Hex.encode( md.digest() );
protected static String calc(MessageDigest md) {
return Hex.encode(md.digest());
}

/**
Expand All @@ -109,22 +95,16 @@ protected static String calc( MessageDigest md )
* @param digest a {@link java.security.MessageDigest} object.
* @throws org.codehaus.plexus.digest.DigesterException if any.
*/
protected static void update( InputStream is, MessageDigest digest )
throws DigesterException
{
try
{
protected static void update(InputStream is, MessageDigest digest) throws DigesterException {
try {
byte[] buffer = new byte[BUFFER_SIZE];
int size = is.read( buffer, 0, BUFFER_SIZE );
while ( size >= 0 )
{
digest.update( buffer, 0, size );
size = is.read( buffer, 0, BUFFER_SIZE );
int size = is.read(buffer, 0, BUFFER_SIZE);
while (size >= 0) {
digest.update(buffer, 0, size);
size = is.read(buffer, 0, BUFFER_SIZE);
}
}
catch ( IOException e )
{
throw new DigesterException( "Unable to update " + digest.getAlgorithm() + " hash: " + e.getMessage(), e );
} catch (IOException e) {
throw new DigesterException("Unable to update " + digest.getAlgorithm() + " hash: " + e.getMessage(), e);
}
}
}
62 changes: 27 additions & 35 deletions src/main/java/org/codehaus/plexus/digest/ChecksumFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@
* limitations under the License.
*/

import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;

import javax.inject.Inject;
import javax.inject.Named;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import org.codehaus.plexus.util.FileUtils;
import org.codehaus.plexus.util.StringUtils;

/**
* ChecksumFile
*
* @author <a href="mailto:[email protected]">Joakim Erdfelt</a>
*/
@Named
public class ChecksumFile
{
public class ChecksumFile {
@Inject
@Named ( "sha256" )
@Named("sha256")
private Digester digestSha256;

@Inject
@Named ( "sha1" )
@Named("sha1")
private Digester digestSha1;

@Inject
@Named ( "md5" )
@Named("md5")
private Digester digestMd5;

/**
Expand All @@ -67,47 +67,40 @@ public class ChecksumFile
* @throws java.io.FileNotFoundException if the checksumFile itself or the file it refers to is not found.
* @throws java.io.IOException if the reading of the checksumFile or the file it refers to fails.
*/
public boolean isValidChecksum( File checksumFile ) throws DigesterException, FileNotFoundException, IOException
{
if ( !checksumFile.exists() )
{
throw new FileNotFoundException( "Unable to find checksum file " + checksumFile.getAbsolutePath() );
public boolean isValidChecksum(File checksumFile) throws DigesterException, FileNotFoundException, IOException {
if (!checksumFile.exists()) {
throw new FileNotFoundException("Unable to find checksum file " + checksumFile.getAbsolutePath());
}

if ( !checksumFile.isFile() )
{
throw new IOException( "Unable to load checksum from non-file " + checksumFile.getAbsolutePath() );
if (!checksumFile.isFile()) {
throw new IOException("Unable to load checksum from non-file " + checksumFile.getAbsolutePath());
}

String path = checksumFile.getAbsolutePath();
Digester digester = findDigesterByFileSuffix(path);

File referenceFile = new File( path.substring( 0, path.length() - digester.getFilenameExtension().length() ) );
File referenceFile = new File(path.substring(
0, path.length() - digester.getFilenameExtension().length()));

String rawChecksum = FileUtils.fileRead( checksumFile, "UTF-8" );
String expectedChecksum = DigestUtils.cleanChecksum( rawChecksum, digester, referenceFile.getName() );
String rawChecksum = FileUtils.fileRead(checksumFile, "UTF-8");
String expectedChecksum = DigestUtils.cleanChecksum(rawChecksum, digester, referenceFile.getName());

String actualChecksum = digester.calc( referenceFile );
String actualChecksum = digester.calc(referenceFile);

return StringUtils.equalsIgnoreCase( expectedChecksum, actualChecksum );
return StringUtils.equalsIgnoreCase(expectedChecksum, actualChecksum);
}

private Digester findDigesterByFileSuffix(String path) throws DigesterException {
if ( path.endsWith( digestMd5.getFilenameExtension() ) )
{
if (path.endsWith(digestMd5.getFilenameExtension())) {
return digestMd5;
}
else if ( path.endsWith( digestSha1.getFilenameExtension() ) )
{
} else if (path.endsWith(digestSha1.getFilenameExtension())) {
return digestSha1;
}
else if ( path.endsWith( digestSha256.getFilenameExtension() ) )
{
} else if (path.endsWith(digestSha256.getFilenameExtension())) {
return digestSha256;
}
// TODO: Add more digester implementations here.

throw new DigesterException( "Unable to determine digester type from filename " + path );
throw new DigesterException("Unable to determine digester type from filename " + path);
}

/**
Expand All @@ -119,11 +112,10 @@ else if ( path.endsWith( digestSha256.getFilenameExtension() ) )
* @throws org.codehaus.plexus.digest.DigesterException if there was a problem calculating the checksum of the referenceFile.
* @throws java.io.IOException if there was a problem either reading the referenceFile, or writing the checksum file.
*/
public File createChecksum( File referenceFile, Digester digester ) throws DigesterException, IOException
{
File checksumFile = new File( referenceFile.getAbsolutePath() + digester.getFilenameExtension() );
String checksum = digester.calc( referenceFile );
FileUtils.fileWrite( checksumFile.getAbsolutePath(), "UTF-8", checksum + " " + referenceFile.getName() );
public File createChecksum(File referenceFile, Digester digester) throws DigesterException, IOException {
File checksumFile = new File(referenceFile.getAbsolutePath() + digester.getFilenameExtension());
String checksum = digester.calc(referenceFile);
FileUtils.fileWrite(checksumFile.getAbsolutePath(), "UTF-8", checksum + " " + referenceFile.getName());
return checksumFile;
}
}
Loading

0 comments on commit 2f49c4f

Please sign in to comment.