Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS 

nginx gzip config

  # output compression saves bandwidth 
  gzip              on;
  gzip_proxied      any;
  gzip_http_version 1.1;
  #gzip_min_length   1100;
  gzip_comp_level   5;
  #gzip_buffers      4 8k;
  gzip_types        text/plain text/html text/xml text/css application/x-javascript text/xml application/xml application/xml+rss text/javascript application/atom+xml;
  #gzip_vary        on;
  #gzip_disable     "MSIE [1-6]\.";

Custom base conversion (to and from)

Handy to store large numbers in a shorter notation. Doesn't work with negative numbers (for negative numbers you need to store an additional byte to indicate a positive or negative value, pass the absolute value).

	private static final short BASE_40_RADIX = 40;
	private static final short BASE_40_BASE = 48;
	private static final String BASE_40_ZERO = "0";

	public static long fromBase40(char[] cs) {
		return fromBase(cs, BASE_40_BASE, BASE_40_RADIX);
	}

	public static char[] toBase40(final long i) {
		return toBase(i, BASE_40_BASE, BASE_40_RADIX, BASE_40_ZERO);
	}

	private static final short BASE_75_RADIX = 75;
	private static final short BASE_75_BASE = 48;
	private static final String BASE_75_ZERO = "0";

	public static long fromBase75(char[] cs) {
		return fromBase(cs, BASE_75_BASE, BASE_75_RADIX);
	}

	public static char[] toBase75(final long i) {
		return toBase(i, BASE_75_BASE, BASE_75_RADIX, BASE_75_ZERO);
	}

	private static final short BASE_90_RADIX = 90;
	private static final short BASE_90_BASE = 33;
	private static final String BASE_90_ZERO = "!";

	public static long fromBase90(char[] cs) {
		return fromBase(cs, BASE_90_BASE, BASE_90_RADIX);
	}

	public static char[] toBase90(final long i) {
		return toBase(i, BASE_90_BASE, BASE_90_RADIX, BASE_90_ZERO);
	}

	private static char[] toBase(final long i, final short base, final short radix, String nullCharacter) {
		long value = i;

		if (value == 0) {
			return nullCharacter.toCharArray();
		}

		String result = "";
		while (value > 0) {
			long mod = value % radix;
			value -= mod;
			if (value > 0) value /= radix;
			result = (char)(mod + base) + result;
		}

		return result.toCharArray();
	}

	private static long fromBase(char[] cs, final short base, final short radix) {
		long value = 0;
		for (int i = cs.length - 1; i >= 0; i--) {
			int digit = ((int)cs[i]) - base;
			if (digit < 0 || digit >= radix) {
				throw new IllegalArgumentException("Invalid Base" + radix + " character: " + cs[i]);
			}
			long digitBase = (long)Math.pow(radix, (cs.length - 1) - i);
			value = (digit * digitBase) + value;
		}

		return value;
	}


And here are some tests:

	public void testFromBase40() {
		assertEquals(2559999, NumberUtils.fromBase40("WWWW".toCharArray()));
		assertEquals(1117580, NumberUtils.fromBase40("ABCD".toCharArray()));

		assertEquals(0, NumberUtils.fromBase40("0".toCharArray()));
		assertEquals(1, NumberUtils.fromBase40("1".toCharArray()));

		assertEquals(40, NumberUtils.fromBase40("10".toCharArray()));
		assertEquals(80, NumberUtils.fromBase40("20".toCharArray()));

		assertEquals(41, NumberUtils.fromBase40("11".toCharArray()));
		assertEquals(81, NumberUtils.fromBase40("21".toCharArray()));

		assertEquals(81, NumberUtils.fromBase40("0021".toCharArray()));

		assertEquals(3061560805L, NumberUtils.fromBase40("MSTSD5".toCharArray()));
	}

	public void testToBase40() {
		assertEquals("0", new String(NumberUtils.toBase40(0)));
		assertEquals("1", new String(NumberUtils.toBase40(1)));

		assertEquals("10", new String(NumberUtils.toBase40(40)));
		assertEquals("20", new String(NumberUtils.toBase40(80)));

		assertEquals("11", new String(NumberUtils.toBase40(41)));
		assertEquals("21", new String(NumberUtils.toBase40(81)));

		assertEquals("ABCD", new String(NumberUtils.toBase40(1117580)));
		assertEquals("WWWW", new String(NumberUtils.toBase40(2559999)));

		assertEquals("MSTSD5", new String(NumberUtils.toBase40(3061560805L)));

	}

	public void testToBase90() throws Exception {
		assertEquals("#Q&Y.`QY#", new String(NumberUtils.toBase90(10908158098650842L)));
	}

	public void testFromBase90() throws Exception {
		assertEquals(10908158098650842L, NumberUtils.fromBase90("#Q&Y.`QY#".toCharArray()));
	}

