Code Monkey home page Code Monkey logo

byte-transforms's Introduction

Clojars Project cljdoc badge CircleCI

This library helps you hash, compress, and encode streams of bytes. It contains the methods in the standard Java lib, as well as a curated collection of the best available methods.

Usage

Leiningen

[org.clj-commons/byte-transforms "0.2.1"]

deps.edn

org.clj-commons/byte-transforms {:mvn/version "0.2.1"}

All functions are in the byte-transforms namespace. There are five primary functions, hash, compress, decompress, encode, and decode. Each takes three arguments: the bytes, the method, and an (optional) options map. The bytes can be anything which is part of the byte-stream conversion graph.

byte-transforms> (hash "hello" :murmur64)
2191231550387646743

byte-transforms> (compress "hello" :snappy)
#<byte[] [B@7e0f980b>
byte-transforms> (byte-streams/to-string (decompress *1 :snappy))
"hello"

byte-transforms> (byte-streams/to-string (encode "hello" :base64 {:url-safe? false}))
"aGVsbG8"
byte-transforms> (byte-streams/to-string (decode *1 :base64))
"hello"

Note that Base64 encoding defaults to the URL-safe variant, which means that the output will not be padded. This can be disabled by passing in {:url-safe? false} to encode.

Available methods can be found via available-hash-functions, available-compressors, and available-encoders:

byte-transforms> (available-hash-functions)
(:sha384 :md2 :crc32 :crc64 :sha512 :sha1 :murmur32 :murmur128 :adler32 :sha256 :md5 :murmur64)

byte-transforms> (available-compressors)
(:lz4 :bzip2 :snappy :gzip)

byte-transforms> (available-encoders)
(:base64)

When choosing a compression algorithm, snappy is typically the fastest, bzip2 yields the highest compression, and lz4 provides a good balance between higher compression rate and fast decompression. All the compression algorithms except lz4 are concat-able; multiple compressed segments can be concatenated and decompressed as a single stream.

Full stats on all methods can be found by cloning the project and running lein test :benchmark.

License

Copyright © 2013 Zachary Tellman

Distributed under the Apache License 2.0.

byte-transforms's People

Contributors

ganeshgautam avatar kingmob avatar reiddraper avatar slipset avatar zac1st1k avatar ztellman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

byte-transforms's Issues

Tests fail with byte-streams 0.2.1-alpha2 and up

I went to bump up byte-streams, since 0.2.0 is quite old, and started seeing a lot of failures in the concat-roundtrip-compressors test like:

FAIL in (concat-roundtrip-compressors) (byte_transforms_simple_check.clj:51)
expected: {:result true}
  actual: {:shrunk {:total-nodes-visited 186, :depth 0, :pass? false, :result false, :result-data nil, :time-shrinking-ms 406, :smallest [#object["[B" 0x3305897c "[B@3305897c"] 1 :gzip]}, :failed-after-ms 1151, :num-tests 1888, :seed 1651736988115, :fail [#object["[B" 0x3305897c "[B@3305897c"] 1 :gzip], :result false, :result-data nil, :failing-size 87, :pass? false, :test-var "concat-roundtrip-compressors"}

The only change in byte-streams from 0.2.1-alpha1 to 0.2.1-alpha2 was clj-commons/byte-streams@4750b5b.

This is almost certainly a byte-streams bug, possibly related to clj-commons/byte-streams#37, given the code change, but this issue serves as a reminder to revisit when byte-streams is fixed.

release 1.0 version of this lib

Given that this project neither has open issues nor open PRs and the activity has been rather limited over the past years, should we let the next release be a 1.0.0 just to signal that it's "Done"?

base64 encoding does not pad output properly

Many base64 decoding libraries expect the encoded string to be padded with one or two "=" characters so that the length is a multiple of 4, as given by the following sample code in https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-08#appendix-C

static byte [] base64urldecode(string arg)
{
  string s = arg;
  s = s.Replace('-', '+'); // 62nd char of encoding
  s = s.Replace('_', '/'); // 63rd char of encoding
  switch (s.Length % 4) // Pad with trailing '='s
  {
    case 0: break; // No pad chars in this case
    case 2: s += "=="; break; // Two pad chars
    case 3: s += "="; break; // One pad char
    default: throw new System.Exception(
      "Illegal base64url string!");
  }
  return Convert.FromBase64String(s); // Standard base64 decoder
}

Can we have an option for byte-transforms/encode :base64 to include this padding?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.