📄 persistenceelement.java
字号:
//===========================================================================//=-------------------------------------------------------------------------=//= Module history: =//= - December 8 2006 - Oscar Chavarro: Original base version =//===========================================================================package vsdk.toolkit.io;import java.io.File;import java.io.InputStream;import java.io.OutputStream;import java.util.StringTokenizer;/**DEFINITION: A `PersistenceElement` in VitralSDK is a software element withalgorithms and data structures (i.e. a class) with the specific functionalityof providing persistence operations for a data Entity.The PersistenceElement abstract class provides an interface for *Persistencestyle classes. This serves three purposes: - To help in design level organization of persistence classes (this eases the study of the class hierarchy) - To provide a place to locate possible future operations, common to all persistence classes and persistence private utility/supporting classes. In particular, this class contains basic low level persistence operations for converting bit streams from and to basic numeric data types. Note that this code is NOT portable, as it needs explicit programmer configuration for little-endian or big-endian hardware platform. - To provide means of accessing some operating system's native library files and other basic file system management.*/public abstract class PersistenceElement { private static final boolean bigEndianArchitecture = false; private static byte[] bytesForInt = new byte[2]; private static byte[] bytesForLong = new byte[4]; private static byte[] bytesForFloat = new byte[4]; public static boolean checkDirectory(String dirName) { File dirFd = new File(dirName); if ( dirFd.exists() && (!dirFd.isDirectory() ) ) { System.err.println("Directory " + dirName + " can not be created, because a file with that name already exists (not overwriten)."); return false; } if ( !dirFd.exists() && !dirFd.mkdir() ) { System.err.println("Directory " + dirName + " can not be created, check permisions and available free disk space."); return false; } return true; } /** Given a filename, this method extract its extension and return it. @todo: This method will fail when directory path or filename contains more than one dot. Needs to be fixed. */ protected static String extractExtensionFromFile(File fd) { String filename = fd.getName(); StringTokenizer st = new StringTokenizer(filename, "."); int numTokens = st.countTokens(); for( int i = 0; i < numTokens - 1; i++ ) { st.nextToken(); } String ext = st.nextToken(); return ext; } /** Given a previously initialized array of bytes, this method fills it with information readed from the given input stream. If it is not enough information to read, this method generates an Exception. */ public static void readBytes(InputStream is, byte[] bytesBuffer) throws Exception { int offset = 0; int numRead = 0; int length = bytesBuffer.length; do { numRead = is.read(bytesBuffer, offset, (length-offset)); offset += numRead; } while( offset < length && numRead >= 0 ); } /** Given a previously initialized array of bytes, this method writes it with information readed from the given output stream. If it is not enough information to read, this method generates an Exception. */ public static void writeBytes(OutputStream os, byte[] bytesBuffer) throws Exception { os.write(bytesBuffer, 0, bytesBuffer.length); } private static void int2byteArrayDirect(byte[] arr, int start, int num) { int i = 0; int len = 2; byte[] tmp = new byte[len]; // Convert number to array for ( i = 0; i < len; i++ ) { tmp[i] = (byte)((num & (0xFF << 8*i)) >> 8*i); } // Export subarray to end array int cnt; for ( i = start, cnt = 0; i < (start + len); i++, cnt++ ) { arr[i] = tmp[cnt]; } } private static void int2byteArrayInvert(byte[] arr, int start, int num) { int i = 0; int len = 2; byte[] tmp = new byte[len]; // Convert number to array for ( i = 0; i < len; i++ ) { tmp[len-i-1] = (byte)((num & (0xFF << 8*i)) >> 8*i); } // Export subarray to end array int cnt; for ( i = start, cnt = 0; i < (start + len); i++, cnt++ ) { arr[i] = tmp[cnt]; } } private static int byteArray2intDirect(byte[] arr, int start) { int low = arr[start] & 0xff; int high = arr[start+1] & 0xff; return ( high << 8 | low ); } private static int byteArray2intInvert(byte[] arr, int start) { int low = arr[start] & 0xff; int high = arr[start+1] & 0xff; return ( low << 8 | high ); } private static long byteArray2longDirect(byte[] arr, int start) { int i = 0; int len = 4; int cnt = 0; byte[] tmp = new byte[len]; for ( i = start; i < (start + len); i++ ) { tmp[cnt] = arr[i]; cnt++; } long accum = 0; i = 0; for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 ) { accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy; i++; } return accum; } private static void long2byteArrayDirect( byte[] arr, int start, long num) { int i = 0; int len = 4; byte[] tmp = new byte[len]; // Convert number to array for ( i = 0; i < len; i++ ) { tmp[i] = (byte)((num & (0xFF << 8*i)) >> 8*i); } // Export subarray to end array int cnt; for ( i = start, cnt = 0; i < (start + len); i++, cnt++ ) { arr[i] = tmp[cnt]; } } private static void long2byteArrayInvert( byte[] arr, int start, long num) { int i = 0; int len = 4; byte[] tmp = new byte[len]; // Convert number to array for ( i = 0; i < len; i++ ) { tmp[len-i-1] = (byte)((num & (0xFF << 8*i)) >> 8*i); } // Export subarray to end array int cnt; for ( i = start, cnt = 0; i < (start + len); i++, cnt++ ) { arr[i] = tmp[cnt]; } } private static long byteArray2longInvert(byte[] arr, int start) { int i = 0; int len = 4; int cnt = 3; byte[] tmp = new byte[len]; for ( i = start; i < (start + len); i++ ) { tmp[cnt] = arr[i]; cnt--; } long accum = 0; i = 0; for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 ) { accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy; i++; } return accum; } private static float byteArray2floatDirect(byte[] arr, int start) { int i = 0; int len = 4; int cnt; byte[] tmp = new byte[len]; for ( i = start, cnt = 0; i < (start + len); i++, cnt++ ) { tmp[cnt] = arr[i]; } int accum = 0; i = 0; for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 ) { accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy; i++; } return Float.intBitsToFloat(accum); } private static float byteArray2floatInvert(byte[] arr, int start) { int i = 0; int len = 4; int cnt = 3; byte[] tmp = new byte[len]; for ( i = start; i < (start + len); i++ ) { tmp[cnt] = arr[i]; cnt--; } int accum = 0; i = 0; for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 ) { accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy; i++; } return Float.intBitsToFloat(accum); } /** This method is responsible of taking into account the endianess of the original data */ public static int byteArray2intBE(byte[] arr, int start) { if ( bigEndianArchitecture ) { return byteArray2intDirect(arr, start); } return byteArray2intInvert(arr, start); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -