1. /*
  2. * @(#)HexOutputStream.java 1.29 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package com.sun.corba.se.internal.orbutil;
  8. import java.io.StringWriter;
  9. import java.io.OutputStream;
  10. import java.io.IOException;
  11. /**
  12. * Writes each input byte as a 2 byte hexidecimal output pair making it
  13. * possible to turn arbitrary binary data into an ASCII format.
  14. * The high 4 bits of the byte is translated into the first byte.
  15. *
  16. * @author Jeff Nisewanger
  17. */
  18. public class HexOutputStream extends OutputStream
  19. {
  20. static private final char hex[] = {
  21. '0', '1', '2', '3', '4', '5', '6', '7',
  22. '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
  23. };
  24. private StringWriter writer;
  25. /**
  26. * Creates a new HexOutputStream.
  27. * @param w The underlying StringWriter.
  28. */
  29. public
  30. HexOutputStream(StringWriter w) {
  31. writer = w;
  32. }
  33. /**
  34. * Writes a byte. Will block until the byte is actually
  35. * written.
  36. * param b The byte to write out.
  37. * @exception java.io.IOException I/O error occurred.
  38. */
  39. public synchronized void write(int b) throws IOException {
  40. writer.write(hex[((b >> 4) & 0xF)]);
  41. writer.write(hex[((b >> 0) & 0xF)]);
  42. }
  43. public synchronized void write(byte[] b) throws IOException {
  44. write(b, 0, b.length);
  45. }
  46. public synchronized void write(byte[] b, int off, int len)
  47. throws IOException
  48. {
  49. for(int i=0; i < len; i++) {
  50. write(b[off + i]);
  51. }
  52. }
  53. }