Java Code Examples for java.io.DataInputStream#readChar()

The following examples show how to use java.io.DataInputStream#readChar() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Parameter.java    From djl with Apache License 2.0 6 votes vote down vote up
/**
 * Loads parameter NDArrays from InputStream.
 *
 * <p>Currently, we cannot deserialize into the exact subclass of NDArray. The SparseNDArray
 * will be loaded as NDArray only.
 *
 * @param manager the NDManager
 * @param dis the InputStream
 * @throws IOException if failed to read
 * @throws MalformedModelException Exception thrown when model is not in expected format
 *     (parameters).
 */
public void load(NDManager manager, DataInputStream dis)
        throws IOException, MalformedModelException {
    char magic = dis.readChar();
    if (magic == 'N') {
        return;
    } else if (magic != 'P') {
        throw new MalformedModelException("Invalid input data.");
    }

    // Version
    byte version = dis.readByte();
    if (version != VERSION) {
        throw new MalformedModelException("Unsupported encoding version: " + version);
    }

    String parameterName = dis.readUTF();
    if (!parameterName.equals(getName())) {
        throw new MalformedModelException(
                "Unexpected parameter name: " + parameterName + ", expected: " + name);
    }

    array = manager.decode(dis);
}
 
Example 2
Source File: InventoryScreen.java    From Alite with GNU General Public License v3.0 6 votes vote down vote up
public static boolean initialize(Alite alite, final DataInputStream dis) {
	InventoryScreen is = new InventoryScreen(alite);
	try {
		byte selectionLength = dis.readByte();
		if (selectionLength > 0) {
			is.pendingSelection = "";
			while (selectionLength > 0) {
				is.pendingSelection += dis.readChar();
				selectionLength--;
			}
		}
	} catch (Exception e) {
		AliteLog.e("Inventory Screen Initialize", "Error in initializer.", e);
		return false;
	}
	alite.setScreen(is);
	return true;
}
 
Example 3
Source File: Trie.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
* <p>Parses the inputstream and creates the trie index with it.</p>
* <p>This is overwritten by the child classes.
* @param inputStream input stream containing the trie information
* @exception IOException thrown when data reading fails.
*/
protected void unserialize(InputStream inputStream) throws IOException
{
    //indexLength is a multiple of 1024 >> INDEX_STAGE_2_SHIFT_
    m_index_              = new char[m_dataOffset_];
    DataInputStream input = new DataInputStream(inputStream);
    for (int i = 0; i < m_dataOffset_; i ++) {
         m_index_[i] = input.readChar();
    }
}
 
Example 4
Source File: ConstrictorMission.java    From Alite with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void load(DataInputStream dis) throws IOException {
	galaxySeed = new char[3];
	galaxySeed[0] = dis.readChar();
	galaxySeed[1] = dis.readChar();
	galaxySeed[2] = dis.readChar();
	targetIndex = dis.readInt();
	state = dis.readInt();
	resetTargetName();
	active = true;
	started = true;
}
 
Example 5
Source File: Trie.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
* <p>Parses the inputstream and creates the trie index with it.</p>
* <p>This is overwritten by the child classes.
* @param inputStream input stream containing the trie information
* @exception IOException thrown when data reading fails.
*/
protected void unserialize(InputStream inputStream) throws IOException
{
    //indexLength is a multiple of 1024 >> INDEX_STAGE_2_SHIFT_
    m_index_              = new char[m_dataOffset_];
    DataInputStream input = new DataInputStream(inputStream);
    for (int i = 0; i < m_dataOffset_; i ++) {
         m_index_[i] = input.readChar();
    }
}
 
Example 6
Source File: ThargoidDocumentsMission.java    From Alite with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void load(DataInputStream dis) throws IOException {	
	galaxySeed = new char[3];
	galaxySeed[0] = dis.readChar();
	galaxySeed[1] = dis.readChar();
	galaxySeed[2] = dis.readChar();
	targetIndex = dis.readInt();
	state = dis.readInt();
	resetTargetName();
	active = true;
	started = true;
}
 
