Skip to content

Commit 8d9ed21

Browse files
committed
CoderFactory does not declare generic Exception to be thrown and throws IllegalArgumentException for unknown encoding schema
1 parent bb47393 commit 8d9ed21

File tree

2 files changed

+28
-38
lines changed

2 files changed

+28
-38
lines changed
Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
/*
22
Copyright 2006-2011 Abdulla Abdurakhmanov ([email protected])
3-
Original sources are available at www.latestbit.com
4-
3+
54
Licensed under the Apache License, Version 2.0 (the "License");
65
you may not use this file except in compliance with the License.
76
You may obtain a copy of the License at
@@ -19,10 +18,9 @@ limitations under the License.
1918

2019
namespace org.bn
2120
{
22-
2321
public class CoderFactory
2422
{
25-
private static CoderFactory instance = new CoderFactory();
23+
private static readonly CoderFactory instance = new CoderFactory();
2624

2725
public static CoderFactory getInstance() {
2826
return instance;
@@ -33,65 +31,64 @@ public IEncoder newEncoder() {
3331
}
3432

3533
public IEncoder newEncoder(String encodingSchema) {
36-
if(encodingSchema.Equals("BER",StringComparison.CurrentCultureIgnoreCase)) {
34+
if (encodingSchema.Equals("BER",StringComparison.CurrentCultureIgnoreCase))
35+
{
3736
return new org.bn.coders.ber.BEREncoder();
3837
}
39-
else
40-
if (encodingSchema.Equals("PER", StringComparison.CurrentCultureIgnoreCase) ||
38+
else if (encodingSchema.Equals("PER", StringComparison.CurrentCultureIgnoreCase) ||
4139
encodingSchema.Equals("PER/Aligned", StringComparison.CurrentCultureIgnoreCase) ||
4240
encodingSchema.Equals("PER/A", StringComparison.CurrentCultureIgnoreCase))
4341
{
4442
return new org.bn.coders.per.PERAlignedEncoder();
4543
}
46-
else
47-
if (encodingSchema.Equals("PER/Unaligned", StringComparison.CurrentCultureIgnoreCase)||
44+
else if (encodingSchema.Equals("PER/Unaligned", StringComparison.CurrentCultureIgnoreCase)||
4845
encodingSchema.Equals("PER/U", StringComparison.CurrentCultureIgnoreCase))
4946
{
5047
return new org.bn.coders.per.PERUnalignedEncoder();
5148
}
52-
else
53-
if (encodingSchema.Equals("DER", StringComparison.CurrentCultureIgnoreCase))
49+
else if (encodingSchema.Equals("DER", StringComparison.CurrentCultureIgnoreCase))
5450
{
5551
return new org.bn.coders.der.DEREncoder();
5652
}
5753
else
58-
return null;
54+
{
55+
throw new ArgumentException("Unknown encoding schema '"+encodingSchema+"'", "encodingSchema");
56+
}
5957
}
6058

6159
public IDecoder newDecoder() {
6260
return newDecoder("BER");
6361
}
6462

6563
public IDecoder newDecoder(String encodingSchema) {
66-
if(encodingSchema.Equals("BER", StringComparison.CurrentCultureIgnoreCase)) {
64+
if (encodingSchema.Equals("BER", StringComparison.CurrentCultureIgnoreCase))
65+
{
6766
return new org.bn.coders.ber.BERDecoder();
6867
}
69-
else
70-
if (encodingSchema.Equals("PER", StringComparison.CurrentCultureIgnoreCase) ||
68+
else if (encodingSchema.Equals("PER", StringComparison.CurrentCultureIgnoreCase) ||
7169
encodingSchema.Equals("PER/Aligned", StringComparison.CurrentCultureIgnoreCase)||
7270
encodingSchema.Equals("PER/A", StringComparison.CurrentCultureIgnoreCase))
7371
{
7472
return new org.bn.coders.per.PERAlignedDecoder();
7573
}
76-
else
77-
if (encodingSchema.Equals("PER/Unaligned", StringComparison.CurrentCultureIgnoreCase)||
74+
else if (encodingSchema.Equals("PER/Unaligned", StringComparison.CurrentCultureIgnoreCase)||
7875
encodingSchema.Equals("PER/U", StringComparison.CurrentCultureIgnoreCase))
7976
{
8077
return new org.bn.coders.per.PERUnalignedDecoder();
8178
}
82-
else
83-
if (encodingSchema.Equals("DER", StringComparison.CurrentCultureIgnoreCase))
79+
else if (encodingSchema.Equals("DER", StringComparison.CurrentCultureIgnoreCase))
8480
{
8581
return new org.bn.coders.der.DERDecoder();
8682
}
8783
else
88-
return null;
84+
{
85+
throw new ArgumentException("Unknown encoding schema '" + encodingSchema + "'", "encodingSchema");
86+
}
8987
}
9088

9189
public IASN1PreparedElementData newPreparedElementData(Type typeInfo)
9290
{
9391
return new ASN1PreparedElementData(typeInfo);
9492
}
95-
9693
}
9794
}

JavaLibrary/src/main/java/org/bn/CoderFactory.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/*
22
Copyright 2006-2011 Abdulla Abdurakhmanov ([email protected])
3-
Original sources are available at www.latestbit.com
43
54
Licensed under the Apache License, Version 2.0 (the "License");
65
you may not use this file except in compliance with the License.
@@ -91,7 +90,7 @@
9190
* }
9291
* @endcode
9392
*
94-
* <i> The encoding/decoding procedure in your prorram: </i>
93+
* <i> The encoding/decoding procedure in your program: </i>
9594
*
9695
* @code
9796
*
@@ -132,10 +131,8 @@ public static CoderFactory getInstance() {
132131

133132
/**
134133
* Create new default encoder (The BER encoding is default)
135-
*
136-
* @return Encoder
137134
*/
138-
public <T> IEncoder<T> newEncoder() throws Exception {
135+
public <T> IEncoder<T> newEncoder() {
139136
return newEncoder("BER");
140137
}
141138

@@ -145,9 +142,9 @@ public <T> IEncoder<T> newEncoder() throws Exception {
145142
*
146143
* @param encodingSchema ASN.1 encoding specification
147144
* @return Encoder for specified specification
148-
* @throws Exception
145+
* @throws IllegalArgumentException if the encodingSchema is not recognized
149146
*/
150-
public <T> IEncoder<T> newEncoder(String encodingSchema) throws Exception {
147+
public <T> IEncoder<T> newEncoder(String encodingSchema) {
151148
if (encodingSchema.equalsIgnoreCase("BER")) {
152149
return new BEREncoder<T>();
153150
} else if (encodingSchema.equalsIgnoreCase("PER") || encodingSchema.equalsIgnoreCase("PER/Aligned") || encodingSchema.equalsIgnoreCase("PER/A")) {
@@ -157,29 +154,26 @@ public <T> IEncoder<T> newEncoder(String encodingSchema) throws Exception {
157154
} else if (encodingSchema.equalsIgnoreCase("DER")) {
158155
return new DEREncoder<T>();
159156
} else {
160-
return null;
157+
throw new IllegalArgumentException("Unknown encoding schema '"+encodingSchema+"'");
161158
}
162159
}
163160

164161
/**
165162
* Create new default decoder (The BER decoding is default)
166-
*
167-
* @return
168-
* @throws Exception
169163
*/
170-
public IDecoder newDecoder() throws Exception {
164+
public IDecoder newDecoder() {
171165
return newDecoder("BER");
172166
}
173167

174168
/**
175169
* Create new decoder for specified schema (BER, PER, PER/Aligned,
176170
* PER/Unaligned, ...)
177171
*
178-
* @param encodingSchema
172+
* @param encodingSchema ASN.1 encoding specification
179173
* @return Decoder for specified specification
180-
* @throws Exception
174+
* @throws IllegalArgumentException if the encodingSchema is not recognized
181175
*/
182-
public IDecoder newDecoder(String encodingSchema) throws Exception {
176+
public IDecoder newDecoder(String encodingSchema) {
183177
if (encodingSchema.equalsIgnoreCase("BER")) {
184178
return new BERDecoder();
185179
} else if (encodingSchema.equalsIgnoreCase("PER") || encodingSchema.equalsIgnoreCase("PER/Aligned") || encodingSchema.equalsIgnoreCase("PER/A")) {
@@ -189,12 +183,11 @@ public IDecoder newDecoder(String encodingSchema) throws Exception {
189183
} else if (encodingSchema.equalsIgnoreCase("DER")) {
190184
return new DERDecoder();
191185
} else {
192-
return null;
186+
throw new IllegalArgumentException("Unknown encoding schema '"+encodingSchema+"'");
193187
}
194188
}
195189

196190
public IASN1PreparedElementData newPreparedElementData(Class<?> typeInfo) {
197191
return new ASN1PreparedElementData(typeInfo);
198192
}
199-
200193
}

0 commit comments

Comments
 (0)