1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // Multi-Phasic Applications: SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc
.squirreljme
.plugin
.general
;
12 import java
.io
.IOException
;
13 import java
.io
.InputStream
;
14 import java
.security
.MessageDigest
;
15 import java
.security
.NoSuchAlgorithmException
;
18 * Represents the JavaDoc file.
22 public final class JavaDocFile
23 implements Comparable
<JavaDocFile
>
25 /** The name of the file. */
26 public final String name
;
28 /** The data supplier. */
29 public final JavaDocFileSupplier supplier
;
32 private volatile byte[] _checksum
;
35 * Initializes the file reference.
37 * @param __name The name of the file.
38 * @param __supplier The supplier for the file data.
39 * @throws NullPointerException On null arguments.
42 public JavaDocFile(String __name
, JavaDocFileSupplier __supplier
)
43 throws NullPointerException
45 if (__name
== null || __supplier
== null)
46 throw new NullPointerException("NARG");
49 this.supplier
= __supplier
;
53 * Returns the checksum of the file.
55 * @return The file checksum.
58 public byte[] checkSum()
60 // Has this already been calculated?
61 byte[] checksum
= this._checksum
;
63 return checksum
.clone();
66 try (InputStream in
= this.supplier
.open())
68 MessageDigest digest
= MessageDigest
.getInstance("sha-1");
70 // Calculate checksum with bytes
71 byte[] buf
= new byte[16384];
74 int rc
= in
.read(buf
);
79 digest
.update(buf
, 0, rc
);
82 checksum
= digest
.digest();
83 this._checksum
= checksum
;
84 return checksum
.clone();
86 catch (IOException
|NoSuchAlgorithmException e
)
88 throw new RuntimeException(e
);
97 public int compareTo(JavaDocFile __b
)
99 return this.name
.compareTo(__b
.name
);
107 public String
toString()