ByteUtils.java Source code

Java tutorial

Introduction

Here is the source code for ByteUtils.java

Source

/*
This file is part of Socks via HTTP.
    
This package is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
Socks via HTTP is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Socks via HTTP; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

// Title :        ByteUtils.java
// Version :      1.2
// Copyright :    Copyright (c) 2001-2002
// Author :       Florent CUETO  & Sebastien LEBRETON <socksviahttp@cqs.dyndns.org>
// Description :  Class to manipulate bytes

//package socksviahttp.core.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class ByteUtils {
    public static byte[] packRaw(byte[] b) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        GZIPOutputStream zos = new GZIPOutputStream(baos);
        zos.write(b);
        zos.close();

        return baos.toByteArray();
    }

    public static byte[] unpackRaw(byte[] b) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ByteArrayInputStream bais = new ByteArrayInputStream(b);

        GZIPInputStream zis = new GZIPInputStream(bais);
        byte[] tmpBuffer = new byte[256];
        int n;
        while ((n = zis.read(tmpBuffer)) >= 0)
            baos.write(tmpBuffer, 0, n);
        zis.close();

        return baos.toByteArray();
    }

    public static byte[] encryptRaw(byte[] key, byte[] b) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        for (int i = 0; i < b.length; i++) {
            baos.write(b[i] ^ key[i % key.length]);
        }
        // baos.close();
        return (baos.toByteArray());
    }

    public static byte[] decryptRaw(byte[] key, byte[] b) throws IOException {
        return (encryptRaw(key, b));
    }
}