HexOutputStream.java revision 608:7e06bf1dcb09
1/* 2 * Copyright (c) 1996, 2002, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26package com.sun.corba.se.impl.orbutil; 27 28import java.io.StringWriter; 29import java.io.OutputStream; 30import java.io.IOException; 31 32/** 33 * Writes each input byte as a 2 byte hexidecimal output pair making it 34 * possible to turn arbitrary binary data into an ASCII format. 35 * The high 4 bits of the byte is translated into the first byte. 36 * 37 * @author Jeff Nisewanger 38 */ 39public class HexOutputStream extends OutputStream 40{ 41 static private final char hex[] = { 42 '0', '1', '2', '3', '4', '5', '6', '7', 43 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' 44 }; 45 46 private StringWriter writer; 47 48 /** 49 * Creates a new HexOutputStream. 50 * @param w The underlying StringWriter. 51 */ 52 public 53 HexOutputStream(StringWriter w) { 54 writer = w; 55 } 56 57 58 /** 59 * Writes a byte. Will block until the byte is actually 60 * written. 61 * param b The byte to write out. 62 * @exception java.io.IOException I/O error occurred. 63 */ 64 public synchronized void write(int b) throws IOException { 65 writer.write(hex[((b >> 4) & 0xF)]); 66 writer.write(hex[((b >> 0) & 0xF)]); 67 } 68 69 public synchronized void write(byte[] b) throws IOException { 70 write(b, 0, b.length); 71 } 72 73 public synchronized void write(byte[] b, int off, int len) 74 throws IOException 75 { 76 for(int i=0; i < len; i++) { 77 write(b[off + i]); 78 } 79 } 80} 81