Java中的提供者getService()和getServices()方法

getService(字符串类型、字符串算法)

这个 getService() 方法 JAVA安全供应商 类用于获取描述此提供程序对此算法或别名的指定类型的实现的服务。

null

如果不存在这样的实现,该方法将返回null。如果有两个匹配的服务,一个使用putService()添加到此提供程序,另一个通过put()添加,则返回通过putService()添加的服务。

语法:

public Provider.Service getService(String type, String algorithm)

参数: 此方法将以下参数作为参数。

  • 类型 –请求的服务类型。
  • 算法 –请求的服务的不区分大小写的算法名称(或备用别名)。

返回值: 此方法返回 服务 描述此提供商的匹配服务或 无效的 如果不存在这样的服务。

例外情况: 这个方法抛出 空指针异常 如果类型或算法为空。

下面是一些例子来说明 getService() 方法:

例1:

// Java program to demonstrate
// getService() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// creating the object of  SecureRandom
Signature sr = Signature.getInstance( "SHA1withDSA" , "SUN" );
// getting the Provider of the SecureRandom sr
// by using method getProvider()
Provider provider = sr.getProvider();
// getting the service of the provider using getServices() method
Provider.Service service1 = provider
.getService( "Signature" , sr.getAlgorithm());
// printing the service
System.out.println( "Provider service : " + service1);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
}
}


输出:

Provider service : SUN: Signature.SHA1withDSA -> sun.security.provider.DSA$SHA1withDSA
  aliases: [DSA, DSS, SHA/DSA, SHA-1/DSA, SHA1/DSA, SHAwithDSA, DSAWithSHA1, OID.1.2.840.10040.4.3, 1.2.840.10040.4.3, 1.3.14.3.2.13, 1.3.14.3.2.27]
  attributes: {ImplementedIn=Software, KeySize=1024, SupportedKeyClasses=java.security.interfaces.DSAPublicKey|java.security.interfaces.DSAPrivateKey}

例2: 显示getService()方法引发的NullPointerException。

// Java program to demonstrate
// getService() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
try {
// creating the object of  SecureRandom
Signature sr = Signature.getInstance( "SHA1withDSA" , "SUN" );
// getting the Provider of the SecureRandom sr
// by using method getProvider()
Provider provider = sr.getProvider();
// getting the service of the provider using getServices() method
Provider.Service service1 = provider
.getService( null , sr.getAlgorithm());
// printing the service
System.out.println( "Provider service : " + service1);
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println( "Exception thrown : " + e);
}
}
}


输出:

Exception thrown : java.lang.NullPointerException

getServices()

这个 getServices() 方法 JAVA安全供应商 类用于获取此提供程序支持的所有服务的不可修改集。

语法:

public Set<Provider.Service> getServices()

返回值: 此方法返回一个 不可修改集 此提供商支持的所有服务的名称。

下面是说明getServices()方法的示例:

项目1:

// Java program to demonstrate
// getService() method
import java.security.*;
import java.util.*;
public class GFG1 {
public static void main(String[] argv) throws Exception
{
// Declaring int variable
int i = 5 ;
try {
// creating the object of  SecureRandom
Signature sr = Signature.getInstance( "SHA1withDSA" , "SUN" );
// getting the Provider of the SecureRandom sr
// by using method getProvider()
Provider provider = sr.getProvider();
// Declaring the variable of set<Map> type
Set<Provider.Service> servicelist;
// getting the service of the provider using getServices() method
servicelist = provider.getServices();
// Creating the object of iterator to iterate set
Iterator<Provider.Service> iter = servicelist.iterator();
// printing the set elements
System.out.println( "Provider servicelist : " );
while (i > 0 ) {
System.out.println( "Value is : " + iter.next());
i--;
}
}
catch (NoSuchAlgorithmException e) {
System.out.println( "Exception thrown : " + e);
}
catch (NullPointerException e) {
System.out.println( "Exception thrown : " + e);
}
}
}


输出:

Provider servicelist : 
 
Value is : SUN: SecureRandom.NativePRNG -> sun.security.provider.NativePRNG

Value is : SUN: SecureRandom.SHA1PRNG -> sun.security.provider.SecureRandom
  attributes: {ImplementedIn=Software}

Value is : SUN: SecureRandom.NativePRNGBlocking -> sun.security.provider.NativePRNG$Blocking

Value is : SUN: SecureRandom.NativePRNGNonBlocking -> sun.security.provider.NativePRNG$NonBlocking

Value is : SUN: Signature.SHA1withDSA -> sun.security.provider.DSA$SHA1withDSA
  aliases: [DSA, DSS, SHA/DSA, SHA-1/DSA, SHA1/DSA, SHAwithDSA, DSAWithSHA1, OID.1.2.840.10040.4.3, 1.2.840.10040.4.3, 1.3.14.3.2.13, 1.3.14.3.2.27]
  attributes: {ImplementedIn=Software, KeySize=1024, SupportedKeyClasses=java.security.interfaces.DSAPublicKey|java.security.interfaces.DSAPrivateKey}

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享