1
package org.herac.tuxguitar.io.gtp;
3
import java.io.IOException;
4
import java.io.OutputStream;
6
import org.herac.tuxguitar.io.base.TGOutputStreamBase;
7
import org.herac.tuxguitar.song.factory.TGFactory;
9
public abstract class GTPOutputStream implements TGOutputStreamBase{
11
private TGFactory factory;
12
private OutputStream outputStream;
14
public void init(TGFactory factory,OutputStream stream) {
15
this.factory = factory;
16
this.outputStream = stream;
19
protected TGFactory getFactory(){
23
protected void skipBytes(int count) throws IOException {
24
for(int i = 0;i < count;i++){
25
this.outputStream.write(0);
29
protected void writeByte(byte v) throws IOException {
30
this.outputStream.write(v);
33
protected void writeUnsignedByte(int v) throws IOException {
34
this.outputStream.write(v);
37
protected void writeBytes(byte[] v) throws IOException {
38
this.outputStream.write(v);
41
protected void writeBoolean(boolean v) throws IOException {
42
this.outputStream.write(v ? 1 : 0);
45
protected void writeInt(int v) throws IOException {
46
byte[] bytes = { (byte)(v & 0x00FF),(byte)((v >> 8) & 0x000000FF),(byte) ((v >> 16) & 0x000000FF),(byte)((v >> 24) & 0x000000FF) };
47
this.outputStream.write(bytes);
50
protected void writeString(char[] chars, int maximumLength) throws IOException {
51
int length = (maximumLength == 0 || maximumLength > chars.length ? chars.length : maximumLength );
52
for(int i = 0 ; i < length; i ++){
53
this.outputStream.write( chars[ i ] );
57
protected void writeStringInteger(String string) throws IOException {
58
char[] chars = string.toCharArray();
59
this.writeInt( chars.length );
60
this.writeString( chars , 0 );
63
protected void writeStringByte(String string,int size) throws IOException {
64
char[] chars = string.toCharArray();
65
this.writeByte( (byte)( size == 0 || size > chars.length ? chars.length : size ));
66
this.writeString( chars , size );
67
this.skipBytes( size - chars.length );
70
protected void writeStringByteSizeOfInteger(String string) throws IOException {
71
char[] chars = string.toCharArray();
72
this.writeInt( (chars.length + 1) );
73
this.writeStringByte(string,chars.length);
76
protected void close() throws IOException{
77
this.outputStream.flush();
78
this.outputStream.close();