B4J Library Hutool library

Hutool is a small but comprehensive library of Java tools, achieved by encapsulation through static methods, reduce the cost of learning related APIs, increase productivity, and make Java as elegant as a functional programming language,let the Java be "sweet" too.

Hutool tools and methods from each user's crafted, it covers all aspects of the underlying code of Java development, it is a powerful tool for large project development to solve small problems, but also the efficiency of small projects;

Hutool is a project "util" package friendly alternative, it saves developers on the project of common classes and common tool methods of encapsulation time, so that development focus on business, at the same time can minimize the encapsulation is not perfect to avoid the bugs.

Source: https://github.com/tummosoft/B4jHuTool/tree/main
 

tummosoft

Active Member
Licensed User
Longtime User
* System parameter tools (JVM information, etc.)
- Java library: https://www.mediafire.com/file/gbyquhzsfmkobs7/HuToolCore.zip/file

SystemHelper:
    Dim host_info As HostInfo
    Log(host_info.Address)
    Log(host_info.Name)
    ' --------------------------------------
    Dim java_info As JavaInfo
    Log(java_info.isJava11)
    Log(java_info.VendorURL)
   
    Dim jr As JavaRuntimeInfo
    Log(jr.Name)
    Log(jr.HomeDir)
    Log(jr.LibraryPath)
   
    Dim jvr As JavaSpecInfo
    Log(jvr.Name)
   
    Dim jvm As JvmInfo
    Log(jvm.Name)
   
    Dim jvm_spec As JvmSpecInfo
    Log(jvm_spec.Name)
   
    Dim os As OsInfo
    Log(os.isWindows10)
   
    Dim rt_info As RuntimeInfo
    Log(rt_info.MaxMemory)
   
    Dim sys_util As SystemUtil
   
   
    Dim user_info As UserInfo
    Log(user_info.CurrentDir)
 

Attachments

  • SystemHelper.zip
    20.3 KB · Views: 16
  • Test_Systemhelper.zip
    2.4 KB · Views: 17
Last edited:

tummosoft

Active Member
Licensed User
Longtime User
* SettingHelper: Stronger Setting Profile tools and Properties tools
- Extend java library: https://www.mediafire.com/file/xqx7hqcojhgzozt/java_libs_settinghelper.zip/file

SettingHelper:
#AdditionalJar: hutool-core-5.8.43.jar
#AdditionalJar: hutool-log-5.8.43.jar
#AdditionalJar: hutool-setting-5.8.43.jar

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Dim setting As SettingHelper
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
   
    If File.Exists(File.DirApp, "setting.ini") = False Then
        File.WriteString(File.DirApp, "setting.ini", "")
    End If

    Dim setting_file As String = File.Combine(File.DirApp, "setting.ini")
    setting.Initialize2(setting_file, "UTF-8", True)
   
    setting.Set("server", "localhost")
       
    setting.setByGroup("user", "account", "admin")
    setting.setByGroup("password", "account", "123456")
    setting.setByGroup("facebook", "social", "https://facebook.com/admin")
    setting.setByGroup("twitter", "social", "https://x.com/admin")
   
    setting.Store
End Sub

Sub Button1_Click
    Dim server As Boolean = setting.containsKey("server")
    Log(server)
    Dim user As Boolean = setting.containsKey2("account", "user")
    Log(user)

setting.ini:
[]
server = localhost
[account]
user = admin
password = 123456
[social]
facebook = https://facebook.com/admin
twitter = https://x.com/admin
 

Attachments

  • SettingHelper.zip
    35.8 KB · Views: 9
  • B4j test_settinghelper.zip
    2.5 KB · Views: 10

tummosoft

Active Member
Licensed User
Longtime User
JWTHelper: JSON Web Token (JWT) implement
- Extend Java library: https://www.mediafire.com/file/crib8ws7y88n30s/hutool-all-5.8.43.jar/file

B4X:
#AdditionalJar: hutool-all-5.8.43.jar

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Dim SECRET As String = "my-secret-key-123456"
    Private Button2 As Button
    Dim jwt As JWTHelper
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    
    jwt.Initialize
End Sub

Sub Button1_Click
    

    
    Dim payload As Map
    payload.Initialize
    
    payload.put("userId", 1001)
    payload.put("username", "admin")
    payload.put("role", "ADMIN")

    Dim expireTime As Long = DateTime.Now + 3600000
    payload.put("exp", expireTime)
    
    Dim token As String = jwt.createToken(payload, SECRET)
    
    Log(token)
End Sub

Private Sub Button2_Click
    Dim token As String = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiQURNSU4iLCJleHAiOjE3NzA3NzM5MTg3NTksInVzZXJJZCI6MTAwMSwidXNlcm5hbWUiOiJhZG1pbiJ9.eCV45D9F_-oVoiJJBcL0VPSCuj_gTL32QnGHZMGQ-_k"
    Dim vr As Boolean = jwt.verify(token, SECRET)
    Log(vr)
    
    Dim userId As Object = jwt.GetPayload(token, "userId")
    Log("userId = " & userId)
    
    Dim username As Object = jwt.GetPayload(token, "username")
    Log("username = " & username)
End Sub
 

Attachments

  • JWTHelper.zip
    2.3 KB · Views: 8
  • Test_jwtHelper.zip
    2.7 KB · Views: 8

tummosoft

Active Member
Licensed User
Longtime User
* CacheHelper: A simple cache library
- Extend Java library: https://www.mediafire.com/file/crib8ws7y88n30s/hutool-all-5.8.43.jar/file

