⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utils.java

📁 這是一個油Java實作的資料庫系統 是個入門的好材料
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* =============================================================
 * SmallSQL : a free Java DBMS library for the Java(tm) platform
 * =============================================================
 *
 * (C) Copyright 2004-2007, by Volker Berlin.
 *
 * Project Info:  http://www.smallsql.de/
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ---------------
 * Utils.java
 * ---------------
 * Author: Volker Berlin
 * 
 */
package smallsql.database;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.RandomAccessFile;
import java.nio.channels.FileLock;
import java.sql.SQLException;
import smallsql.database.language.Language;

class Utils {

	static final String MASTER_FILENAME = "smallsql.master";
	static final String TABLE_VIEW_EXTENTION = ".sdb";
	private static final String LOB_EXTENTION = ".lob";
	static final String IDX_EXTENTION = ".idx";
	private static final Integer[] integerCache = new Integer[260];
	private static final Short[]   shortCache   = new Short[260];
	
	static{
		for(int i=-4; i<256; i++){
			integerCache[ i+4 ] = new Integer(i);
			shortCache  [ i+4 ] = new Short((short)i);
		}
	}
    
    static String createTableViewFileName(Database database, String name){
        return database.getName() + '/' + name + TABLE_VIEW_EXTENTION;
    }

	static String createLobFileName(Database database, String name){
		return database.getName() + '/' + name + LOB_EXTENTION;
	}

	static String createIdxFileName(Database database, String name){
		return database.getName() + '/' + name + IDX_EXTENTION;
	}

	static boolean like(String value, String pattern){
		if(value == null || pattern == null) return false;
		if(pattern.length() == 0) return true;

		int mIdx = 0;//index in mask Array
		int sIdx = 0;//index in search Array
		boolean range = false;
		weiter:
		while(pattern.length() > mIdx && value.length() > sIdx) {
			char m = Character.toUpperCase(pattern.charAt(mIdx++));
			switch(m) {
				case '%':
					range = true;
					break;
				case '_':
					sIdx++;
					break;
				default:
					if(range) {//% wildcard is active
						for(; sIdx < value.length(); sIdx++) {
							if(Character.toUpperCase(value.charAt(sIdx)) == m) break;//Counter mustn't increment before break
						}
						if(sIdx >= value.length()) return false;
						int lastmIdx = mIdx - 1;
						sIdx++;
						while(pattern.length() > mIdx && value.length() > sIdx) {
							m = Character.toUpperCase(pattern.charAt(mIdx++));
							if(Character.toUpperCase(value.charAt(sIdx)) != m) {
								if(m == '%' || m == '_') {
									mIdx--;
									break;
								}
								mIdx = lastmIdx;
								continue weiter;
							}
							sIdx++;
						}
						range = false;
					}else{
						if(Character.toUpperCase(value.charAt(sIdx)) != m) return false;
						sIdx++;
					}
					break;
			}
		}
		while(pattern.length() > mIdx) {
            //Search mask is not too ends yet it may only '%' be contained 
			if(Character.toUpperCase(pattern.charAt(mIdx++)) != '%') return false;
		}
		while(value.length() > sIdx && !range) return false;
		return true;
	}
	
	
	static int long2int(long value){
		if(value > Integer.MAX_VALUE)
			return Integer.MAX_VALUE;
		if(value < Integer.MIN_VALUE)
			return Integer.MIN_VALUE;
		return (int)value;
	}
	
	static long double2long(double value){
		if(value > Long.MAX_VALUE)
			return Long.MAX_VALUE;
		if(value < Long.MIN_VALUE)
			return Long.MIN_VALUE;
		return (long)value;
	}



    static float bytes2float( byte[] bytes ){
        return Float.intBitsToFloat( bytes2int( bytes ) );
    }

    static double bytes2double( byte[] bytes ){
        return Double.longBitsToDouble( bytes2long( bytes ) );
    }

    static long bytes2long( byte[] bytes ){
        long result = 0;
        int length = Math.min( 8, bytes.length);
        for(int i=0; i<length; i++){
            result = (result << 8) | (bytes[i] & 0xFF);
        }
        return result;
    }

    static int bytes2int( byte[] bytes ){
        int result = 0;
        int length = Math.min( 4, bytes.length);
        for(int i=0; i<length; i++){
            result = (result << 8) | (bytes[i] & 0xFF);
        }
        return result;
    }

    static byte[] double2bytes( double value ){
        return long2bytes(Double.doubleToLongBits(value));
    }

    static byte[] float2bytes( float value ){
        return int2bytes(Float.floatToIntBits(value));
    }

    static byte[] long2bytes( long value ){
        byte[] result = new byte[8];
        result[0] = (byte)(value >> 56);
        result[1] = (byte)(value >> 48);
        result[2] = (byte)(value >> 40);
        result[3] = (byte)(value >> 32);
        result[4] = (byte)(value >> 24);
        result[5] = (byte)(value >> 16);
        result[6] = (byte)(value >> 8);
        result[7] = (byte)(value);
        return result;
    }
    
    static int money2int( long value ) {
		if (value < Integer.MIN_VALUE) return Integer.MIN_VALUE;
		else if (value > Integer.MAX_VALUE) return Integer.MAX_VALUE;
		else return (int) value;
	}

	static byte[] int2bytes( int value ){
		byte[] result = new byte[4];
		result[0] = (byte)(value >> 24);
		result[1] = (byte)(value >> 16);
		result[2] = (byte)(value >> 8);
		result[3] = (byte)(value);
		return result;
	}

    static String bytes2hex( byte[] bytes ){
        StringBuffer buf = new StringBuffer(bytes.length << 1);
        for(int i=0; i<bytes.length; i++){
            buf.append( digits[ (bytes[i] >> 4) & 0x0F ] );
            buf.append( digits[ (bytes[i]     ) & 0x0F ] );
        }
        return buf.toString();
    }

    static byte[] hex2bytes( char[] hex, int offset, int length) throws SQLException{
        try{
            byte[] bytes = new byte[length / 2];
            for(int i=0; i<bytes.length; i++){
                bytes[i] = (byte)((hexDigit2int( hex[ offset++ ] ) << 4)
                                | hexDigit2int( hex[ offset++ ] ));
            }
            return bytes;
        }catch(Exception e){
             throw SmallSQLException.create(Language.SEQUENCE_HEX_INVALID, String.valueOf(offset)); /*, offset*/
        }
    }

    private static int hexDigit2int(char digit){
        if(digit >= '0' && digit <= '9') return digit - '0';
        digit |= 0x20;
        if(digit >= 'a' && digit <= 'f') return digit - 'W'; // -'W'  ==  -'a' + 10
        throw new RuntimeException();
    }

    static byte[] unique2bytes( String unique ) throws SQLException{
        char[] chars = unique.toCharArray();
        byte[] daten = new byte[16];
        daten[3] = hex2byte( chars, 0 );

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -