com.jmatio.io.MatFileHeader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of matfilerw Show documentation
Show all versions of matfilerw Show documentation
MatFileRW - Read and write .mat files
/*
* Code licensed under new-style BSD (see LICENSE).
* All code up to tags/original: Copyright (c) 2006, Wojciech Gradkowski
* All code after tags/original: Copyright (c) 2015, DiffPlug
*/
package com.jmatio.io;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import com.jmatio.common.MatDataTypes;
/**
* MAT-file header
*
* Level 5 MAT-files begin with a 128-byte header made up of a 124 byte text field
* and two, 16-bit flag fields
*
* @author Wojciech Gradkowski ([email protected])
*/
public class MatFileHeader {
private static final String DEFAULT_DESCRIPTIVE_TEXT = "MATLAB 5.0 MAT-file, Platform: "
+ System.getProperty("os.name")
+ ", CREATED on: ";
public static final int DEFAULT_VERSION = 0x0100;
private int version;
private String description;
private byte[] endianIndicator;
/**
* New MAT-file header
*
* @param description - descriptive text (no longer than 116 characters)
* @param version - by default is set to 0x0100
* @param endianIndicator - byte array size of 2 indicating byte-swapping requirement
*/
public MatFileHeader(String description, int version, byte[] endianIndicator) {
this.description = description;
this.version = version;
this.endianIndicator = new byte[endianIndicator.length];
System.arraycopy(endianIndicator, 0, this.endianIndicator, 0, endianIndicator.length);
}
/**
* Gets descriptive text
*
* @return
*/
public String getDescription() {
return description;
}
/**
* Gets endian indicator. Bytes written as "MI" suggest that byte-swapping operation is required
* in order to interpret data correctly. If value is set to "IM" byte-swapping is not needed.
*
* @return - a byte array size of 2
*/
byte[] getEndianIndicator() {
return endianIndicator;
}
/**
* When creating a MAT-file, set version to 0x0100
*
* @return
*/
public int getVersion() {
return version;
}
//@facotry
/**
* A factory. Creates new MatFileHeader
instance with default header values:
*
* - MAT-file is 5.0 version
* - version is set to 0x0100
* - no byte-swapping ("IM")
*
*
* @return - new MatFileHeader
instance
*/
public static MatFileHeader createHeader() {
return new MatFileHeader(DEFAULT_DESCRIPTIVE_TEXT + (new Date()).toString(),
DEFAULT_VERSION,
DEFAULT_ENDIAN_INDICATOR());
}
/** Returns the default endianness indicator. */
private static final byte[] DEFAULT_ENDIAN_INDICATOR() {
return new byte[]{(byte) 'M', (byte) 'I'};
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
try {
StringBuffer sb = new StringBuffer();
sb.append("[");
sb.append("desriptive text: " + description);
sb.append(", version: " + version);
sb.append(", endianIndicator: " + new String(endianIndicator, MatDataTypes.CHARSET));
sb.append("]");
return sb.toString();
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
}
}