Example 7
Source File: SupernovaMission.java    From Alite with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void load(DataInputStream dis) throws IOException {	
	galaxySeed = new char[3];
	galaxySeed[0] = dis.readChar();
	galaxySeed[1] = dis.readChar();
	galaxySeed[2] = dis.readChar();
	supernovaSystemIndex = dis.readInt();
	state = dis.readInt();
	active = true;
	started = true;
}
 
Example 8
Source File: CougarMission.java    From Alite with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void load(DataInputStream dis) throws IOException {	
	galaxySeed = new char[3];
	galaxySeed[0] = dis.readChar();
	galaxySeed[1] = dis.readChar();
	galaxySeed[2] = dis.readChar();
	targetIndex = dis.readInt();
	state = dis.readInt();
	active = true;
	started = true;
}
 
Example 9
Source File: CharTrie.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
* <p>Parses the input stream and stores its trie content into a index and
* data array</p>
* @param inputStream data input stream containing trie data
* @exception IOException thrown when data reading fails
*/
protected final void unserialize(InputStream inputStream)
                                            throws IOException
{
    DataInputStream input = new DataInputStream(inputStream);
    int indexDataLength = m_dataOffset_ + m_dataLength_;
    m_index_ = new char[indexDataLength];
    for (int i = 0; i < indexDataLength; i ++) {
        m_index_[i] = input.readChar();
    }
    m_data_           = m_index_;
    m_initialValue_   = m_data_[m_dataOffset_];
}
 
Example 10
Source File: Trie.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/**
* <p>Parses the inputstream and creates the trie index with it.</p>
* <p>This is overwritten by the child classes.
* @param inputStream input stream containing the trie information
* @exception IOException thrown when data reading fails.
*/
protected void unserialize(InputStream inputStream) throws IOException
{
    //indexLength is a multiple of 1024 >> INDEX_STAGE_2_SHIFT_
    m_index_              = new char[m_dataOffset_];
    DataInputStream input = new DataInputStream(inputStream);
    for (int i = 0; i < m_dataOffset_; i ++) {
         m_index_[i] = input.readChar();
    }
}
 
Example 11
Source File: CharTrie.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
* <p>Parses the input stream and stores its trie content into a index and
* data array</p>
* @param inputStream data input stream containing trie data
* @exception IOException thrown when data reading fails
*/
protected final void unserialize(InputStream inputStream)
                                            throws IOException
{
    DataInputStream input = new DataInputStream(inputStream);
    int indexDataLength = m_dataOffset_ + m_dataLength_;
    m_index_ = new char[indexDataLength];
    for (int i = 0; i < indexDataLength; i ++) {
        m_index_[i] = input.readChar();
    }
    m_data_           = m_index_;
    m_initialValue_   = m_data_[m_dataOffset_];
}
 
Example 12
Source File: Trie.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
* <p>Parses the inputstream and creates the trie index with it.</p>
* <p>This is overwritten by the child classes.
* @param inputStream input stream containing the trie information
* @exception IOException thrown when data reading fails.
*/
protected void unserialize(InputStream inputStream) throws IOException
{
    //indexLength is a multiple of 1024 >> INDEX_STAGE_2_SHIFT_
    m_index_              = new char[m_dataOffset_];
    DataInputStream input = new DataInputStream(inputStream);
    for (int i = 0; i < m_dataOffset_; i ++) {
         m_index_[i] = input.readChar();
    }
}
 
