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

📄 utils.java

📁 這是一個油Java實作的資料庫系統 是個入門的好材料
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        daten[2] = hex2byte( chars, 2 );
        daten[1] = hex2byte( chars, 4 );
        daten[0] = hex2byte( chars, 6 );

        daten[5] = hex2byte( chars, 9 );
        daten[4] = hex2byte( chars, 11 );

        daten[7] = hex2byte( chars, 14 );
        daten[6] = hex2byte( chars, 16 );

        daten[8] = hex2byte( chars, 19 );
        daten[9] = hex2byte( chars, 21 );

        daten[10] = hex2byte( chars, 24 );
        daten[11] = hex2byte( chars, 26 );
        daten[12] = hex2byte( chars, 28 );
        daten[13] = hex2byte( chars, 30 );
        daten[14] = hex2byte( chars, 32 );
        daten[15] = hex2byte( chars, 34 );
        return daten;
    }

    private static byte hex2byte( char[] hex, int offset) throws SQLException{
        try{
                return (byte)((hexDigit2int( hex[ offset++ ] ) << 4)
                                | hexDigit2int( hex[ offset++ ] ));
        }catch(Exception e){
             throw SmallSQLException.create(Language.SEQUENCE_HEX_INVALID_STR, new Object[] { new Integer(offset), new String(hex) });
        }
    }

    static String bytes2unique( byte[] daten, int offset ){
    	if(daten.length-offset < 16){
    		byte[] temp = new byte[16];
    		System.arraycopy(daten, offset, temp, 0, daten.length-offset);
    		daten = temp;
    	}
        char[] chars = new char[36];
        chars[8] = chars[13] = chars[18] = chars[23] = '-';

        chars[0] = digits[ (daten[offset+3] >> 4) & 0x0F ];
        chars[1] = digits[ (daten[offset+3]     ) & 0x0F ];
        chars[2] = digits[ (daten[offset+2] >> 4) & 0x0F ];
        chars[3] = digits[ (daten[offset+2]     ) & 0x0F ];
        chars[4] = digits[ (daten[offset+1] >> 4) & 0x0F ];
        chars[5] = digits[ (daten[offset+1]     ) & 0x0F ];
        chars[6] = digits[ (daten[offset+0] >> 4) & 0x0F ];
        chars[7] = digits[ (daten[offset+0]     ) & 0x0F ];

        chars[ 9] = digits[ (daten[offset+5] >> 4) & 0x0F ];
        chars[10] = digits[ (daten[offset+5]     ) & 0x0F ];
        chars[11] = digits[ (daten[offset+4] >> 4) & 0x0F ];
        chars[12] = digits[ (daten[offset+4]     ) & 0x0F ];

        chars[14] = digits[ (daten[offset+7] >> 4) & 0x0F ];
        chars[15] = digits[ (daten[offset+7]     ) & 0x0F ];
        chars[16] = digits[ (daten[offset+6] >> 4) & 0x0F ];
        chars[17] = digits[ (daten[offset+6]     ) & 0x0F ];

        chars[19] = digits[ (daten[offset+8] >> 4) & 0x0F ];
        chars[20] = digits[ (daten[offset+8]     ) & 0x0F ];
        chars[21] = digits[ (daten[offset+9] >> 4) & 0x0F ];
        chars[22] = digits[ (daten[offset+9]     ) & 0x0F ];

        chars[24] = digits[ (daten[offset+10] >> 4) & 0x0F ];
        chars[25] = digits[ (daten[offset+10]     ) & 0x0F ];
        chars[26] = digits[ (daten[offset+11] >> 4) & 0x0F ];
        chars[27] = digits[ (daten[offset+11]     ) & 0x0F ];
        chars[28] = digits[ (daten[offset+12] >> 4) & 0x0F ];
        chars[29] = digits[ (daten[offset+12]     ) & 0x0F ];
        chars[30] = digits[ (daten[offset+13] >> 4) & 0x0F ];
        chars[31] = digits[ (daten[offset+13]     ) & 0x0F ];
        chars[32] = digits[ (daten[offset+14] >> 4) & 0x0F ];
        chars[33] = digits[ (daten[offset+14]     ) & 0x0F ];
        chars[34] = digits[ (daten[offset+15] >> 4) & 0x0F ];
        chars[35] = digits[ (daten[offset+15]     ) & 0x0F ];
        return new String(chars);
    }

    static boolean string2boolean( String val){
        try{
            return Double.parseDouble( val ) != 0;
        }catch(NumberFormatException e){/*ignore it if it not a number*/}
        return "true".equalsIgnoreCase( val ) || "yes".equalsIgnoreCase( val ) || "t".equalsIgnoreCase( val );
    }
	
	
	static long doubleToMoney(double value){
		if(value < 0)
			return (long)(value * 10000 - 0.5);
		return (long)(value * 10000 + 0.5);
	}

    static int indexOf( char value, char[] str, int offset, int length ){
        value |= 0x20;
        for(int end = offset+length;offset < end; offset++){
            if((str[offset] | 0x20) == value) return offset;
        }
        return -1;
    }

    static int indexOf( int value, int[] list ){
        int offset = 0;
        for(int end = list.length; offset < end; offset++){
            if((list[offset]) == value) return offset;
        }
        return -1;
    }

    static int indexOf( byte[] value, byte[] list, int offset ){
        int length = value.length;
        loop1:
        for(int end = list.length-length; offset <= end; offset++){
            for(int i=0; i<length; i++ ){
                if(list[offset+i] != value[i]){
                    continue loop1;
                }
            }
            return offset;
        }
        return -1;
    }

    static int compareBytes( byte[] leftBytes, byte[] rightBytes){
        int length = Math.min( leftBytes.length, rightBytes.length );
        int comp = 0;
        for(int i=0; i<length; i++){
            if(leftBytes[i] != rightBytes[i]){
                comp = leftBytes[i] < rightBytes[i] ? -1 : 1;
                break;
            }
        }
        if(comp == 0 && leftBytes.length != rightBytes.length){
            comp = leftBytes.length < rightBytes.length ? -1 : 1;
        }
        return comp;
    }
	
    
    /**
     * 
     * @param colNames
     * @param data
     * @return
     * @throws SQLException
     */
    static CommandSelect createMemoryCommandSelect( SSConnection con, String[] colNames, Object[][] data) throws SQLException{
		MemoryResult source = new MemoryResult(data, colNames.length);
		CommandSelect cmd = new CommandSelect(con.log);
		for(int i=0; i<colNames.length; i++){
			ExpressionName expr = new ExpressionName(colNames[i]);
			cmd.addColumnExpression( expr );
			expr.setFrom( source, i, source.getColumn(i));
		}
		cmd.setSource(source);
		return cmd;
    }
	

	/**
     *  recycle Integer objects, this is faster as to garbage the objects
	 */
	static final Integer getInteger(int value){
		if(value >= -4 && value < 256){
			return integerCache[ value+4 ];		
		}else
			return new Integer(value);
	}
	
	/**
     * recycle Integer objects, this is faster as to garbage the objects
	 */
	static final Short getShort(int value){
		if(value >= -4 && value < 256){
			return shortCache[ value+4 ];		
		}else
			return new Short((short)value);
	}
    
    
    /**
     * Open a RandomAccessFile and lock it that no other thread or VM can open it..
     * 
     * @param file
     *            The file that should be open.
     * @return a RandomAccessFile
     * @throws FileNotFoundException
     *             If the file can not open
     * @throws SQLException
     *             If the file can't lock.
     */
    static final RandomAccessFile openRaFile(File file) throws FileNotFoundException, SQLException{
        RandomAccessFile raFile = new RandomAccessFile(file, "rw");
        try{
            FileLock lock = raFile.getChannel().tryLock();
            if(lock == null){
                throw SmallSQLException.create(Language.CANT_LOCK_FILE, file);
            }
        }catch(SQLException sqlex){
            throw sqlex;
        }catch(Throwable th){
            throw SmallSQLException.createFromException(Language.CANT_LOCK_FILE, file, th);
        }
        return raFile;
    }
    
    
    /**
     * Get all the ExpressionName objects that are part of the tree.
     * If it only a constant expression then a empty list is return.
     * @param tree the expression to scan
     * @return the list of ExpressionName instances
     */
    static final Expressions getExpressionNameFromTree(Expression tree){
        Expressions list = new Expressions();
        getExpressionNameFromTree( list, tree );
        return list;
    }
    
    /**
     * Scan the tree recursively.
     */
    private static final void getExpressionNameFromTree(Expressions list, Expression tree){
        if(tree.getType() == Expression.NAME ){
            list.add(tree);
        }
        Expression[] params = tree.getParams();
        if(params != null){
            for(int i=0; i<params.length; i++){
                getExpressionNameFromTree( list, tree );
            }
        }
    }

    final static char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
}

⌨️ 快捷键说明

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