// Path: src/main/java/org/rr/mobi4java/ByteUtils.java
// public static int getInt(byte[] buffer, int offset, int length) {
// 	return getInt(getBytes(buffer, offset, length));
// }
// 
// Path: src/main/java/org/rr/mobi4java/ByteUtils.java
// public static String getString(byte[] buffer, int offset, int length) {
// 	return getString(getBytes(buffer, offset, length));
// }
// 
// Path: src/main/java/org/rr/mobi4java/ByteUtils.java
// public static void writeInt(int data, int length, OutputStream out) throws IOException {
// 	out.write(getBytes(data, new byte[length]), 0, length);
// }
// 
// Path: src/main/java/org/rr/mobi4java/ByteUtils.java
// public static void writeString(String data, int length, OutputStream out) throws IOException {
// 	byte[] s = getBytes(data);
// 	byte[] b = new byte[length];
// 	System.arraycopy(s, 0, b, 0, s.length);
// 	out.write(b, 0, length);
// }


import static org.apache.commons.lang3.BooleanUtils.negate;
import static org.rr.mobi4java.ByteUtils.getInt;
import static org.rr.mobi4java.ByteUtils.getString;
import static org.rr.mobi4java.ByteUtils.writeInt;
import static org.rr.mobi4java.ByteUtils.writeString;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.io.output.TeeOutputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
package org.rr.mobi4java;




public class MobiContentIdxt extends MobiContent {
	
	private static final String IDENTIFIER = "IDXT";

	private int[] indexEntriesIndices;
	
	private int indexEntriesCount;
	
	MobiContentIdxt(byte[] content, int entriesCount) throws IOException {
		super(content, CONTENT_TYPE.IDXT);
		readMobiIdxt(entriesCount);
	}

	private void readMobiIdxt(int entriesCount) throws IOException {
		String identifier = getString(content, 0, 4);
		if (negate(StringUtils.equals(identifier, IDENTIFIER))) {
			throw new IOException("Expected to find IDXT header identifier IDXT but got '" + identifier + "' instead");
		}
		
		indexEntriesIndices = new int[entriesCount];
		for (int i = 0; i < entriesCount; i++) {
			indexEntriesIndices[i] = getInt(content, 4 + (i * 2), 2);
		}
		
		indexEntriesCount = getInt(content, 4 + (indexEntriesIndices.length * 2), 2);
	}
	
	@Override
	byte[] writeContent(OutputStream out) throws IOException {
		ByteArrayOutputStream branch = new ByteArrayOutputStream();
		TeeOutputStream tee = new TeeOutputStream(out, branch);
// Path: src/main/java/org/rr/mobi4java/ByteUtils.java
// public static int getInt(byte[] buffer, int offset, int length) {
// 	return getInt(getBytes(buffer, offset, length));
// }
// 
// Path: src/main/java/org/rr/mobi4java/ByteUtils.java
// public static String getString(byte[] buffer, int offset, int length) {
// 	return getString(getBytes(buffer, offset, length));
// }
// 
// Path: src/main/java/org/rr/mobi4java/ByteUtils.java
// public static void writeInt(int data, int length, OutputStream out) throws IOException {
// 	out.write(getBytes(data, new byte[length]), 0, length);
// }
// 
// Path: src/main/java/org/rr/mobi4java/ByteUtils.java
// public static void writeString(String data, int length, OutputStream out) throws IOException {
// 	byte[] s = getBytes(data);
// 	byte[] b = new byte[length];
// 	System.arraycopy(s, 0, b, 0, s.length);
// 	out.write(b, 0, length);
// }


// Path: src/main/java/org/rr/mobi4java/MobiContentIdxt.java
import static org.apache.commons.lang3.BooleanUtils.negate;
import static org.rr.mobi4java.ByteUtils.getInt;
import static org.rr.mobi4java.ByteUtils.getString;
import static org.rr.mobi4java.ByteUtils.writeInt;
import static org.rr.mobi4java.ByteUtils.writeString;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.commons.io.output.TeeOutputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;

package org.rr.mobi4java;




public class MobiContentIdxt extends MobiContent {
	
	private static final String IDENTIFIER = "IDXT";

	private int[] indexEntriesIndices;
	
	private int indexEntriesCount;
	
	MobiContentIdxt(byte[] content, int entriesCount) throws IOException {
		super(content, CONTENT_TYPE.IDXT);
		readMobiIdxt(entriesCount);
	}

	private void readMobiIdxt(int entriesCount) throws IOException {
		String identifier = getString(content, 0, 4);
		if (negate(StringUtils.equals(identifier, IDENTIFIER))) {
			throw new IOException("Expected to find IDXT header identifier IDXT but got '" + identifier + "' instead");
		}
		
		indexEntriesIndices = new int[entriesCount];
		for (int i = 0; i < entriesCount; i++) {
			indexEntriesIndices[i] = getInt(content, 4 + (i * 2), 2);
		}
		
		indexEntriesCount = getInt(content, 4 + (indexEntriesIndices.length * 2), 2);
	}
	
	@Override
	byte[] writeContent(OutputStream out) throws IOException {
		ByteArrayOutputStream branch = new ByteArrayOutputStream();
		TeeOutputStream tee = new TeeOutputStream(out, branch);
		writeString(IDENTIFIER, 4, tee);