1 /* 2 * Hunt - A redis client library for D programming language. 3 * 4 * Copyright (C) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.redis.util.SafeEncoder; 13 14 import hunt.redis.Exceptions; 15 import hunt.redis.Protocol; 16 17 import hunt.Exceptions; 18 import hunt.text.StringUtils; 19 20 import std.array; 21 import std.conv; 22 23 /** 24 * The only reason to have this is to be able to compatible with java 1.5 :( 25 */ 26 class SafeEncoder { 27 28 private this(){ 29 throw new InstantiationError( "Must not instantiate this class" ); 30 } 31 32 static const(ubyte)[][] encodeMany(string[] strs...) { 33 const(ubyte)[][] many = new const(ubyte)[][strs.length]; 34 for (size_t i = 0; i < strs.length; i++) { 35 many[i] = encode(strs[i]); 36 } 37 return many; 38 } 39 // static string[] encodeMany(string[] strs...) { 40 // return strs; 41 // // byte[][] many = new byte[][strs.length]; 42 // // for (size_t i = 0; i < strs.length; i++) { 43 // // many[i] = encode(strs[i]); 44 // // } 45 // // return many; 46 // } 47 48 // static const(ubyte)[] encode(string str) { 49 // return cast(const(ubyte)[])str; 50 // } 51 52 static const(ubyte)[] encode(string str) { 53 try { 54 if (str.empty) { 55 throw new RedisDataException("The value sent to redis cannot be null"); 56 } 57 return cast(const(ubyte)[])(StringUtils.getBytes(str, Protocol.CHARSET)); 58 } catch (UnsupportedEncodingException e) { 59 throw new RedisException(e); 60 } 61 } 62 63 static string encode(const(ubyte)[] data) { 64 try { 65 return cast(string)data.idup; 66 } catch (UnsupportedEncodingException e) { 67 throw new RedisException(e); 68 } 69 } 70 }