Writes {@code unit} to the correct file.

<p>This is complicated by an EJC bug that returns the wrong object from
{@link Writer#append(CharSequence)}.
171742
public static void writeCompilationUnit(
      Filer filer,
      SourceBuilder unit,
      Element originatingElement) throws IOException {
    String typename = unit.typename().toString();
    String finalSource = unit.toString();
    try (Writer writer = filer.createSourceFile(typename, originatingElement).openWriter()) {
      writer.append(finalSource);
    }
  }
703449
@Override
    public void write(char[] cbuf, int off, int len) throws IOException {
        encodedAppender.append(encoder, null, cbuf, off, len);
    }
129874
public void write(final int c) throws IOException {
        if (c >= 0XDC00 && c <= 0XDFFF) {
            // Low surrogate
            this.surrogatePair.append((char) c);

            if (this.encoder.canEncode(this.surrogatePair)) {
                this.writer.write(this.surrogatePair.toString());
            } else {
                this.writer.write("&#x");
                this.writer.write(Integer.toHexString(0X10000 + ((this.surrogatePair.charAt(0) & 0X3FF) << 10) + (c & 0X3FF)));
                this.writer.write(';');
            }

            this.haveHighSurrogate = false;
            this.surrogatePair.setLength(0);
        } else {
            if (this.haveHighSurrogate) {
                this.haveHighSurrogate = false;
                this.surrogatePair.setLength(0);
                throw new IOException("High Surrogate not followed by Low Surrogate");
            }

            if (c >= 0XD800 && c <= 0XDBFF) {
                // High surrogate
                this.surrogatePair.append((char) c);
                this.haveHighSurrogate = true;

            } else if (!this.encoder.canEncode((char) c)) {
                this.writer.write("&#x");
                this.writer.write(Integer.toHexString(c));
                this.writer.write(';');
            } else if (c == '\'' && this.writingAttribute && !useDoubleQuotes) {
                this.writer.write("&apos;");
            } else if (c == '"' && this.writingAttribute && useDoubleQuotes) {
                this.writer.write("&quot;");
            } else if (c == '\n' && this.writingAttribute) {
                this.writer.write("&#10;");
            } else if (c == '\r' && this.writingAttribute) {
                this.writer.write("&#13;");
            } else if (c == '\t' && this.writingAttribute) {
                this.writer.write("&#09;");
            } else {
                this.writer.write(c);
            }
        }
    }
85292
static void appendBytes(String content,
                          Mode mode,
                          BitArray bits,
                          String encoding) throws WriterException {
    switch (mode) {
      case NUMERIC:
        appendNumericBytes(content, bits);
        break;
      case ALPHANUMERIC:
        appendAlphanumericBytes(content, bits);
        break;
      case BYTE:
        append8BitBytes(content, bits, encoding);
        break;
      case KANJI:
        appendKanjiBytes(content, bits);
        break;
      default:
        throw new WriterException("Invalid mode: " + mode);
    }
  }
90819
private void write(int c) throws IOException {
		if (!firstCharacterWritten) {
			if (c != '\n') {
				out.write(c);
				firstCharacterWritten = true;
			}
		} else {
			out.write(c);
		}
	}
155479
public static void write(File file, String text, boolean append) throws IOException {
        FileWriter writer = new FileWriter(file, append);
        try {
            writer.write(text);
        } finally {
            writer.close();
        }
    }
1370574
public void write(String s, int off, int len) throws IOException {
	    if (writer != null) {
	        writer.write(s, off, len);
	    } else {
	        ensureOpen();
	        //PK33136
	        strBuffer.append(s.substring(off, off+len));
	        nextChar += len;
	        //PK33136
	    }
    }
359907
@Override
	public void write(int b) throws IOException {
		if (this.position >= this.buffer.length) {
			resize(1);
		}
		this.buffer[this.position++] = (byte) (b & 0xff);
	}
76040
@Override
    protected void appendCharSequence(EncodingState encodingState, CharSequence csq, int start, int end) throws IOException {
        CharSequences.writeCharSequence(target, csq, start, end);
    }
1370902
@Override public void write(char cbuf[], int off, int len)
      throws IOException {
    checkNotClosed();
    // It turns out that creating a new String is usually as fast, or faster
    // than wrapping cbuf in a light-weight CharSequence.
    target.append(new String(cbuf, off, len));
  }
1355077
public void write(ByteAppendable os, char ch)
    throws IOException
  {
    os.write(ch >> 8);
    os.write(ch);
  }
245285
@Override
    public StringWriter append(CharSequence csq) {
        if (csq == null)
            append("null"); //$NON-NLS-1$
        else {
    		int len = csq.length();
    		for (int i = 0; i < len; i++) {
    			append(csq.charAt(i));
    		}
        }
        return this;
    }
799501
public FileAppendWriter write(String content) {
        try {
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return this;
    }
183980
public static void write(final CharSequence data, final File file) throws IOException {
		Validate.notNull(file);
		Validate.notNull(data);

		try (BufferedWriter writer = Files.newBufferedWriter(file.toPath(), Charsets.UTF_8)) {
			writer.append(data);
		}
	}
727273
@Override
    public Writer append(CharSequence csq, int start, int end) throws IOException {
        encodedAppender.append(encoder, null, csq, 0, end - start);
        return this;
    }
869092
public void encodeToStream(Encoder thisInstance, CharSequence source, int offset, int len, EncodedAppender appender,
            EncodingState encodingState) throws IOException {
        appender.appendEncoded(thisInstance, encodingState, source, offset, len);
    }
954391
public static void encode(CharSequence in, Appendable out) throws IOException{
		encode(in, UTF_8, out);
	}