com.davidcode.code.util.ErrorBuffer.java Source code

Java tutorial

Introduction

Here is the source code for com.davidcode.code.util.ErrorBuffer.java

Source

/*
 * This file is part of ErrorReporter
 * The MIT License (MIT)
 *
 * Copyright (c) 2014 David
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.davidcode.code.util;

import org.apache.commons.codec.binary.Base64;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.nio.ByteBuffer;

/**
 * @name: ErrorBuffer
 * @author: David
 * @version: 1.0
 * <p/>
 * An ErrorBuffer provides methods to store and send
 * error information over TCP.
 */
public class ErrorBuffer {
    private static final int DEFAULT_BUFFER_SIZE = 2048;
    public ByteBuffer buffer;

    /**
     * Creates an ErrorBuffer with a default buffer size
     */
    public ErrorBuffer() {
        this(DEFAULT_BUFFER_SIZE);
    }

    /**
     * Creates an ErrorBuffer with a specified buffer size
     * Allocates two bytes for the length of the data for the server
     * to read
     *
     * @param size
     */
    public ErrorBuffer(int size) {
        buffer = ByteBuffer.allocate(size);
        buffer.position(2);
    }

    /**
     * Inserts an String into the buffer with the length in front of it.
     *
     * @param s
     */
    public void pstr(String s) {
        p4(s.length());
        pdata(s.getBytes());
    }

    /**
     * Inserts a reversed string into the buffer
     *
     * @param input
     */
    public void rpstr(String input) {
        pstr(new StringBuffer(input).reverse().toString());
    }

    /**
     * Inserts a Base64 encded string into the buffer
     *
     * @param input
     */
    public void pstrb64(String input) {
        byte[] encoded = Base64.encodeBase64(input.getBytes());
        p4(encoded.length);
        pdata(encoded);
    }

    /**
     * Inserts a byte array into the buffer
     *
     * @param bytes
     */
    public void pdata(byte[] bytes) {
        buffer.put(bytes);
    }

    /**
     * Inserts a long into the buffer
     *
     * @param i
     */
    public void p8(long i) {
        buffer.putLong(i);
    }

    /**
     * Inserts an integer into the buffer
     *
     * @param i
     */
    public void p4(int i) {
        buffer.putInt(i);
    }

    /**
     * Inserts a short into the buffer
     *
     * @param i
     */
    public void p2(int i) {
        buffer.putShort((short) i);
    }

    /**
     * Inserts a byte into the buffer
     *
     * @param i
     */
    public void p1(int i) {
        buffer.put((byte) i);
    }

    /**
     * Writes the data to a socket using prehistoric java
     *
     * @param socket
     * @throws IOException
     */
    public void write(Socket socket) throws IOException {

        //Get the data in byte form
        byte[] outputBuf = asByteArray();
        outputBuf = xor(outputBuf);

        //Write the data to the socket which is connected to a server for recieving this data
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
        dataOutputStream.write(outputBuf);

        //Flush and close the stream
        dataOutputStream.flush();
        dataOutputStream.close();
    }

    /**
     * Perform a exclusive OR operation on each byte of the array using a byte from the stored key
     *
     * @param buf
     * @return
     */
    private byte[] xor(byte[] buf) {

        byte[] keyBytes = xorKey.getBytes();

        for (int i = 0; i < buf.length; i++) {
            buf[i] ^= keyBytes[i % keyBytes.length];
        }

        return buf;
    }

    /**
     * Copies the buffer into a byte array
     *
     * @return
     */
    public byte[] asByteArray() {
        //Insert the length of the content at the beginning of the buffer
        int len = buffer.position() - 2;
        buffer.flip();
        p2(len);

        //Copy the data to the byte array with room for the length
        buffer.position(0);
        byte[] data = new byte[len + 2];
        buffer.get(data);

        //Return the data array
        return data;
    }

    private final String xorKey = "abas@d0a9SD7098#$(*7asdJHLKHL";

}