aboutsummaryrefslogtreecommitdiff
path: root/java/com/xagasoft/gats/GatsString.java
blob: 442ef67a6d33799b3b16c9afcf08d6dae802a138 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/*
 * Copyright (C) 2007-2012 Xagasoft, All rights reserved.
 *
 * This file is part of the libgats library and is released under the
 * terms of the license contained in the file LICENSE.
 */

package com.xagasoft.gats;

import java.io.InputStream;
import java.io.OutputStream;

/**
 * Represents a Gats string, that is, a string of 8-bit bytes.  Unlike the
 * standard Java string, a Gats string is a string of 8-bit bytes, not 16-bit
 * UCS characters.  If you want to transmit textual data, we highly recommend
 * encoding it into UTF-8.  You can do this by constructing a GatsString from
 * a java string thusly:  new GatsString( myStr.getBytes("UTF8") )
 * <p>
 * If you pass a java string into a GatsString instead of an array of bytes it
 * will simply call the getBytes function.  This may not be what you want in
 * many cases, but will work great for simple cases.
 */
public class GatsString extends GatsObject
{
	private byte[] aValue = null;

	public GatsString()
	{
	}

	public GatsString( String sValue )
	{
		this.aValue = sValue.getBytes();
	}

	public GatsString( byte[] aValue )
	{
		this.aValue = aValue;
	}

	public byte[] getValue()
	{
		return aValue;
	}

	public void setValue( String sValue )
	{
		this.aValue = sValue.getBytes();
	}

	public void setValue( byte[] aValue )
	{
		this.aValue = aValue;
	}

	public String toString()
	{
		return new String( aValue );
	}

	public int getType()
	{
		return GatsObject.STRING;
	}

	void read( InputStream is, char cType ) throws java.io.IOException
	{
		int lSize = (int)GatsInteger.readPackedInt( is );
		aValue = new byte[lSize];
		int lRead = 0;
		do
		{
			lRead += is.read( aValue, lRead, lSize-lRead );
		} while( lRead < lSize );
	}

	void write( OutputStream os ) throws java.io.IOException
	{
		os.write( (int)'s' );
		if( aValue == null )
		{
			GatsInteger.writePackedInt( os, 0 );
		}
		else
		{
			GatsInteger.writePackedInt( os, aValue.length );
			os.write( aValue );
		}
	}
};