Example 13
Source File: TutHud.java    From Alite with GNU General Public License v3.0 5 votes vote down vote up
public static boolean initialize(Alite alite, DataInputStream dis) {		
	try {
		FlightScreen fs = FlightScreen.createScreen(alite, dis);
		TutHud th = new TutHud(alite, fs);
		th.currentLineIndex = dis.readInt();
		th.savedGalaxySeed = new char[3];
		th.savedGalaxySeed[0] = dis.readChar();
		th.savedGalaxySeed[1] = dis.readChar();
		th.savedGalaxySeed[2] = dis.readChar();
		alite.getGenerator().buildGalaxy(th.savedGalaxySeed[0], th.savedGalaxySeed[1], th.savedGalaxySeed[2]);
		int systemIndex = dis.readInt();
		int hyperspaceIndex = dis.readInt();
		th.savedPresentSystem = systemIndex == -1 ? null : alite.getGenerator().getSystem(systemIndex);
		th.savedHyperspaceSystem = hyperspaceIndex == -1 ? null : alite.getGenerator().getSystem(hyperspaceIndex);
		th.savedInstalledEquipment = new ArrayList<Equipment>();
		int numEquip = dis.readInt();
		for (int i = 0; i < numEquip; i++) {
			th.savedInstalledEquipment.add(EquipmentStore.fromInt(dis.readByte()));
		}
		th.savedFuel = dis.readInt();
		for (int i = 0; i < 4; i++) {
			int laser = dis.readInt();
			th.savedLasers[i] = laser < 0 ? null : (Laser) EquipmentStore.fromInt(laser); 
		}
		for (int i = 0; i < Settings.buttonPosition.length; i++) {
			th.savedButtonConfiguration[i] = dis.readInt();
		}
		th.savedMarketFluct = dis.readInt();
		th.savedLegalStatus = LegalStatus.values()[dis.readInt()];
		th.savedLegalValue = dis.readInt();
		alite.setScreen(th);
	} catch (Exception e) {
		AliteLog.e("Tutorial HUD Screen Initialize", "Error in initializer.", e);
		return false;			
	}		
	return true;
}
 
Example 14
Source File: ICUBinary.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static final byte[] readHeader(InputStream inputStream,
                                    byte dataFormatIDExpected[],
                                    Authenticate authenticate)
                                                      throws IOException
{
    DataInputStream input = new DataInputStream(inputStream);
    char headersize = input.readChar();
    int readcount = 2;
    //reading the header format
    byte magic1 = input.readByte();
    readcount ++;
    byte magic2 = input.readByte();
    readcount ++;
    if (magic1 != MAGIC1 || magic2 != MAGIC2) {
        throw new IOException(MAGIC_NUMBER_AUTHENTICATION_FAILED_);
    }

    input.readChar(); // reading size
    readcount += 2;
    input.readChar(); // reading reserved word
    readcount += 2;
    byte bigendian    = input.readByte();
    readcount ++;
    byte charset      = input.readByte();
    readcount ++;
    byte charsize     = input.readByte();
    readcount ++;
    input.readByte(); // reading reserved byte
    readcount ++;

    byte dataFormatID[] = new byte[4];
    input.readFully(dataFormatID);
    readcount += 4;
    byte dataVersion[] = new byte[4];
    input.readFully(dataVersion);
    readcount += 4;
    byte unicodeVersion[] = new byte[4];
    input.readFully(unicodeVersion);
    readcount += 4;
    if (headersize < readcount) {
        throw new IOException("Internal Error: Header size error");
    }
    input.skipBytes(headersize - readcount);

    if (bigendian != BIG_ENDIAN_ || charset != CHAR_SET_
        || charsize != CHAR_SIZE_
        || !Arrays.equals(dataFormatIDExpected, dataFormatID)
        || (authenticate != null
            && !authenticate.isDataVersionAcceptable(dataVersion))) {
        throw new IOException(HEADER_AUTHENTICATION_FAILED_);
    }
    return unicodeVersion;
}
 
