~ubuntu-branches/ubuntu/oneiric/tuxguitar/oneiric

« back to all changes in this revision

Viewing changes to TuxGuitar-gtp/src/org/herac/tuxguitar/io/gtp/GTPOutputStream.java

  • Committer: Bazaar Package Importer
  • Author(s): Philippe Coval
  • Date: 2008-06-19 00:30:30 UTC
  • mto: (5.1.2 sid)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20080619003030-h719szrhsngou7c6
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.herac.tuxguitar.io.gtp;
 
2
 
 
3
import java.io.IOException;
 
4
import java.io.OutputStream;
 
5
 
 
6
import org.herac.tuxguitar.io.base.TGOutputStreamBase;
 
7
import org.herac.tuxguitar.song.factory.TGFactory;
 
8
 
 
9
public abstract class GTPOutputStream implements TGOutputStreamBase{
 
10
        
 
11
        private TGFactory factory;
 
12
        private OutputStream outputStream;
 
13
        
 
14
        public void init(TGFactory factory,OutputStream stream) {
 
15
                this.factory = factory;
 
16
                this.outputStream = stream;
 
17
        }
 
18
        
 
19
        protected TGFactory getFactory(){
 
20
                return this.factory;
 
21
        }
 
22
        
 
23
        protected void skipBytes(int count) throws IOException {
 
24
                for(int i = 0;i < count;i++){
 
25
                        this.outputStream.write(0);
 
26
                }
 
27
        }
 
28
        
 
29
        protected void writeByte(byte v) throws IOException {
 
30
                this.outputStream.write(v);
 
31
        }
 
32
        
 
33
        protected void writeUnsignedByte(int v) throws IOException {
 
34
                this.outputStream.write(v);
 
35
        }
 
36
        
 
37
        protected void writeBytes(byte[] v) throws IOException {
 
38
                this.outputStream.write(v);
 
39
        }
 
40
        
 
41
        protected void writeBoolean(boolean v) throws IOException {
 
42
                this.outputStream.write(v ? 1 : 0);
 
43
        }
 
44
        
 
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);
 
48
        }
 
49
        
 
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 ] );
 
54
                }
 
55
        }
 
56
        
 
57
        protected void writeStringInteger(String string) throws IOException {
 
58
                char[] chars = string.toCharArray();
 
59
                this.writeInt( chars.length );
 
60
                this.writeString( chars , 0 );
 
61
        }
 
62
        
 
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 );
 
68
        }
 
69
        
 
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);
 
74
        }
 
75
        
 
76
        protected void close() throws IOException{
 
77
                this.outputStream.flush();
 
78
                this.outputStream.close();
 
79
        }
 
80
}