Created
May 19, 2012 00:06
-
-
Save mushkevych/2728253 to your computer and use it in GitHub Desktop.
Simplest Integer Encoder Example
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
import org.apache.log4j.Logger; | |
import org.apache.mahout.math.Varint; | |
import java.io.*; | |
/** | |
* @author Bohdan Mushkevych | |
* Description: module presents tuple of two int values: alpha and beta | |
*/ | |
class Tuple2I { | |
protected int alpha; | |
protected int beta; | |
public Tuple2I(int alpha, int beta) { | |
this.alpha = alpha; | |
this.beta = beta; | |
} | |
/** | |
* @return int presenting alpha | |
*/ | |
public int getAlpha() { | |
return alpha; | |
} | |
/** | |
* @return int presenting beta | |
*/ | |
public int getBeta() { | |
return beta; | |
} | |
} | |
/** | |
* @author Bohdan Mushkevych | |
* Description: module contains logic for VarInt integer encoding: conversion to and from the byte array | |
*/ | |
public class Encoder { | |
protected ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
protected DataOutputStream dos = new DataOutputStream(baos); | |
private Logger log = Logger.getRootLogger(); | |
public Encoder() { | |
} | |
/** | |
* Method generates byte[] for varargs of integers | |
* @param args varargs of Integer type | |
* @return generated value | |
*/ | |
protected byte[] getByteArray(int... args) { | |
byte[] result = null; | |
try { | |
baos.reset(); | |
for (int i : args) { | |
Varint.writeSignedVarInt(i, dos); | |
} | |
result = baos.toByteArray(); | |
} catch (IOException e) { | |
log.error("Exception on Integer encoding", e); | |
} | |
return result; | |
} | |
/** | |
* method decodes tuple from byte array | |
* @param value encoded tuple | |
* @return formed tuple instance | |
*/ | |
public Tuple2I decode(byte[] value) { | |
ByteArrayInputStream bais = new ByteArrayInputStream(value); | |
DataInputStream dis = new DataInputStream(bais); | |
Tuple2I tuple = null; | |
try { | |
int alpha = Varint.readSignedVarInt(dis); | |
int beta = Varint.readSignedVarInt(dis); | |
tuple = new Tuple2I(alpha, beta); | |
} catch (IOException e) { | |
log.error("Exception on Integer decoding", e); | |
} | |
return tuple; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment