forked from tiny-craft/tiny-rdm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70c38d9
commit 0739cb8
Showing
6 changed files
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package convutil | ||
|
||
import ( | ||
"bytes" | ||
"github.com/pierrec/lz4/v4" | ||
"io" | ||
) | ||
|
||
type LZ4Convert struct{} | ||
|
||
func (LZ4Convert) Enable() bool { | ||
return true | ||
} | ||
|
||
func (LZ4Convert) Encode(str string) (string, bool) { | ||
var compress = func(b []byte) (string, error) { | ||
var buf bytes.Buffer | ||
writer := lz4.NewWriter(&buf) | ||
if _, err := writer.Write([]byte(str)); err != nil { | ||
writer.Close() | ||
return "", err | ||
} | ||
writer.Close() | ||
return string(buf.Bytes()), nil | ||
} | ||
|
||
if gzipStr, err := compress([]byte(str)); err == nil { | ||
return gzipStr, true | ||
} | ||
return str, false | ||
} | ||
|
||
func (LZ4Convert) Decode(str string) (string, bool) { | ||
reader := lz4.NewReader(bytes.NewReader([]byte(str))) | ||
if decompressed, err := io.ReadAll(reader); err == nil { | ||
return string(decompressed), true | ||
} | ||
return str, false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters