1 /******************************************************************************** 2 * $Id: HexEncodingOutputStream.java 395 2007-09-09 17:40:33Z pgmjsd $ 3 * $Author: pgmjsd $ 4 * $Date: 2007-09-09 13:40:33 -0400 (Sun, 09 Sep 2007) $ 5 * 6 * Copyright 2002-2003 YAJUL Developers, Joshua Davis, Kent Vogel. 7 * 8 * Permission is hereby granted, free of charge, to any person obtaining a copy 9 * of this software and associated documentation files (the "Software"), to deal 10 * in the Software without restriction, including without limitation the rights 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 * copies of the Software, and to permit persons to whom the Software is 13 * furnished to do so, subject to the following conditions: 14 * 15 * The above copyright notice and this permission notice shall be included in 16 * all copies or substantial portions of the Software. 17 * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 * THE SOFTWARE. 25 * 26 ******************************************************************************/ 27 package org.yajul.io; 28 29 import org.yajul.util.StringUtil; 30 import org.yajul.util.Bytes; 31 32 import java.io.FilterOutputStream; 33 import java.io.IOException; 34 import java.io.OutputStream; 35 36 /*** 37 * Encodes the input in hexadecimal form. 38 * User: josh 39 * Date: Jan 23, 2004 40 * Time: 9:20:13 AM 41 */ 42 public class HexEncodingOutputStream extends FilterOutputStream { 43 /*** 44 * Hex chars buffer. * 45 */ 46 private char[] hex; 47 /*** 48 * Hex byte buffer. * 49 */ 50 private byte[] hexBytes; 51 /*** 52 * The hexadecimal encoding character set. * 53 */ 54 private char[] hexEncodingChars; 55 56 public HexEncodingOutputStream(OutputStream out) { 57 super(out); 58 hexBytes = new byte[2]; 59 hex = new char[2]; 60 hexEncodingChars = Bytes.HEX_CHARS_LOWER; 61 } 62 63 public void write(int b) throws IOException { 64 writeHex(b); 65 } 66 67 protected void writeHex(int b) throws IOException { 68 StringUtil.hexChars(hexEncodingChars, b, hex); 69 hexBytes[0] = (byte) hex[0]; 70 hexBytes[1] = (byte) hex[1]; 71 out.write(hexBytes); 72 } 73 }