1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.mkstubs.sourcer; 18 19 import java.io.IOException; 20 import java.io.Writer; 21 22 /** 23 * An {@link Output} objects is an helper to write to a character stream {@link Writer}. 24 * <p/> 25 * It provide some helper methods to the various "sourcer" classes from this package 26 * to help them write to the underlying stream. 27 */ 28 public class Output { 29 30 private final Writer mWriter; 31 32 /** 33 * Creates a new {@link Output} object that wraps the given {@link Writer}. 34 * <p/> 35 * The caller is responsible of opening and closing the {@link Writer}. 36 * 37 * @param writer The writer to write to. Could be a file, a string, etc. 38 */ Output(Writer writer)39 public Output(Writer writer) { 40 mWriter = writer; 41 } 42 43 /** 44 * Writes a formatted string to the writer. 45 * 46 * @param format The format string. 47 * @param args The arguments for the format string. 48 * 49 * @see String#format(String, Object...) 50 */ write(String format, Object... args)51 public void write(String format, Object... args) { 52 try { 53 mWriter.write(String.format(format, args)); 54 } catch (IOException e) { 55 throw new RuntimeException(e); 56 } 57 } 58 59 /** 60 * Writes a single character to the writer. 61 * 62 * @param c The character to write. 63 */ write(char c)64 public void write(char c) { 65 write(Character.toString(c)); 66 } 67 68 /** 69 * Writes a {@link StringBuilder} to the writer. 70 * 71 * @param sb The {@link StringBuilder#toString()} method is used to ge the string to write. 72 */ write(StringBuilder sb)73 public void write(StringBuilder sb) { 74 write(sb.toString()); 75 } 76 } 77