View Javadoc
1 2 package jsdsi.sexp; 3 4 import java.io.IOException; 5 import java.io.ObjectInputStream; 6 import java.io.ObjectOutputStream; 7 import java.io.OutputStream; 8 import java.io.Writer; 9 10 /*** 11 * A byte string with an optional display hint. 12 * 13 * @see Sexp 14 * 15 * @author Alexander Morcos, Sameer Ajmani 16 * @version $Revision: 1.1.6.1 $ $Date: 2005/11/08 03:12:52 $ 17 */ 18 public class SexpString extends Sexp { 19 private SimpleString display; 20 private SimpleString content; 21 22 // implements serializable 23 SexpString() { 24 } 25 26 // implements serializable 27 private void writeObject(ObjectOutputStream out) throws IOException { 28 writeCanonical(out); 29 } 30 31 // implements serializable 32 private void readObject(ObjectInputStream in) 33 throws IOException, ClassNotFoundException { 34 try { 35 SexpString s = (SexpString) (new SexpInputStream(in)).readSexp(); 36 this.display = s.display; 37 this.content = s.content; 38 } catch (SexpException e) { 39 throw (IOException) new IOException().initCause(e); 40 } catch (ClassCastException e) { 41 throw (IOException) new IOException().initCause(e); 42 } 43 } 44 45 /*** 46 * Creates a new <code>SexpString</code> with the given display-hint 47 * and content. 48 * 49 * @param d the display hint. 50 * @param c the content of the string. 51 */ 52 public SexpString(byte[] d, byte[] c) { 53 display = new SimpleString(d); 54 content = new SimpleString(c); 55 } 56 57 /*** 58 * Creates a new <code>SexpString</code> with no display-hint. 59 * 60 * @param c the content of the string. 61 */ 62 public SexpString(String c) { 63 display = null; 64 content = new SimpleString(c); 65 } 66 67 /*** 68 * Creates a new <code>SexpString</code> with the given display-hint 69 * and content. 70 * 71 * @param d the display hint. 72 * @param c the content of the string. 73 */ 74 public SexpString(String d, String c) { 75 display = new SimpleString(d); 76 content = new SimpleString(c); 77 } 78 79 /*** 80 * Creates a new <code>SexpString</code> with no display-hint. 81 * 82 * @param c the content of the string. 83 */ 84 public SexpString(byte[] c) { 85 display = null; 86 content = new SimpleString(c); 87 } 88 89 /*** 90 * Creates a new <code>SexpString</code> with the given display-hint 91 * and content. 92 * 93 * @param d the display hint 94 * @param c the content of the string. 95 */ 96 public SexpString(String d, byte[] c) { 97 display = new SimpleString(d); 98 content = new SimpleString(c); 99 } 100 101 /*** 102 * Returns the content of this string as a <code>String</code>. 103 * 104 * @return the content of this string as a <code>String</code>. 105 */ 106 public String toString() { 107 return content.toString(); 108 } 109 110 /*** 111 * Returns the content of this string as a byte array. 112 * 113 * @return the content of this string as a byte array. 114 */ 115 public byte[] toByteArray() { 116 return content.toByteArray(); 117 } 118 119 public void writeCanonical(OutputStream out) throws IOException { 120 if (display != null) { 121 out.write('['); 122 display.writeCanonical(out); 123 out.write(']'); 124 } 125 content.writeCanonical(out); 126 } 127 128 public void writeReadable(Writer out, int offset, int width, int last) 129 throws IOException { 130 if (display != null) { 131 out.write('['); 132 display.writeReadable(out, offset, width, last); 133 out.write(']'); 134 if (getReadableLen() > width - offset - last) { 135 out.write('\n'); 136 } 137 } 138 content.writeReadable(out, offset, width, last); 139 } 140 141 int getReadableLenImpl() { 142 if (display != null) { 143 return display.getReadableLen() + 2 + content.getReadableLen(); 144 } else { 145 return content.getReadableLen(); 146 } 147 } 148 }

This page was automatically generated by Maven