diff options
| author | Casey Marshall <csm@gnu.org> | 2004-08-30 13:06:48 +0000 |
|---|---|---|
| committer | Andreas Tobler <andreast@gcc.gnu.org> | 2004-08-30 15:06:48 +0200 |
| commit | bd8a2e96afaf00312fb3b0c1879e440043cf0e46 (patch) | |
| tree | 4fc5d308b76cb8c412058d6e39d3b66cacf8ac81 /libjava/gnu/java/security/der/DERWriter.java | |
| parent | 39880d37d6a779821cf0d3b7e5cc7b13e4982f4c (diff) | |
| download | gcc-bd8a2e96afaf00312fb3b0c1879e440043cf0e46.zip gcc-bd8a2e96afaf00312fb3b0c1879e440043cf0e46.tar.gz gcc-bd8a2e96afaf00312fb3b0c1879e440043cf0e46.tar.bz2 | |
Author e-mail updated for all files.
2004-08-30 Casey Marshall <csm@gnu.org>
Author e-mail updated for all files.
* gnu/java/security/OID.java (equals): Test if the aurgment is an
instance of OID.
(compareTo): Use `equals'.
* gnu/java/security/der/BitString.java (equals): Test if the
argument is an instance of BitString.
* gnu/java/security/der/DERReader.java: Removed NIO imports. Made
class final. Made fields private.
(<init>): New constructor.
(skip): New method.
(makeString): Made static; don't use NIO.
(fromIso88591, fromUtf16Be, fromUtf8): New methods.
* gnu/java/security/der/DERWriter.java: Fixed imports.
(writeString): Don't use NIO.
(toIso88591, toUtf16Be, toUtf8): New methods.
* gnu/java/security/der/DERValue.java: Formatting changes only.
* gnu/java/security/der/DER.java: Likewise.
From-SVN: r86765
Diffstat (limited to 'libjava/gnu/java/security/der/DERWriter.java')
| -rw-r--r-- | libjava/gnu/java/security/der/DERWriter.java | 77 |
1 files changed, 54 insertions, 23 deletions
diff --git a/libjava/gnu/java/security/der/DERWriter.java b/libjava/gnu/java/security/der/DERWriter.java index 4e679ec..8bf80b5 100644 --- a/libjava/gnu/java/security/der/DERWriter.java +++ b/libjava/gnu/java/security/der/DERWriter.java @@ -7,7 +7,7 @@ GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. - + GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU @@ -43,12 +43,12 @@ import gnu.java.security.OID; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; + import java.math.BigInteger; -import java.nio.ByteBuffer; -import java.nio.CharBuffer; -import java.nio.charset.Charset; -import java.nio.charset.CharsetEncoder; + import java.text.SimpleDateFormat; + +import java.util.BitSet; import java.util.Date; import java.util.Iterator; import java.util.List; @@ -66,7 +66,7 @@ import java.util.TimeZone; * <p>This class only defines static methods; there are no instance * variables needed. * - * @author Casey Marshall (rsdio@metastatic.org) + * @author Casey Marshall (csm@gnu.org) */ public class DERWriter implements DER { @@ -82,7 +82,7 @@ public class DERWriter implements DER // Class methods. // ------------------------------------------------------------------------ - public static int write(OutputStream out, DERValue object) + public static int write(OutputStream out, DERValue object) throws IOException { out.write(object.getExternalTag()); @@ -226,7 +226,6 @@ public class DERWriter implements DER private static int writeString(OutputStream out, int tag, String str) throws IOException { - Charset charset = null; byte[] b = null; switch (tag & 0x1F) { @@ -238,33 +237,65 @@ public class DERWriter implements DER case GRAPHIC_STRING: case ISO646_STRING: case GENERAL_STRING: - charset = Charset.forName("ISO-8859-1"); + b = toIso88591(str); break; + case UNIVERSAL_STRING: case BMP_STRING: - charset = Charset.forName("UTF-16BE"); + b = toUtf16Be(str); break; + case UTF8_STRING: default: - charset = Charset.forName("UTF-8"); + b = toUtf8(str); break; } - if (charset == null) - throw new DEREncodingException("no charset"); - CharsetEncoder encoder = charset.newEncoder(); - ByteBuffer result = encoder.encode(CharBuffer.wrap(str)); - if (result.hasArray()) + writeLength(out, b.length); + out.write(b); + return b.length; + } + + private static byte[] toIso88591(String string) + { + byte[] result = new byte[string.length()]; + for (int i = 0; i < string.length(); i++) + result[i] = (byte) string.charAt(i); + return result; + } + + private static byte[] toUtf16Be(String string) + { + byte[] result = new byte[string.length() * 2]; + for (int i = 0; i < string.length(); i++) { - b = result.array(); + result[i*2 ] = (byte) ((string.charAt(i) >>> 8) & 0xFF); + result[i*2+1] = (byte) (string.charAt(i) & 0xFF); } - else + return result; + } + + private static byte[] toUtf8(String string) + { + ByteArrayOutputStream buf = + new ByteArrayOutputStream((int)(string.length() * 1.5)); + for (int i = 0; i < string.length(); i++) { - b = new byte[result.remaining()]; - result.get(b); + char c = string.charAt(i); + if (c < 0x0080) + buf.write(c & 0xFF); + else if (c < 0x0800) + { + buf.write(0xC0 | ((c >>> 6) & 0x3F)); + buf.write(0x80 | (c & 0x3F)); + } + else + { + buf.write(0xE0 | ((c >>> 12) & 0x0F)); + buf.write(0x80 | ((c >>> 6) & 0x3F)); + buf.write(0x80 | (c & 0x3F)); + } } - writeLength(out, b.length); - out.write(b); - return b.length; + return buf.toByteArray(); } private static int writeDate(OutputStream out, int tag, Date date) |
