Package javax0.license3j.io
Class KeyPairReader
- java.lang.Object
-
- javax0.license3j.io.KeyPairReader
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public class KeyPairReader extends Object implements Closeable
Class to read a key from file. The class can be used in the application to load the public key, and it is also used in the repl application. Note that the applications that need to validate a license can only read the public key. The environment (client machine) where the license is checked should not have the private key available and hence it cannot be read.Create an instance of this class using one of the constructors specifying the source of the license key and then use one of the
read...()
methods to read the key into aLicenseKeyPair
object.Note: the class file is named
KeyPairReader
because it is the reading part of the classKeyPairWriter
. However, this class reads one key at a time and not a pair. The keys, when created are written in pairs to save them, but then only one is read when encoding or decoding. On the other hand the return value of the read methods areLicenseKeyPair
objects that are filled with either a public or private key and the other key in these objects arenull
.
-
-
Constructor Summary
Constructors Constructor Description KeyPairReader(File file)
Create a reader with thefile
as the source for the key.KeyPairReader(InputStream is)
Create a reader with the input streamis
as the source for the key.KeyPairReader(String fileName)
Create a reader with thefileName
as the source for the key.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
The class implements theCloseable
interface.LicenseKeyPair
readPrivate()
Read the private key from the source specified in the constructor in BINARY format.LicenseKeyPair
readPrivate(IOFormat format)
Read the private key from the source specified in the constructor in the specified format.LicenseKeyPair
readPublic()
Read the public key from the source specified in the constructor in BINARY format.LicenseKeyPair
readPublic(IOFormat format)
Read the public key from the source specified in the constructor in the specified format.
-
-
-
Constructor Detail
-
KeyPairReader
public KeyPairReader(InputStream is)
Create a reader with the input streamis
as the source for the key.- Parameters:
is
- where the key bytes or characters in case of base64 encoding are coming from.
-
KeyPairReader
public KeyPairReader(File file) throws FileNotFoundException
Create a reader with thefile
as the source for the key.- Parameters:
file
- where the key bytes or characters in case of base64 encoding are coming from.- Throws:
FileNotFoundException
- when the file cannot be found
-
KeyPairReader
public KeyPairReader(String fileName) throws FileNotFoundException
Create a reader with thefileName
as the source for the key.- Parameters:
fileName
- where the key bytes or characters in case of base64 encoding are coming from.- Throws:
FileNotFoundException
- when the file cannot be found
-
-
Method Detail
-
readPublic
public LicenseKeyPair readPublic() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException
Read the public key from the source specified in the constructor in BINARY format.- Returns:
- the license key pair that contains only the public key.
- Throws:
IOException
- when the source cannot be read.InvalidKeySpecException
- if the format of the key in the source is invalid.NoSuchAlgorithmException
- if the format of the key in the source is invalid.
-
readPublic
public LicenseKeyPair readPublic(IOFormat format) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException
Read the public key from the source specified in the constructor in the specified format.- Parameters:
format
- can be either BINARY or BASE64.- Returns:
- the license key pair that contains only the public key.
- Throws:
IOException
- when the source cannot be read.InvalidKeySpecException
- if the format of the key in the source is invalid.NoSuchAlgorithmException
- if the format of the key in the source is invalid.
-
readPrivate
public LicenseKeyPair readPrivate() throws IOException, InvalidKeySpecException, NoSuchAlgorithmException
Read the private key from the source specified in the constructor in BINARY format.- Returns:
- the license key pair that contains only the private key.
- Throws:
IOException
- when the source cannot be read.InvalidKeySpecException
- if the format of the key in the source is invalid.NoSuchAlgorithmException
- if the format of the key in the source is invalid.
-
readPrivate
public LicenseKeyPair readPrivate(IOFormat format) throws IOException, InvalidKeySpecException, NoSuchAlgorithmException
Read the private key from the source specified in the constructor in the specified format.- Parameters:
format
- can be either BINARY or BASE64.- Returns:
- the license key pair that contains only the private key.
- Throws:
IOException
- when the source cannot be read.InvalidKeySpecException
- if the format of the key in the source is invalid.NoSuchAlgorithmException
- if the format of the key in the source is invalid.
-
close
public void close() throws IOException
The class implements theCloseable
interface. This method closes the license source stream.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Throws:
IOException
- when the stream cannot be closed.
-
-