Example 15
Source File: ICUBinary.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
* <p>ICU data header reader method.
* Takes a ICU generated big-endian input stream, parse the ICU standard
* file header and authenticates them.</p>
* <p>Header format:
* <ul>
*     <li> Header size (char)
*     <li> Magic number 1 (byte)
*     <li> Magic number 2 (byte)
*     <li> Rest of the header size (char)
*     <li> Reserved word (char)
*     <li> Big endian indicator (byte)
*     <li> Character set family indicator (byte)
*     <li> Size of a char (byte) for c++ and c use
*     <li> Reserved byte (byte)
*     <li> Data format identifier (4 bytes), each ICU data has its own
*          identifier to distinguish them. [0] major [1] minor
*                                          [2] milli [3] micro
*     <li> Data version (4 bytes), the change version of the ICU data
*                             [0] major [1] minor [2] milli [3] micro
*     <li> Unicode version (4 bytes) this ICU is based on.
* </ul>
* </p>
* <p>
* Example of use:<br>
* <pre>
* try {
*    FileInputStream input = new FileInputStream(filename);
*    If (Utility.readICUDataHeader(input, dataformat, dataversion,
*                                  unicode) {
*        System.out.println("Verified file header, this is a ICU data file");
*    }
* } catch (IOException e) {
*    System.out.println("This is not a ICU data file");
* }
* </pre>
* </p>
* @param inputStream input stream that contains the ICU data header
* @param dataFormatIDExpected Data format expected. An array of 4 bytes
*                     information about the data format.
*                     E.g. data format ID 1.2.3.4. will became an array of
*                     {1, 2, 3, 4}
* @param authenticate user defined extra data authentication. This value
*                     can be null, if no extra authentication is needed.
* @exception IOException thrown if there is a read error or
*            when header authentication fails.
* @draft 2.1
*/
public static final byte[] readHeader(InputStream inputStream,
                                    byte dataFormatIDExpected[],
                                    Authenticate authenticate)
                                                      throws IOException
{
    DataInputStream input = new DataInputStream(inputStream);
    char headersize = input.readChar();
    int readcount = 2;
    //reading the header format
    byte magic1 = input.readByte();
    readcount ++;
    byte magic2 = input.readByte();
    readcount ++;
    if (magic1 != MAGIC1 || magic2 != MAGIC2) {
        throw new IOException(MAGIC_NUMBER_AUTHENTICATION_FAILED_);
    }

    input.readChar(); // reading size
    readcount += 2;
    input.readChar(); // reading reserved word
    readcount += 2;
    byte bigendian    = input.readByte();
    readcount ++;
    byte charset      = input.readByte();
    readcount ++;
    byte charsize     = input.readByte();
    readcount ++;
    input.readByte(); // reading reserved byte
    readcount ++;

    byte dataFormatID[] = new byte[4];
    input.readFully(dataFormatID);
    readcount += 4;
    byte dataVersion[] = new byte[4];
    input.readFully(dataVersion);
    readcount += 4;
    byte unicodeVersion[] = new byte[4];
    input.readFully(unicodeVersion);
    readcount += 4;
    if (headersize < readcount) {
        throw new IOException("Internal Error: Header size error");
    }
    input.skipBytes(headersize - readcount);

    if (bigendian != BIG_ENDIAN_ || charset != CHAR_SET_
        || charsize != CHAR_SIZE_
        || !Arrays.equals(dataFormatIDExpected, dataFormatID)
        || (authenticate != null
            && !authenticate.isDataVersionAcceptable(dataVersion))) {
        throw new IOException(HEADER_AUTHENTICATION_FAILED_);
    }
    return unicodeVersion;
}
 
Example 16
Source File: ICUBinary.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
* <p>ICU data header reader method.
* Takes a ICU generated big-endian input stream, parse the ICU standard
* file header and authenticates them.</p>
* <p>Header format:
* <ul>
*     <li> Header size (char)
*     <li> Magic number 1 (byte)
*     <li> Magic number 2 (byte)
*     <li> Rest of the header size (char)
*     <li> Reserved word (char)
*     <li> Big endian indicator (byte)
*     <li> Character set family indicator (byte)
*     <li> Size of a char (byte) for c++ and c use
*     <li> Reserved byte (byte)
*     <li> Data format identifier (4 bytes), each ICU data has its own
*          identifier to distinguish them. [0] major [1] minor
*                                          [2] milli [3] micro
*     <li> Data version (4 bytes), the change version of the ICU data
*                             [0] major [1] minor [2] milli [3] micro
*     <li> Unicode version (4 bytes) this ICU is based on.
* </ul>
* </p>
* <p>
* Example of use:<br>
* <pre>
* try {
*    FileInputStream input = new FileInputStream(filename);
*    If (Utility.readICUDataHeader(input, dataformat, dataversion,
*                                  unicode) {
*        System.out.println("Verified file header, this is a ICU data file");
*    }
* } catch (IOException e) {
*    System.out.println("This is not a ICU data file");
* }
* </pre>
* </p>
* @param inputStream input stream that contains the ICU data header
* @param dataFormatIDExpected Data format expected. An array of 4 bytes
*                     information about the data format.
*                     E.g. data format ID 1.2.3.4. will became an array of
*                     {1, 2, 3, 4}
* @param authenticate user defined extra data authentication. This value
*                     can be null, if no extra authentication is needed.
* @exception IOException thrown if there is a read error or
*            when header authentication fails.
* @draft 2.1
*/
public static final byte[] readHeader(InputStream inputStream,
                                    byte dataFormatIDExpected[],
                                    Authenticate authenticate)
                                                      throws IOException
{
    DataInputStream input = new DataInputStream(inputStream);
    char headersize = input.readChar();
    int readcount = 2;
    //reading the header format
    byte magic1 = input.readByte();
    readcount ++;
    byte magic2 = input.readByte();
    readcount ++;
    if (magic1 != MAGIC1 || magic2 != MAGIC2) {
        throw new IOException(MAGIC_NUMBER_AUTHENTICATION_FAILED_);
    }

    input.readChar(); // reading size
    readcount += 2;
    input.readChar(); // reading reserved word
    readcount += 2;
    byte bigendian    = input.readByte();
    readcount ++;
    byte charset      = input.readByte();
    readcount ++;
    byte charsize     = input.readByte();
    readcount ++;
    input.readByte(); // reading reserved byte
    readcount ++;

    byte dataFormatID[] = new byte[4];
    input.readFully(dataFormatID);
    readcount += 4;
    byte dataVersion[] = new byte[4];
    input.readFully(dataVersion);
    readcount += 4;
    byte unicodeVersion[] = new byte[4];
    input.readFully(unicodeVersion);
    readcount += 4;
    if (headersize < readcount) {
        throw new IOException("Internal Error: Header size error");
    }
    input.skipBytes(headersize - readcount);

    if (bigendian != BIG_ENDIAN_ || charset != CHAR_SET_
        || charsize != CHAR_SIZE_
        || !Arrays.equals(dataFormatIDExpected, dataFormatID)
        || (authenticate != null
            && !authenticate.isDataVersionAcceptable(dataVersion))) {
        throw new IOException(HEADER_AUTHENTICATION_FAILED_);
    }
    return unicodeVersion;
}
 
Example 17
Source File: ICUBinary.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
* <p>ICU data header reader method.
* Takes a ICU generated big-endian input stream, parse the ICU standard
* file header and authenticates them.</p>
* <p>Header format:
* <ul>
*     <li> Header size (char)
*     <li> Magic number 1 (byte)
*     <li> Magic number 2 (byte)
*     <li> Rest of the header size (char)
*     <li> Reserved word (char)
*     <li> Big endian indicator (byte)
*     <li> Character set family indicator (byte)
*     <li> Size of a char (byte) for c++ and c use
*     <li> Reserved byte (byte)
*     <li> Data format identifier (4 bytes), each ICU data has its own
*          identifier to distinguish them. [0] major [1] minor
*                                          [2] milli [3] micro
*     <li> Data version (4 bytes), the change version of the ICU data
*                             [0] major [1] minor [2] milli [3] micro
*     <li> Unicode version (4 bytes) this ICU is based on.
* </ul>
* </p>
* <p>
* Example of use:<br>
* <pre>
* try {
*    FileInputStream input = new FileInputStream(filename);
*    If (Utility.readICUDataHeader(input, dataformat, dataversion,
*                                  unicode) {
*        System.out.println("Verified file header, this is a ICU data file");
*    }
* } catch (IOException e) {
*    System.out.println("This is not a ICU data file");
* }
* </pre>
* </p>
* @param inputStream input stream that contains the ICU data header
* @param dataFormatIDExpected Data format expected. An array of 4 bytes
*                     information about the data format.
*                     E.g. data format ID 1.2.3.4. will became an array of
*                     {1, 2, 3, 4}
* @param authenticate user defined extra data authentication. This value
*                     can be null, if no extra authentication is needed.
* @exception IOException thrown if there is a read error or
*            when header authentication fails.
* @draft 2.1
*/
public static final byte[] readHeader(InputStream inputStream,
                                    byte dataFormatIDExpected[],
                                    Authenticate authenticate)
                                                      throws IOException
{
    DataInputStream input = new DataInputStream(inputStream);
    char headersize = input.readChar();
    int readcount = 2;
    //reading the header format
    byte magic1 = input.readByte();
    readcount ++;
    byte magic2 = input.readByte();
    readcount ++;
    if (magic1 != MAGIC1 || magic2 != MAGIC2) {
        throw new IOException(MAGIC_NUMBER_AUTHENTICATION_FAILED_);
    }

    input.readChar(); // reading size
    readcount += 2;
    input.readChar(); // reading reserved word
    readcount += 2;
    byte bigendian    = input.readByte();
    readcount ++;
    byte charset      = input.readByte();
    readcount ++;
    byte charsize     = input.readByte();
    readcount ++;
    input.readByte(); // reading reserved byte
    readcount ++;

    byte dataFormatID[] = new byte[4];
    input.readFully(dataFormatID);
    readcount += 4;
    byte dataVersion[] = new byte[4];
    input.readFully(dataVersion);
    readcount += 4;
    byte unicodeVersion[] = new byte[4];
    input.readFully(unicodeVersion);
    readcount += 4;
    if (headersize < readcount) {
        throw new IOException("Internal Error: Header size error");
    }
    input.skipBytes(headersize - readcount);

    if (bigendian != BIG_ENDIAN_ || charset != CHAR_SET_
        || charsize != CHAR_SIZE_
        || !Arrays.equals(dataFormatIDExpected, dataFormatID)
        || (authenticate != null
            && !authenticate.isDataVersionAcceptable(dataVersion))) {
        throw new IOException(HEADER_AUTHENTICATION_FAILED_);
    }
    return unicodeVersion;
}
 
Example 18
Source File: ICUBinary.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
public static final byte[] readHeader(InputStream inputStream,
                                    byte dataFormatIDExpected[],
                                    Authenticate authenticate)
                                                      throws IOException
{
    DataInputStream input = new DataInputStream(inputStream);
    char headersize = input.readChar();
    int readcount = 2;
    //reading the header format
    byte magic1 = input.readByte();
    readcount ++;
    byte magic2 = input.readByte();
    readcount ++;
    if (magic1 != MAGIC1 || magic2 != MAGIC2) {
        throw new IOException(MAGIC_NUMBER_AUTHENTICATION_FAILED_);
    }

    input.readChar(); // reading size
    readcount += 2;
    input.readChar(); // reading reserved word
    readcount += 2;
    byte bigendian    = input.readByte();
    readcount ++;
    byte charset      = input.readByte();
    readcount ++;
    byte charsize     = input.readByte();
    readcount ++;
    input.readByte(); // reading reserved byte
    readcount ++;

    byte dataFormatID[] = new byte[4];
    input.readFully(dataFormatID);
    readcount += 4;
    byte dataVersion[] = new byte[4];
    input.readFully(dataVersion);
    readcount += 4;
    byte unicodeVersion[] = new byte[4];
    input.readFully(unicodeVersion);
    readcount += 4;
    if (headersize < readcount) {
        throw new IOException("Internal Error: Header size error");
    }
    input.skipBytes(headersize - readcount);

    if (bigendian != BIG_ENDIAN_ || charset != CHAR_SET_
        || charsize != CHAR_SIZE_
        || !Arrays.equals(dataFormatIDExpected, dataFormatID)
        || (authenticate != null
            && !authenticate.isDataVersionAcceptable(dataVersion))) {
        throw new IOException(HEADER_AUTHENTICATION_FAILED_);
    }
    return unicodeVersion;
}
 
Example 19
Source File: ICUBinary.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
* <p>ICU data header reader method.
* Takes a ICU generated big-endian input stream, parse the ICU standard
* file header and authenticates them.</p>
* <p>Header format:
* <ul>
*     <li> Header size (char)
*     <li> Magic number 1 (byte)
*     <li> Magic number 2 (byte)
*     <li> Rest of the header size (char)
*     <li> Reserved word (char)
*     <li> Big endian indicator (byte)
*     <li> Character set family indicator (byte)
*     <li> Size of a char (byte) for c++ and c use
*     <li> Reserved byte (byte)
*     <li> Data format identifier (4 bytes), each ICU data has its own
*          identifier to distinguish them. [0] major [1] minor
*                                          [2] milli [3] micro
*     <li> Data version (4 bytes), the change version of the ICU data
*                             [0] major [1] minor [2] milli [3] micro
*     <li> Unicode version (4 bytes) this ICU is based on.
* </ul>
* </p>
* <p>
* Example of use:<br>
* <pre>
* try {
*    FileInputStream input = new FileInputStream(filename);
*    If (Utility.readICUDataHeader(input, dataformat, dataversion,
*                                  unicode) {
*        System.out.println("Verified file header, this is a ICU data file");
*    }
* } catch (IOException e) {
*    System.out.println("This is not a ICU data file");
* }
* </pre>
* </p>
* @param inputStream input stream that contains the ICU data header
* @param dataFormatIDExpected Data format expected. An array of 4 bytes
*                     information about the data format.
*                     E.g. data format ID 1.2.3.4. will became an array of
*                     {1, 2, 3, 4}
* @param authenticate user defined extra data authentication. This value
*                     can be null, if no extra authentication is needed.
* @exception IOException thrown if there is a read error or
*            when header authentication fails.
* @draft 2.1
*/
public static final byte[] readHeader(InputStream inputStream,
                                    byte dataFormatIDExpected[],
                                    Authenticate authenticate)
                                                      throws IOException
{
    DataInputStream input = new DataInputStream(inputStream);
    char headersize = input.readChar();
    int readcount = 2;
    //reading the header format
    byte magic1 = input.readByte();
    readcount ++;
    byte magic2 = input.readByte();
    readcount ++;
    if (magic1 != MAGIC1 || magic2 != MAGIC2) {
        throw new IOException(MAGIC_NUMBER_AUTHENTICATION_FAILED_);
    }

    input.readChar(); // reading size
    readcount += 2;
    input.readChar(); // reading reserved word
    readcount += 2;
    byte bigendian    = input.readByte();
    readcount ++;
    byte charset      = input.readByte();
    readcount ++;
    byte charsize     = input.readByte();
    readcount ++;
    input.readByte(); // reading reserved byte
    readcount ++;

    byte dataFormatID[] = new byte[4];
    input.readFully(dataFormatID);
    readcount += 4;
    byte dataVersion[] = new byte[4];
    input.readFully(dataVersion);
    readcount += 4;
    byte unicodeVersion[] = new byte[4];
    input.readFully(unicodeVersion);
    readcount += 4;
    if (headersize < readcount) {
        throw new IOException("Internal Error: Header size error");
    }
    input.skipBytes(headersize - readcount);

    if (bigendian != BIG_ENDIAN_ || charset != CHAR_SET_
        || charsize != CHAR_SIZE_
        || !Arrays.equals(dataFormatIDExpected, dataFormatID)
        || (authenticate != null
            && !authenticate.isDataVersionAcceptable(dataVersion))) {
        throw new IOException(HEADER_AUTHENTICATION_FAILED_);
    }
    return unicodeVersion;
}
 
Example 20
Source File: ICUBinary.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
* <p>ICU data header reader method.
* Takes a ICU generated big-endian input stream, parse the ICU standard
* file header and authenticates them.</p>
* <p>Header format:
* <ul>
*     <li> Header size (char)
*     <li> Magic number 1 (byte)
*     <li> Magic number 2 (byte)
*     <li> Rest of the header size (char)
*     <li> Reserved word (char)
*     <li> Big endian indicator (byte)
*     <li> Character set family indicator (byte)
*     <li> Size of a char (byte) for c++ and c use
*     <li> Reserved byte (byte)
*     <li> Data format identifier (4 bytes), each ICU data has its own
*          identifier to distinguish them. [0] major [1] minor
*                                          [2] milli [3] micro
*     <li> Data version (4 bytes), the change version of the ICU data
*                             [0] major [1] minor [2] milli [3] micro
*     <li> Unicode version (4 bytes) this ICU is based on.
* </ul>
* </p>
* <p>
* Example of use:<br>
* <pre>
* try {
*    FileInputStream input = new FileInputStream(filename);
*    If (Utility.readICUDataHeader(input, dataformat, dataversion,
*                                  unicode) {
*        System.out.println("Verified file header, this is a ICU data file");
*    }
* } catch (IOException e) {
*    System.out.println("This is not a ICU data file");
* }
* </pre>
* </p>
* @param inputStream input stream that contains the ICU data header
* @param dataFormatIDExpected Data format expected. An array of 4 bytes
*                     information about the data format.
*                     E.g. data format ID 1.2.3.4. will became an array of
*                     {1, 2, 3, 4}
* @param authenticate user defined extra data authentication. This value
*                     can be null, if no extra authentication is needed.
* @exception IOException thrown if there is a read error or
*            when header authentication fails.
* @draft 2.1
*/
public static final byte[] readHeader(InputStream inputStream,
                                    byte dataFormatIDExpected[],
                                    Authenticate authenticate)
                                                      throws IOException
{
    DataInputStream input = new DataInputStream(inputStream);
    char headersize = input.readChar();
    int readcount = 2;
    //reading the header format
    byte magic1 = input.readByte();
    readcount ++;
    byte magic2 = input.readByte();
    readcount ++;
    if (magic1 != MAGIC1 || magic2 != MAGIC2) {
        throw new IOException(MAGIC_NUMBER_AUTHENTICATION_FAILED_);
    }

    input.readChar(); // reading size
    readcount += 2;
    input.readChar(); // reading reserved word
    readcount += 2;
    byte bigendian    = input.readByte();
    readcount ++;
    byte charset      = input.readByte();
    readcount ++;
    byte charsize     = input.readByte();
    readcount ++;
    input.readByte(); // reading reserved byte
    readcount ++;

    byte dataFormatID[] = new byte[4];
    input.readFully(dataFormatID);
    readcount += 4;
    byte dataVersion[] = new byte[4];
    input.readFully(dataVersion);
    readcount += 4;
    byte unicodeVersion[] = new byte[4];
    input.readFully(unicodeVersion);
    readcount += 4;
    if (headersize < readcount) {
        throw new IOException("Internal Error: Header size error");
    }
    input.skipBytes(headersize - readcount);

    if (bigendian != BIG_ENDIAN_ || charset != CHAR_SET_
        || charsize != CHAR_SIZE_
        || !Arrays.equals(dataFormatIDExpected, dataFormatID)
        || (authenticate != null
            && !authenticate.isDataVersionAcceptable(dataVersion))) {
        throw new IOException(HEADER_AUTHENTICATION_FAILED_);
    }
    return unicodeVersion;
}