1. FIFO Cache (First In First Out Cache)
FIFO (First In First Out) cache follows the order in which elements are inserted. When the cache is full, the earliest inserted element is removed first.

fifoCache:
Dim fifoCache As CacheHelper

fifoCache.InitFIFOCache(3)
fifoCache.put("key1", "value1")
fifoCache.put("key2", "value2")
fifoCache.put("key3", "value3")
    
Dim k1 As String = fifoCache.get("key1")
Log("key 1 = " & k1)

2. LRU (Least Recently Used) cache

The LRU (Least Recently Used) cache prioritizes removing the least recently used elements. When the cache is full, the least recently accessed element will be removed.

lruCache:
Dim lruCache As CacheHelper
lruCache.InitLRUCache(3)
lruCache.put("key1", "value1")
lruCache.put("key2", "value2")
lruCache.put("key3", "value3")
    
lruCache.get("key1")
    
lruCache.put("key4", "value4")

3. Timed caching

Timed caching sets an expiration time for each element, and automatically removes the element from the cache once it expires.

timedCache:
Dim timedCache As CacheHelper
timedCache.InitTimedCache(2000)

timedCache.put("key1", "value1")
    
Dim value As String = timedCache.get("key1")
Log("timedCache [key1]: " & value)

Sleep(3000)
    
value = timedCache.get("key1")
Log("timedCache - [key1]: " & value)
 

Attachments

  • CacheHelper.zip
    1.8 KB · Views: 8
  • Test_cachedHelper.zip
    2.5 KB · Views: 7

tummosoft

Active Member
Licensed User
Longtime User
* EncryptionHelper (hutool-crypto) is based on the native JDK JCE implementation and supports all standard algorithms (such as AES, RSA, MD5, etc.).
- Extend Java library: https://www.mediafire.com/file/crib8ws7y88n30s/hutool-all-5.8.43.jar/file

B4X:
#AdditionalJar: hutool-all-5.8.43.jar

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
    Dim Encypton As EncryptionHelper

End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    
    EncyptonWithNull
    
    'EncyptonWithOnlyPublicKey
End Sub

Sub EncyptonWithNull
    Encypton.InitRSA(Null, Null)
    
    Log(Encypton.GetPrivateKeyBase64)
    Log(Encypton.GetPublicKeyBase64)
End Sub

Sub EncyptonWithPublicAndPrivateKey
    Dim privateKey As String = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAPcTfAoqXadHi3FYK1vuRXaOS2f968no29VH5XNUi6Gn7qohNDY9vqpImSV/h42Mmv7qIvu8SxgXQd/TLnP9q45Fbw0emuOv505Bp3cG9t2CLqJPfh8P+jJTYWUsk3nFiqRQszjXoAswPy+MlRXJ9e1Fz9ClgDzGWRLyKorltYtnAgMBAAECgYAA9KjWqzpIgtzVt+cXCHFpV3yQLunCEq4DAa+wBgQyQGGKc+1tRkjeBMDe5cLi98fDs6kl+yD5O2HMWVdFKJaIyObg++jYqFJjniABVG6yU6b0TfONqmGuhQLJAVlnTy0Zlm4x0PUQ6e360QJPG7Danl8HKruyc99jEXDS0qQTnQJBAPolfxW5XIu7AzG685/vYgcAPzuAzZKUQyfH56WmBYEaKZIw0vLH+WdZXA1Nb4i/3tPdnbQYNwrUtXwhuX26GlsCQQD825hY1NY+4yOpzcITsF9bl3TYjRsF4j9ChMWYsTUSCbZLmuP5KP0WQnwnzNU2pVVpuBfq+sUH7HDSi9V7RWjlAkEAvZaDvmxRxxnDbwKZuUMOke3O/wM9S3YRe+oYGMU+8L1qeoGEsDt+PKVOjkp9WTDK6trRtviPZycWMfHBi5fsdwJAV9JFRYUWrFzjhBRstb3qQLuH37aSjiaYZhgLMQ9LKWqqH/Q5/c3YqSSKTqB75TDjS4ae+AQE7s2WGeprSRdAaQJAGKcYPXO4Mbj9JPCacV2NBv4DyjbzbWV3ZgclBL+wd+2MJDu2sAd0i2oqMAsIR9OZ2og7jAh/k/Qrmw3EqyoUdw=="
    Dim publickey As String = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQD3E3wKKl2nR4txWCtb7kV2jktn/evJ6NvVR+VzVIuhp+6qITQ2Pb6qSJklf4eNjJr+6iL7vEsYF0Hf0y5z/auORW8NHprjr+dOQad3Bvbdgi6iT34fD/oyU2FlLJN5xYqkULM416ALMD8vjJUVyfXtRc/QpYA8xlkS8iqK5bWLZwIDAQAB"
    ' First
    Encypton.InitRSA(privateKey, publickey)
    
    Log(Encypton.GetPrivateKeyBase64)
    Log(Encypton.GetPublicKeyBase64)
    
    Dim entext As String = Encypton.Encrypt("Hello world")
    Log(entext)
    Dim detext As String = Encypton.Decrypt(entext)
    Log(detext)
End Sub

Sub EncyptonWithOnlyPublicKey
    Dim privateKey As String = ""
    Dim publickey As String = ""
    ' First
    Encypton.InitRSA(Null, publickey)
    
    Dim entext As String = Encypton.Encrypt("Hello world")
    Log(entext)
    Dim detext As String = Encypton.Decrypt(entext)
    Log(detext)
End Sub
 

Attachments

  • Test_EncryptionHelper.zip
    3.3 KB · Views: 3
  • EncryptionHelper.zip
    3 KB · Views: 3
Top