KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > orbutil > HexOutputStream


1 /*
2  * @(#)HexOutputStream.java 1.31 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.orbutil;
9
10 import java.io.StringWriter JavaDoc;
11 import java.io.OutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13
14 /**
15  * Writes each input byte as a 2 byte hexidecimal output pair making it
16  * possible to turn arbitrary binary data into an ASCII format.
17  * The high 4 bits of the byte is translated into the first byte.
18  *
19  * @author Jeff Nisewanger
20  */

21 public class HexOutputStream extends OutputStream JavaDoc
22 {
23     static private final char hex[] = {
24         '0', '1', '2', '3', '4', '5', '6', '7',
25         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
26     };
27
28     private StringWriter JavaDoc writer;
29
30     /**
31      * Creates a new HexOutputStream.
32      * @param w The underlying StringWriter.
33      */

34     public
35     HexOutputStream(StringWriter JavaDoc w) {
36         writer = w;
37     }
38
39
40     /**
41      * Writes a byte. Will block until the byte is actually
42      * written.
43      * param b The byte to write out.
44      * @exception java.io.IOException I/O error occurred.
45      */

46     public synchronized void write(int b) throws IOException JavaDoc {
47     writer.write(hex[((b >> 4) & 0xF)]);
48     writer.write(hex[((b >> 0) & 0xF)]);
49     }
50
51     public synchronized void write(byte[] b) throws IOException JavaDoc {
52     write(b, 0, b.length);
53     }
54
55     public synchronized void write(byte[] b, int off, int len)
56     throws IOException JavaDoc
57     {
58     for(int i=0; i < len; i++) {
59         write(b[off + i]);
60     }
61     }
62 }
63
64
Popular Tags