1 /* 2 * Zemucan: A Syntax Assistant for DB2 3 * Copyright (C) 2009, 2010 Andres Gomez Casanova 4 * 5 * This file is part of Zemucan. 6 * 7 * Zemucan is free software: you can redistribute it and/or modify 8 * it under the terms of the GNU Lesser General Public License as published by 9 * the Free Software Foundation; either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * Zemucan is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU Lesser General Public License for more details. 16 * 17 * You should have received a copy of the GNU Lesser General Public License 18 * along with this library; if not, see <http://www.gnu.org/licenses/>. 19 * 20 * Contact: 21 * a n g o c a at y a h o o dot c o m 22 * Cra. 45 No 61 - 31, Bogota, Colombia. 23 * 24 * Author: $LastChangedBy: angoca $: 25 * Date: $LastChangedDate: 2011-03-05 23:06:11 -0500 (sáb, 05 mar 2011) $: 26 * Revision: $LastChangedRevision: 1906 $: 27 * URL: $HeadURL: https://zemucan.svn.sourceforge.net/svnroot/zemucan/branches/zemucan_v1/source-code/uiImplJLine/src/test/java/jline/Buffer.java $: 28 */ 29 package jline; 30 31 import java.io.ByteArrayOutputStream; 32 33 /** 34 * This class is a helper to commands in the console. 35 * <p> 36 * This class is based on a jline's test class written by Ryan. <b>Control 37 * Version</b> 38 * <p> 39 * <ul> 40 * <li>1.0.0 Class creation.</li> 41 * </ul> 42 * 43 * @author Andres Gomez Casanova <a 44 * href="mailto:a n g o c a at y a h o o dot c o m" >(AngocA)</a> 45 * @version 1.0.0 2010-05-13 46 */ 47 public class Buffer { 48 /** 49 * Stream that represents what the user writes. 50 */ 51 private final ByteArrayOutputStream byteOutStream = new ByteArrayOutputStream(); 52 53 /** 54 * Constructor with a given string. 55 * 56 * @param string 57 * String that represents what the user wrote. 58 */ 59 public Buffer(final String/* ! */string) { 60 this.append(string); 61 } 62 63 /** 64 * Adds a byte to the stream. 65 * 66 * @param b 67 * byte to add. 68 */ 69 private void append(final byte b) { 70 this.byteOutStream.write(b); 71 } 72 73 /** 74 * Adds a integer to the stream. 75 * 76 * @param integer 77 * integer to add. 78 */ 79 private void append(final int integer) { 80 this.append((byte) integer); 81 } 82 83 /** 84 * Appends a string to the stream. 85 * 86 * @param string 87 * String to add to the array. 88 */ 89 private void append(final String/* ! */string) { 90 final byte[] bytes = string.getBytes(); 91 92 for (int i = 0; i < bytes.length; i++) { 93 this.append(bytes[i]); 94 } 95 } 96 97 /** 98 * Retrieves the output stream as an array of bytes. 99 * 100 * @return Array of bytes. 101 */ 102 public final byte[] getBytes() { 103 return this.byteOutStream.toByteArray(); 104 } 105 106 /** 107 * Adds a operation to the stream. 108 * 109 * @param operation 110 * Operation to add. 111 */ 112 public final void op(final short operation) { 113 this.append(AssertionHelper.getKeyForAction(operation)); 114 } 115 }