summaryrefslogtreecommitdiffstats
blob: 437346faa320b10a0255350289b951a9d567a2b8 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * Copyright (c) 2011-2015, Isode Limited, London, England.
 * All rights reserved.
 */
package com.isode.stroke.base;

import com.isode.stroke.base.SafeByteArray;
import com.isode.stroke.base.ByteArray;

/**
* It's currently not actually secure,
* and that we might consider if http://developer.android.com/reference/java/nio/ByteBuffer.html#allocateDirect(int) could help us in the future.
*/
public class SafeByteArray extends ByteArray {

	public SafeByteArray() {

	}

	public SafeByteArray(String s) {
		super(s);
	}

	public SafeByteArray(ByteArray b) {
		super(b);
	}

	/**
	 * Constructs a new ByteArray containing the bytes in a user-supplied
	 * byte[]
	 * @param c an array of bytes, which must not be null, but may contain
	 * zero elements.
	 */
	public SafeByteArray(byte[] c) {
		super(c);
	}

	/**
	 * Creates a new SafeByteArray object containing all 
	 * the elements from two existing ByteArrays (immutable add).
	 * 
	 * @param a an existing SafeByteArray. Must not be null, but may be empty.
	 * @param b an existing SafeByteArray. Must not be null, but may be empty.
	 * @return a new SafeByteArray containing all the elements of <em>a</em>
	 * followed by all the elements of <em>b</em>.
	 */  
	public static SafeByteArray plus(SafeByteArray a, SafeByteArray b) {
		SafeByteArray x = new SafeByteArray(a);
		x.append(b);
		return x;
	}
	
	/**
	 * Updates the SafeByteArray by adding all the elements
	 * of another SafeByteArray to the end of the array (mutable add).
	 * @param b an existing SafeByteArray. Must not be null, but may be empty
	 * @return a reference to the updated object 
	 */
	public SafeByteArray append(ByteArray b) {
		super.append(b);
		return this;
	}

	public SafeByteArray append(SafeByteArray b) {
		append(b.getData());
		return this;
	}

	/** 
	 * Updates the SafeByteArray by adding all the bytes
	 * in a byte[] to the end of the array (mutable add).  
	 * 
	 * @param b an array of bytes. Must not be null, but may contain zero
	 * elements.
	 * 
	 * @return a reference to the updated object
	 */
	public SafeByteArray append(byte[] b) {
		super.append(b);
		return this;
	}

	/** Mutable add */
	public SafeByteArray append(byte[] b, int len) {
		super.append(b, len);
		return this;
	}

	/** 
	 * Updates the SafeByteArray by adding a single byte
	 * value to the end of the array (mutable add).
	 * @param b a single byte
	 * @return a reference to the updated object
	 */
	public SafeByteArray append(byte b) {
		super.append(b);
		return this;
	}

	/**
	 * Updates the SafeByteArray by adding all the bytes
	 * obtained by UTF-8 encoding the provided String to the end of the array (mutable add).
	 * @param s a String that must not be null.
	 * @return a reference to the updated object.
	 */ 
	public SafeByteArray append(String s) {
		super.append(s);
		return this;
	}
}