001package com.randomnoun.common.io; 002 003import java.io.IOException; 004import java.io.Serializable; 005import java.io.Writer; 006 007 008/** A Writer that has the same append() methods as StringBuilder. 009 * 010 * <p>This class exists mainly so that I can cut and paste the existing StringBuilder code, but use 011 * a Writer backing implementation instead. 012 * 013 * <p>Note that these methods may now throw IOExceptions. 014 */ 015public class AppendWriter extends Writer implements Serializable { 016 017 private static final long serialVersionUID = -146927496096066153L; 018 private final Writer w; 019 020 /** 021 * Constructs a new {@link StringBuilder} instance with default capacity. 022 */ 023 public AppendWriter(Writer w) { 024 this.w = w; 025 } 026 027 // these methods extend the Writer abstract class 028 029 /** 030 * Appends a single character to this Writer. 031 * 032 * @param value The character to append 033 * @return This writer instance 034 * @throws IOException 035 */ 036 @Override 037 public Writer append(final char value) throws IOException { 038 w.write((int) value); 039 return this; 040 } 041 042 /** 043 * Appends a character sequence to this Writer. 044 * 045 * @param value The character to append 046 * @return This writer instance 047 */ 048 @Override 049 public Writer append(final CharSequence value) throws IOException { 050 if (value == null) { 051 w.write("null"); 052 } else { 053 w.write(value.toString()); 054 } 055 return this; 056 } 057 058 059 @Override 060 public void close() throws IOException { 061 w.close(); 062 } 063 064 @Override 065 public void flush() throws IOException { 066 w.flush(); 067 } 068 069 @Override 070 public void write(final String value) throws IOException { 071 w.write(value); 072 } 073 074 @Override 075 public void write(final char[] value, final int offset, final int length) throws IOException { 076 w.write(value, offset, length); 077 } 078 079 // these methods are in StringBuilder 080 081 public AppendWriter append(Object obj) throws IOException { 082 return append(String.valueOf(obj)); 083 } 084 085 public AppendWriter append(String str) throws IOException { 086 w.write(str == null ? "null" : str); 087 return this; 088 } 089 090 public AppendWriter append(StringBuffer sb) throws IOException { 091 w.write(sb.toString()); 092 return this; 093 } 094 095 public AppendWriter append(char[] str) throws IOException { 096 w.write(str); 097 return this; 098 } 099 100 public AppendWriter append(char[] str, int offset, int len) throws IOException { 101 w.write(str, offset, len); 102 return this; 103 } 104 105 public AppendWriter append(boolean b) throws IOException { 106 w.write(b ? "true" : "false"); 107 return this; 108 } 109 110 public AppendWriter append(int i) throws IOException { 111 w.write(String.valueOf(i)); 112 return this; 113 } 114 115 public AppendWriter append(long lng) throws IOException { 116 w.write(String.valueOf(lng)); 117 return this; 118 } 119 120 public AppendWriter append(float f) throws IOException { 121 w.write(String.valueOf(f)); 122 return this; 123 } 124 125 public AppendWriter append(double d) throws IOException { 126 w.write(String.valueOf(d)); 127 return this; 128 } 129 130}