Compress/decompress byte array

 using System;
 using System.Collections.Generic;
 using System.IO.Compression;
 using System.IO;
 using System.Collections;

namespace Utilities
{
 class Compression
 {
        public static byte[] Compress(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress);
            ds.Write(data, 0, data.Length);
            ds.Flush();
            ds.Close();
            return ms.ToArray();
        }
        public static byte[] Decompress(byte[] data)
        {
            const int BUFFER_SIZE = 256;
            byte[] tempArray = new byte[BUFFER_SIZE];
            List<byte[]> tempList = new List<byte[]>();
            int count = 0, length = 0;

            MemoryStream ms = new MemoryStream(data);
            DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);

            while ((count = ds.Read(tempArray, 0, BUFFER_SIZE)) > 0)
            {
                if (count == BUFFER_SIZE)
                {
                    tempList.Add(tempArray);
                    tempArray = new byte[BUFFER_SIZE];
                }
                else
                {
                    byte[] temp = new byte[count];
                    Array.Copy(tempArray, 0, temp, 0, count);
                    tempList.Add(temp);
                }
                length += count;
            }

            byte[] retVal = new byte[length];

            count = 0;
            foreach (byte[] temp in tempList)
            {
                Array.Copy(temp, 0, retVal, count, temp.Length);
                count += temp.Length;
            }

            return retVal;
        }
 }
}

Compress your ActiveRecord sessions

Using ActiveRecordStore and your sessions are getting too big? Try this!

# in environment.rb or some file you require
require 'zlib'
CGI::Session::ActiveRecordStore::Session.class_eval {
  class << self
    def marshal_with_compression(data)
      Zlib::Deflate.deflate(marshal_without_compression(data))
    end
    def unmarshal_with_compression(data)
      unmarshal_without_compression(Zlib::Inflate.inflate(data))
    end
    alias_method_chain :marshal, :compression
    alias_method_chain :unmarshal, :compression
  end
}

# in migration
def self.up
  change_column :sessions, :data, :binary
end

def self.down
  change_column :sessions, :data, :text
end

Dataset compression


    private static byte[] DataSetCompress(DataSet dataSet)
    {
        MemoryStream ms = new MemoryStream();
        BinaryFormatter bf = new BinaryFormatter();
        DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress);
        bf.Serialize(ds, dataSet);
        ds.Flush();
        ds.Close();
        return ms.ToArray();
    }
    private static DataSet DataSetDecompress(byte[] data)
    {
        BinaryFormatter bf = new BinaryFormatter();
        MemoryStream ms = new MemoryStream(data);
        DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);
        DataSet dataSet = (DataSet)bf.Deserialize(ds);
        return dataSet;
    }

XML (or any string type) compression

        public static byte[] CompressXML(string xml)
        {
            byte[] temp = Encoding.UTF8.GetBytes(xml);
            MemoryStream ms = new MemoryStream();
            DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress);
            ds.Write(temp, 0, temp.Length);
            ds.Flush();
            ds.Close();
            return ms.ToArray();
        }
       
        public static string DecompressXML(byte[] data)
        {
            const int BUFFER_SIZE = 10;
            byte[] tempArray = new byte[BUFFER_SIZE];
            ArrayList tempList = new ArrayList();
            int count = 0, length = 0;

            MemoryStream ms = new MemoryStream(data);
            DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress);

            while ((count = ds.Read(tempArray, 0, BUFFER_SIZE)) > 0)
            {
                if (count == BUFFER_SIZE)
                {
                    tempList.Add(tempArray);
                    tempArray = new byte[BUFFER_SIZE];
                }
                else
                {
                    byte[] temp = new byte[count];
                    Array.Copy(tempArray, 0, temp, 0, count);
                    tempList.Add(temp);
                }
                length += count;
            }

            byte[] retVal = new byte[length];

            count = 0;
            foreach (byte[] temp in tempList)
            {
                Array.Copy(temp, 0, retVal, count, temp.Length);
                count += temp.Length;
            }

            return Encoding.UTF8.GetString(retVal);
        }

ssh remote backups

// http://www.linux.com/article.pl?sid=06/01/12/1937210
// Backup with remote compression and storage

// When compression is necessary (and feasible), workload
// distribution becomes more effective with OpenSSH. Just as // distcc allows multiple machines to compile
// simultaneously, OpenSSH lets one system create the
// archive, while another system compresses it:

// tar cf - dirname | ssh remotehost "gzip -c >
// ${TMPFILE}.tar.gz"

tar cf - local-dir-eliotwalker | ssh -l mctt remote-glos.corruptive.co.uk "gzip -c > corr.tar.gz"
« Newer Snippets
Older Snippets »
Showing 1-7 of 7 total  RSS