EncodedWithHeader.java

package fr.sii.ogham.sms.encoder;

/**
 * The encoded message into a byte array also contains a header.
 * 
 * <p>
 * This happens for example when a message is split into several segments. The
 * header is used to indicate how the message was split (see
 * <a href="https://en.wikipedia.org/wiki/User_Data_Header">User Data
 * Header</a>).
 * 
 * <p>
 * {@link #getBytes()} returns the whole message (header + payload).
 * 
 * 
 * @author Aurélien Baudet
 *
 */
public class EncodedWithHeader implements Encoded {
	private final byte[] header;
	private final Encoded payload;
	private final byte[] wholeSegment;

	/**
	 * Initializes with the header byte array and the encoded message (byte
	 * array and charset unsed to encode it).
	 * 
	 * <p>
	 * The whole segment byte array is generated by creating a new array of
	 * bytes, appending header bytes and appending {@link Encoded#getBytes()}.
	 * 
	 * @param header
	 *            the header bytes
	 * @param payload
	 *            the encoded message without the header.
	 */
	public EncodedWithHeader(byte[] header, Encoded payload) {
		super();
		this.header = header;
		this.payload = payload;
		wholeSegment = new byte[header.length + payload.getBytes().length];
		System.arraycopy(header, 0, wholeSegment, 0, header.length);
		System.arraycopy(payload.getBytes(), 0, wholeSegment, header.length, payload.getBytes().length);
	}

	@Override
	public byte[] getBytes() {
		return wholeSegment;
	}

	@Override
	public String getCharsetName() {
		return payload.getCharsetName();
	}

	/**
	 * The encoded message may contain a header. This happens for example when a
	 * message is split into several segments. The header is used to indicate
	 * how the message was split (see
	 * <a href="https://en.wikipedia.org/wiki/User_Data_Header">User Data
	 * Header</a>).
	 * 
	 * @return the header bytes
	 */
	public byte[] getHeader() {
		return header;
	}

	/**
	 * The encoded message may contain a header. This happens for example when a
	 * message is split into several segments. The header is used to indicate
	 * how the message was split (see
	 * <a href="https://en.wikipedia.org/wiki/User_Data_Header">User Data
	 * Header</a>). This method gives access to the real message (named
	 * payload).
	 * 
	 * @return the payload (real message without header) bytes
	 */
	public Encoded getPayload() {
		return payload;
	}

}