I do not know the site, and I have only asked one question here. I have no idea - how to handle a code problem. I tried a lot - but I could not fix.
I use StringBuilder - for because of its benefits according to the standard string
I want to delete the first character in the string - but the character that appears in the last place - is duplicated - in the last two places.
for example:
i have the String abcdef, when i delete - the first instace - 'a':
i got back the String bcdeff
well i try - to set the length of the String to original length minus one - but this dont give any result.
i try also - to set the string to new String - and after that - send the String that i was save in tmp string - but this do help either.
public void appendBuffer(StringBuilder dictionary)
{
for (int i = 0; i < appendToWindowBuffer; i++) {
if(dictionary.length() == windowSize)
{
dictionary.deleteCharAt(0);
}
if(nextByteIndex<source.length )
{
dictionary.append((char)source[nextByteIndex]);
nextByteIndex++;
}
else
{
currentLookaheadBufferSize--;
}
if(currentSearchBufferSize < searchBufferSize)
{
currentSearchBufferSize++;
}
}
appendToWindowBuffer = 0;
}
full code:
main class
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException
{
System.out.println("main");
String inPath = "C:\\Users\\avraam\\Documents\\final-assignment\\LZ77\\source.txt";
String outPath = "C:\\Users\\avraam\\Documents\\final-assignment\\LZ77\\encoded.txt";
String decompressedPath = "C:\\Users\\avraam\\Documents\\final-assignment\\LZ77\\decoded.txt";
int windowSize = 14;
int lookaheadBufferSize = 6;
LZ77 compress = new LZ77(inPath,outPath,windowSize,lookaheadBufferSize);
compress.compress();
}
}
match class
public class Match {
protected int length;
protected int offset;
protected String value;
public Match(int length, int offset, String value)
{
this.length=length;
this.offset=offset;
this.value = value;
}
public void SetOffset(int offset) { this.offset = offset; }
public void SetLength(int length) { this.length = length; }
public void SetValue(String value) { this.value = value; }
public void AddValue(char value) { this.value += value; }
public void Reset()
{
this.offset = 0;
this.length = 0;
this.value = "";
}
}
LZ77 class
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
public class LZ77 {
private String inPath = null;
private String outPath = null;
private File inFile;
private File outFile;
private final int windowSize;
private final int lookaheadBufferSize;
private final int searchBufferSize;
private int nextByteIndex = 0;
//private int lookAheadIndex = 0; //not always should be (windowsize - lookaheadBufferSize.) in the end maybr will be less character in the lookAhead buffer. the index when LookAhead start is equel to the length of SearchBuffer
private int currentSearchBufferSize = 0;
private int currentLookaheadBufferSize = 0;
private int appendToWindowBuffer = 0;
private byte[] source = null;
public LZ77(String inPath,String outPath,int windowSize,int lookaheadBufferSize) throws IOException
{
this.inPath = inPath;
this.outPath = outPath;
this.inFile = new File(inPath);
this.outFile = new File(outPath);
this.windowSize = windowSize;
this.lookaheadBufferSize = lookaheadBufferSize;
this.searchBufferSize = windowSize - lookaheadBufferSize;
this.source = Files.readAllBytes(inFile.toPath());
}
public void compress() throws IOException
{
/*
* 1. create whole windowBuffer (named - `dictionary`)- that will by used by lookahead and by search Buffers.
* 2. create compressed data - where the data that compressed will be send.
* 3. initialize dictionary - look a head buffer by giving it the size of `lookaheadBuffer`.
* 4. start encode.
* 5. make the encode.
*/
System.out.println("compress");
System.out.println("read the file.");
System.out.println("check if source array work: ");
for (int element: source) {
System.out.print((char)element + "");
}
System.out.println("call to bufferInitialize function");
StringBuilder dictionary = new StringBuilder();
bufferInitialize(dictionary);
System.out.println(dictionary.toString());
StringBuilder compressed = new StringBuilder();
encode(dictionary,compressed);
}
public void bufferInitialize(StringBuilder dictionary)
{
System.out.println("bufferInitialize");
for (int i = 0; i < lookaheadBufferSize; i++) {
dictionary.append((char)source[nextByteIndex]);
nextByteIndex++;
currentLookaheadBufferSize++;
}
// initialize the buffer in the beginning with look a head buffer.
}
public void bufferUpdate()
{
// gets int length - and read those number of bytes - from `inPath` source file.
}
public void encode(StringBuilder dictionary,StringBuilder compressed)
{
//while(nextByteIndex < source.length)
while(currentLookaheadBufferSize > 0)
{
Match match = findMatch(dictionary);
System.out.print("<"+match.offset + ","+match.length+","+ dictionary.charAt(currentSearchBufferSize + match.length) + ">");
appendToWindowBuffer = increaseBuffer(match.length);
appendBuffer(dictionary);
}
/**
* do while you reach to the end of the file
* check if there any possible match
* if do so
* find the maxMatch try always to add another character DONE
* call update function -
* the function will update the
* windowbuffer(dictionary), DONE
* nextByteIndex and DONE
* the position of the index that begins the lookAheadBuffer
* and size of the lookahead and
* search buffers, and
* reset the appendToWindowBuffer. DONE
*/
}
public void convertStringToBits()
{
}
public Match findMatch(StringBuilder dictionary)
{
/**
* function get the window buffer - and index to start.
* the function will be find the max match that starts from index 0 to index start (we cant start search after the `start index`)
* because this parts belong to the look a head buffer.
* #param
* #return
*/
Match match= new Match(0,0, "");
String matchedString = null;
int offset;
int matchLookAheadIndex = currentSearchBufferSize;
if(!haveAnyMatch(dictionary))
{
addMatch();
}
else {
matchedString = "" + dictionary.charAt(matchLookAheadIndex);
offset = findMatchIndex(dictionary,matchedString);
while(offset != -1)
{
match.SetLength(match.length + 1);
match.SetOffset(offset);
match.SetValue(matchedString);
matchLookAheadIndex++;
matchedString +=dictionary.charAt(matchLookAheadIndex);
offset = findMatchIndex(dictionary,matchedString);
}
}
return match;
}
public int findMatchIndex(StringBuilder dictionary,String value)
{
int stringLength = value.length();
String tmpMatch = null;
int offsetMatch;
for (int i = currentSearchBufferSize - 1; i >=0; i--)
{
tmpMatch = dictionary.substring(i, i +stringLength );
offsetMatch = currentSearchBufferSize - i;
if(tmpMatch.equals(value))
{
System.out.println("data was match is searchWindow");
System.out.println("the offset from LookAHead is: " + offsetMatch);
return offsetMatch;
}
}
return -1;
}
public boolean haveAnyMatch(StringBuilder dictionary)
{
if (currentSearchBufferSize == 0)
{
System.out.println("dont have match - search buffer is empty now");
return false;
}
if(!isExistInSearchBuffer(dictionary,dictionary.charAt(currentSearchBufferSize)))
{
System.out.println("dont have match - the first character in lookAheadBuffer wasn't found in searchBuffer");
return false;
}
return true;
/*
* check:
* if search buffer is empty
* if the needed character isn't exist in the search buffer
* if the current value is big enough - and match was not found.
*/
}
public boolean isExistInSearchBuffer(StringBuilder dictionary, char isCharAtDictionary)
{
for (int i = 0; i < currentSearchBufferSize; i++) {
if(dictionary.charAt(i) == isCharAtDictionary)
{
return true;
}
}
return false;
}
public void nextMatch(StringBuilder dictionary)
{
/**
* #param: value, window buffer.
* #description: find the current match with the needed value in the search buffer boundaries.
*/
}
public int increaseBuffer(int matchLength)
{
return 1 + matchLength;
/*
* return int - that calulate by how many byte we need to increase the buffer
*/
}
public void addMatch()
{
}
public void addBitSize() {
}
public void appendBuffer(StringBuilder dictionary)
{
for (int i = 0; i < appendToWindowBuffer; i++) {
if(dictionary.length() == windowSize)
{
dictionary.deleteCharAt(0);
}
if(nextByteIndex<source.length )
{
dictionary.append((char)source[nextByteIndex]);
nextByteIndex++;
}
else
{
currentLookaheadBufferSize--;
}
if(currentSearchBufferSize < searchBufferSize)
{
currentSearchBufferSize++;
}
}
appendToWindowBuffer = 0;
}
}
This seems to work:
StringBuilder builder = new StringBuilder("abcdef");
builder.deleteCharAt(0);
System.out.println(builder.toString());
Prints: bcdef
How are you doing it?
the solution that work for me:
i need to write the decompress - to outout file.
I registered to the file - in CHAR - this is not true - need to register output in BYTE
WORNG WAY THAT I DO:
private void writeDecode (StringBuilder decompress) throws IOException
{
Writer write = new FileWriter(this.outFile);
write.write(decompress.toString());
write.close();
}
THE CORRECT WAY
private void WriteToFile(StringBuilder decodedData) throws IOException
{
FileOutputStream outputFileStream = new FileOutputStream(this.outPath);
for(int i = 0; i < decodedData.length(); i++)
{
byte currentByte = (byte)decodedData.charAt(i);
outputFileStream.write(currentByte);
}
outputFileStream.close();
}
I need to register in the file in BYTE - this is the right way.
Related
I have a string that might contain both Unicode and UTF-8 characters. This makes it difficult when I want to save them to a database that doesn't deal with Unicode characters. The database I am using is PostgreSQL. They might be to big for a certain column this is a simple example for my situation
public static void main(String[] args) {
String test= "İİİİİİİİİİ";
byte[] bytesOrig = null;
bytesOrig = test.getBytes("UTF-8");
System.out.println("bytesOrig="+new String(bytesOrig));
byte[] bytesFive = new byte[5];
System.arraycopy(bytesOrig, 0, bytesFive, 0, 5);
System.out.println("bytes-Five="+new String(bytesFive));
System.out.println("Substring="+test.substring(0,5));
System.out.println("Substring real length=" + test.substring(0,5).getBytes().length);
}
I cant use the String.substring method since it will NOT help me in case of double bytes characters - i have tried working with Bytes-Array copy but it means that the last characters might be deleted.
And this is the Debug info:
bytesOrig=İİİİİİİİİİ
bytes-Five=İİ�
Substring=İİİİİ
Substring real length=10
You can see since I have only part of the Bytes array - I don't want the last character to appear as �
You can try this: the changing places are shows in comment.
public static void main(String[] args) throws UnsupportedEncodingException {
String test= "İİİİİİİİİİ";
System.out.println("test.length() = " + test.length()); // out: 10
byte[] bytesOrig;
bytesOrig = test.getBytes("UTF-8"); // but after get bytes will return 20
System.out.println("bytesOrig.length = " + bytesOrig.length); // it
System.out.println("bytesOrig="+new String(bytesOrig));
byte[] bytesFive = new byte[10]; // 1. so change here to twice
System.arraycopy(bytesOrig, 0, bytesFive, 0, 10); // 2. change here also
System.out.println("bytes-Five="+new String(bytesFive));
System.out.println("Substring="+test.substring(0,5));
System.out.println("Substring real length=" + test.substring(0,5).getBytes().length);
}
And here is the output:
test.length() = 10
bytesOrig.length = 20
bytesOrig=İİİİİİİİİİ
bytes-Five=İİİİİ
Substring=İİİİİ
Substring real length=10
Most of the logic is done at transformOverhead method
according to the value, we will increase i.
public class ZoneTrimTransformer implements ZoneTransformer {
private int maxLength;
public ZoneTrimTransformer(int maxLength) {
this.maxLength = maxLength;
}
#Override
public Object transform(Object value) {
if (value == null) {
return null;
}
else {
String val = value.toString();
if (getDBRequireLength(val) > getJavaLength(val) ){
return transformOverhead(val);
}
else if (val != null && getJavaLength(val) > maxLength) {
// Trim
val = val.substring(0, maxLength);
}
return val;
}
}
public static void main(String[] args) {
System.out.println("1234567".substring(0, 5));
}
public int getJavaLength(String val) {
return val.length();
}
public int getDBRequireLength(String val) {
return val.getBytes().length;
}
private Object transformOverhead(Object s) {
byte[] byeArray = s.toString().getBytes();
if (byeArray.length < maxLength) maxLength = byeArray.length;
int n16 = 0;
int advance = 1;
int i = 0;
while (i < maxLength) {
advance = 1;
if ((byeArray[i] & 0x80) == 0) i += 1;
else if ((byeArray[i] & 0xE0) == 0xC0) i += 2;
else if ((byeArray[i] & 0xF0) == 0xE0) i += 3;
else { i += 4; advance = 2; }
if (i <= maxLength) n16 += advance;
}
return s.toString().substring(0,n16);
}
}
And a test class for it:
#Category(UnitTest.class)
#RunWith(MockitoJUnitRunner.class)
public class ZoneTrimTransformerTest {
#Test
public void testLengths()
{
ZoneTrimTransformer lztr = new ZoneTrimTransformer(5);
assertEquals(lztr.transform("İİİİİİİİİİ"),"İİ");
assertEquals(lztr.getDBRequireLength(lztr.transform("İİİİİİİİİİ").toString()),4);
assertEquals(lztr.getJavaLength(lztr.transform("İİİİİİİİİİ").toString()),2);
assertEquals(lztr.transform("%&*%$^&$$^&$").toString(),"%&*%$");
assertEquals(lztr.getDBRequireLength(("%&*%$^&$$^&$").toString()),12);
assertEquals(lztr.getJavaLength(("%&*%$^&$$^&$").toString()),12);
assertEquals(lztr.transform("媒体アカウント名"),"媒");
assertEquals(lztr.getDBRequireLength(("媒体アカウント名").toString()),24);
assertEquals(lztr.getJavaLength(("媒体アカウント名").toString()),8);
assertEquals(lztr.transform("媒体媒体アカウント名アカウント名"),"媒");
assertEquals(lztr.getDBRequireLength(("媒体媒体アカウント名アカウント名").toString()),48);
assertEquals(lztr.getJavaLength(("媒体媒体アカウント名アカウント名").toString()),16);
assertEquals(lztr.transform("SÄ°STA+UNÄ°VERSAL"),"SÄ°");
assertEquals(lztr.getDBRequireLength(("SÄ°STA+UNÄ°VERSAL").toString()),21);
assertEquals(lztr.getJavaLength(("SÄ°STA+UNÄ°VERSAL").toString()),17);
assertEquals(lztr.transform("1234567890"),"12345");
assertEquals(lztr.getDBRequireLength(("1234567890").toString()),10);
assertEquals(lztr.getJavaLength(("1234567890").toString()),10);
assertEquals(lztr.transform("abcdefghI"),"abcde");
assertEquals(lztr.getDBRequireLength(("abcdefghI").toString()),9);
assertEquals(lztr.getJavaLength(("abcdefghI").toString()),9);
assertEquals(lztr.transform("אהגדהוזחט"),"אה");
assertEquals(lztr.getDBRequireLength(("אהגדהוזחט").toString()),18);
assertEquals(lztr.getJavaLength(("אהגדהוזחט").toString()),9);
assertEquals(lztr.transform("ABAA"),"ABAA");
assertEquals(lztr.getDBRequireLength(("ABAA").toString()),4);
assertEquals(lztr.getJavaLength(("ABAA").toString()),4);
assertEquals(lztr.getDBRequireLength(lztr.transform("ABCE").toString()),4);
assertEquals(lztr.getJavaLength(lztr.transform("ABCE").toString()),4);
}
}
I used jflex to generate a Java class and now I want to use it to get lexical analysis of a .txt file. The Java Class begins like this:
/* The following code was generated by JFlex 1.6.1 */
/**
* This class is a scanner generated by
* JFlex 1.6.1
* from the specification file <tt>patch.flex</tt>
*/
public class Patch {
/** This character denotes the end of file */
public static final int YYEOF = -1;
/** initial size of the lookahead buffer */
private static final int ZZ_BUFFERSIZE = 16384;
/** lexical states */
public static final int YYINITIAL = 0;
/**
* ZZ_LEXSTATE[l] is the state in the DFA for the lexical state l
* ZZ_LEXSTATE[l+1] is the state in the DFA for the lexical state l
* at the beginning of a line
* l is of the form l = 2*k, k a non negative integer
*/
private static final int ZZ_LEXSTATE[] = {
0, 0
};
/**
* Translates characters to character classes
*/
private static final String ZZ_CMAP_PACKED =
"\141\0\1\1\1\2\1\3\1\4\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffff\0\uffab\0";
/**
* Translates characters to character classes
*/
private static final char [] ZZ_CMAP = zzUnpackCMap(ZZ_CMAP_PACKED);
/**
* Translates DFA states to action switch labels.
*/
private static final int [] ZZ_ACTION = zzUnpackAction();
private static final String ZZ_ACTION_PACKED_0 =
"\1\0\2\1\3\0\1\2\2\0\2\2";
private static int [] zzUnpackAction() {
int [] result = new int[11];
int offset = 0;
offset = zzUnpackAction(ZZ_ACTION_PACKED_0, offset, result);
return result;
}
private static int zzUnpackAction(String packed, int offset, int [] result) {
int i = 0; /* index in packed string */
int j = offset; /* index in unpacked array */
int l = packed.length();
while (i < l) {
int count = packed.charAt(i++);
int value = packed.charAt(i++);
do result[j++] = value; while (--count > 0);
}
return j;
}
/**
* Translates a state to a row index in the transition table
*/
private static final int [] ZZ_ROWMAP = zzUnpackRowMap();
private static final String ZZ_ROWMAP_PACKED_0 =
"\0\0\0\5\0\12\0\12\0\17\0\24\0\31\0\36"+
"\0\31\0\5\0\36";
private static int [] zzUnpackRowMap() {
int [] result = new int[11];
int offset = 0;
offset = zzUnpackRowMap(ZZ_ROWMAP_PACKED_0, offset, result);
return result;
}
private static int zzUnpackRowMap(String packed, int offset, int [] result) {
int i = 0; /* index in packed string */
int j = offset; /* index in unpacked array */
int l = packed.length();
while (i < l) {
int high = packed.charAt(i++) << 16;
result[j++] = high | packed.charAt(i++);
}
return j;
}
/**
* The transition table of the DFA
*/
private static final int [] ZZ_TRANS = zzUnpackTrans();
private static final String ZZ_TRANS_PACKED_0 =
"\1\2\1\3\3\2\6\0\1\4\1\5\1\6\3\0"+
"\1\5\1\7\2\0\1\10\1\0\1\6\4\0\1\11"+
"\1\12\1\0\1\13\3\0";
private static int [] zzUnpackTrans() {
int [] result = new int[35];
int offset = 0;
offset = zzUnpackTrans(ZZ_TRANS_PACKED_0, offset, result);
return result;
}
private static int zzUnpackTrans(String packed, int offset, int [] result) {
int i = 0; /* index in packed string */
int j = offset; /* index in unpacked array */
int l = packed.length();
while (i < l) {
int count = packed.charAt(i++);
int value = packed.charAt(i++);
value--;
do result[j++] = value; while (--count > 0);
}
return j;
}
/* error codes */
private static final int ZZ_UNKNOWN_ERROR = 0;
private static final int ZZ_NO_MATCH = 1;
private static final int ZZ_PUSHBACK_2BIG = 2;
/* error messages for the codes above */
private static final String ZZ_ERROR_MSG[] = {
"Unknown internal scanner error",
"Error: could not match input",
"Error: pushback value was too large"
};
/**
* ZZ_ATTRIBUTE[aState] contains the attributes of state <code>aState</code>
*/
private static final int [] ZZ_ATTRIBUTE = zzUnpackAttribute();
private static final String ZZ_ATTRIBUTE_PACKED_0 =
"\1\0\1\11\1\1\3\0\1\1\2\0\1\11\1\1";
private static int [] zzUnpackAttribute() {
int [] result = new int[11];
int offset = 0;
offset = zzUnpackAttribute(ZZ_ATTRIBUTE_PACKED_0, offset, result);
return result;
}
private static int zzUnpackAttribute(String packed, int offset, int [] result) {
int i = 0; /* index in packed string */
int j = offset; /* index in unpacked array */
int l = packed.length();
while (i < l) {
int count = packed.charAt(i++);
int value = packed.charAt(i++);
do result[j++] = value; while (--count > 0);
}
return j;
}
/** the input device */
private java.io.Reader zzReader;
/** the current state of the DFA */
private int zzState;
/** the current lexical state */
private int zzLexicalState = YYINITIAL;
/** this buffer contains the current text to be matched and is
the source of the yytext() string */
private char zzBuffer[] = new char[ZZ_BUFFERSIZE];
/** the textposition at the last accepting state */
private int zzMarkedPos;
/** the current text position in the buffer */
private int zzCurrentPos;
/** startRead marks the beginning of the yytext() string in the buffer */
private int zzStartRead;
/** endRead marks the last character in the buffer, that has been read
from input */
private int zzEndRead;
/** number of newlines encountered up to the start of the matched text */
private int yyline;
/** the number of characters up to the start of the matched text */
private int yychar;
/**
* the number of characters from the last newline up to the start of the
* matched text
*/
private int yycolumn;
/**
* zzAtBOL == true <=> the scanner is currently at the beginning of a line
*/
private boolean zzAtBOL = true;
/** zzAtEOF == true <=> the scanner is at the EOF */
private boolean zzAtEOF;
/** denotes if the user-EOF-code has already been executed */
private boolean zzEOFDone;
/**
* The number of occupied positions in zzBuffer beyond zzEndRead.
* When a lead/high surrogate has been read from the input stream
* into the final zzBuffer position, this will have a value of 1;
* otherwise, it will have a value of 0.
*/
private int zzFinalHighSurrogate = 0;
/**
* Creates a new scanner
*
* #param in the java.io.Reader to read input from.
*/
public Patch(java.io.Reader in) {
this.zzReader = in;
}
/**
* Unpacks the compressed character translation table.
*
* #param packed the packed character translation table
* #return the unpacked character translation table
*/
private static char [] zzUnpackCMap(String packed) {
char [] map = new char[0x110000];
int i = 0; /* index in packed string */
int j = 0; /* index in unpacked array */
while (i < 44) {
int count = packed.charAt(i++);
char value = packed.charAt(i++);
do map[j++] = value; while (--count > 0);
}
return map;
}
/**
* Refills the input buffer.
*
* #return <code>false</code>, iff there was new input.
*
* #exception java.io.IOException if any I/O-Error occurs
*/
private boolean zzRefill() throws java.io.IOException {
/* first: make room (if you can) */
if (zzStartRead > 0) {
zzEndRead += zzFinalHighSurrogate;
zzFinalHighSurrogate = 0;
System.arraycopy(zzBuffer, zzStartRead,
zzBuffer, 0,
zzEndRead-zzStartRead);
/* translate stored positions */
zzEndRead-= zzStartRead;
zzCurrentPos-= zzStartRead;
zzMarkedPos-= zzStartRead;
zzStartRead = 0;
}
/* is the buffer big enough? */
if (zzCurrentPos >= zzBuffer.length - zzFinalHighSurrogate) {
/* if not: blow it up */
char newBuffer[] = new char[zzBuffer.length*2];
System.arraycopy(zzBuffer, 0, newBuffer, 0, zzBuffer.length);
zzBuffer = newBuffer;
zzEndRead += zzFinalHighSurrogate;
zzFinalHighSurrogate = 0;
}
/* fill the buffer with new input */
int requested = zzBuffer.length - zzEndRead;
int numRead = zzReader.read(zzBuffer, zzEndRead, requested);
/* not supposed to occur according to specification of java.io.Reader */
if (numRead == 0) {
throw new java.io.IOException("Reader returned 0 characters. See JFlex examples for workaround.");
}
if (numRead > 0) {
zzEndRead += numRead;
/* If numRead == requested, we might have requested to few chars to
encode a full Unicode character. We assume that a Reader would
otherwise never return half characters. */
if (numRead == requested) {
if (Character.isHighSurrogate(zzBuffer[zzEndRead - 1])) {
--zzEndRead;
zzFinalHighSurrogate = 1;
}
}
/* potentially more input available */
return false;
}
/* numRead < 0 ==> end of stream */
return true;
}
/**
* Closes the input stream.
*/
public final void yyclose() throws java.io.IOException {
zzAtEOF = true; /* indicate end of file */
zzEndRead = zzStartRead; /* invalidate buffer */
if (zzReader != null)
zzReader.close();
}
/**
* Resets the scanner to read from a new input stream.
* Does not close the old reader.
*
* All internal variables are reset, the old input stream
* <b>cannot</b> be reused (internal buffer is discarded and lost).
* Lexical state is set to <tt>ZZ_INITIAL</tt>.
*
* Internal scan buffer is resized down to its initial length, if it has grown.
*
* #param reader the new input stream
*/
public final void yyreset(java.io.Reader reader) {
zzReader = reader;
zzAtBOL = true;
zzAtEOF = false;
zzEOFDone = false;
zzEndRead = zzStartRead = 0;
zzCurrentPos = zzMarkedPos = 0;
zzFinalHighSurrogate = 0;
yyline = yychar = yycolumn = 0;
zzLexicalState = YYINITIAL;
if (zzBuffer.length > ZZ_BUFFERSIZE)
zzBuffer = new char[ZZ_BUFFERSIZE];
}
/**
* Returns the current lexical state.
*/
public final int yystate() {
return zzLexicalState;
}
/**
* Enters a new lexical state
*
* #param newState the new lexical state
*/
public final void yybegin(int newState) {
zzLexicalState = newState;
}
/**
* Returns the text matched by the current regular expression.
*/
public final String yytext() {
return new String( zzBuffer, zzStartRead, zzMarkedPos-zzStartRead );
}
/**
* Returns the character at position <tt>pos</tt> from the
* matched text.
*
* It is equivalent to yytext().charAt(pos), but faster
*
* #param pos the position of the character to fetch.
* A value from 0 to yylength()-1.
*
* #return the character at position pos
*/
public final char yycharat(int pos) {
return zzBuffer[zzStartRead+pos];
}
/**
* Returns the length of the matched text region.
*/
public final int yylength() {
return zzMarkedPos-zzStartRead;
}
/**
* Reports an error that occured while scanning.
*
* In a wellformed scanner (no or only correct usage of
* yypushback(int) and a match-all fallback rule) this method
* will only be called with things that "Can't Possibly Happen".
* If this method is called, something is seriously wrong
* (e.g. a JFlex bug producing a faulty scanner etc.).
*
* Usual syntax/scanner level error handling should be done
* in error fallback rules.
*
* #param errorCode the code of the errormessage to display
*/
private void zzScanError(int errorCode) {
String message;
try {
message = ZZ_ERROR_MSG[errorCode];
}
catch (ArrayIndexOutOfBoundsException e) {
message = ZZ_ERROR_MSG[ZZ_UNKNOWN_ERROR];
}
throw new Error(message);
}
/**
* Pushes the specified amount of characters back into the input stream.
*
* They will be read again by then next call of the scanning method
*
* #param number the number of characters to be read again.
* This number must not be greater than yylength()!
*/
public void yypushback(int number) {
if ( number > yylength() )
zzScanError(ZZ_PUSHBACK_2BIG);
zzMarkedPos -= number;
}
/**
* Resumes scanning until the next regular expression is matched,
* the end of input is encountered or an I/O-Error occurs.
*
* #return the next token
* #exception java.io.IOException if any I/O-Error occurs
*/
public int yylex() throws java.io.IOException {
int zzInput;
int zzAction;
// cached fields:
int zzCurrentPosL;
int zzMarkedPosL;
int zzEndReadL = zzEndRead;
char [] zzBufferL = zzBuffer;
char [] zzCMapL = ZZ_CMAP;
int [] zzTransL = ZZ_TRANS;
int [] zzRowMapL = ZZ_ROWMAP;
int [] zzAttrL = ZZ_ATTRIBUTE;
while (true) {
zzMarkedPosL = zzMarkedPos;
boolean zzR = false;
int zzCh;
int zzCharCount;
for (zzCurrentPosL = zzStartRead ;
zzCurrentPosL < zzMarkedPosL ;
zzCurrentPosL += zzCharCount ) {
zzCh = Character.codePointAt(zzBufferL, zzCurrentPosL, zzMarkedPosL);
zzCharCount = Character.charCount(zzCh);
switch (zzCh) {
case '\u000B':
case '\u000C':
case '\u0085':
case '\u2028':
case '\u2029':
yyline++;
yycolumn = 0;
zzR = false;
break;
case '\r':
yyline++;
yycolumn = 0;
zzR = true;
break;
case '\n':
if (zzR)
zzR = false;
else {
yyline++;
yycolumn = 0;
}
break;
default:
zzR = false;
yycolumn += zzCharCount;
}
}
if (zzR) {
// peek one character ahead if it is \n (if we have counted one line too much)
boolean zzPeek;
if (zzMarkedPosL < zzEndReadL)
zzPeek = zzBufferL[zzMarkedPosL] == '\n';
else if (zzAtEOF)
zzPeek = false;
else {
boolean eof = zzRefill();
zzEndReadL = zzEndRead;
zzMarkedPosL = zzMarkedPos;
zzBufferL = zzBuffer;
if (eof)
zzPeek = false;
else
zzPeek = zzBufferL[zzMarkedPosL] == '\n';
}
if (zzPeek) yyline--;
}
zzAction = -1;
zzCurrentPosL = zzCurrentPos = zzStartRead = zzMarkedPosL;
zzState = ZZ_LEXSTATE[zzLexicalState];
// set up zzAction for empty match case:
int zzAttributes = zzAttrL[zzState];
if ( (zzAttributes & 1) == 1 ) {
zzAction = zzState;
}
zzForAction: {
while (true) {
if (zzCurrentPosL < zzEndReadL) {
zzInput = Character.codePointAt(zzBufferL, zzCurrentPosL, zzEndReadL);
zzCurrentPosL += Character.charCount(zzInput);
}
else if (zzAtEOF) {
zzInput = YYEOF;
break zzForAction;
}
else {
// store back cached positions
zzCurrentPos = zzCurrentPosL;
zzMarkedPos = zzMarkedPosL;
boolean eof = zzRefill();
// get translated positions and possibly new buffer
zzCurrentPosL = zzCurrentPos;
zzMarkedPosL = zzMarkedPos;
zzBufferL = zzBuffer;
zzEndReadL = zzEndRead;
if (eof) {
zzInput = YYEOF;
break zzForAction;
}
else {
zzInput = Character.codePointAt(zzBufferL, zzCurrentPosL, zzEndReadL);
zzCurrentPosL += Character.charCount(zzInput);
}
}
int zzNext = zzTransL[ zzRowMapL[zzState] + zzCMapL[zzInput] ];
if (zzNext == -1) break zzForAction;
zzState = zzNext;
zzAttributes = zzAttrL[zzState];
if ( (zzAttributes & 1) == 1 ) {
zzAction = zzState;
zzMarkedPosL = zzCurrentPosL;
if ( (zzAttributes & 8) == 8 ) break zzForAction;
}
}
}
// store back cached position
zzMarkedPos = zzMarkedPosL;
if (zzInput == YYEOF && zzStartRead == zzCurrentPos) {
zzAtEOF = true;
return YYEOF;
}
else {
switch (zzAction < 0 ? zzAction : ZZ_ACTION[zzAction]) {
case 1:
{ System.out.print(yytext());
}
case 3: break;
case 2:
{ System.out.println("***found match");
}
case 4: break;
default:
zzScanError(ZZ_NO_MATCH);
}
}
}
}
/**
* Runs the scanner on input files.
*
* This is a standalone scanner, it will print any unmatched
* text to System.out unchanged.
*
* #param argv the command line, contains the filenames to run
* the scanner on.
*/
public static void main(String argv[]) {
if (argv.length == 0) {
System.out.println("Usage : java Patch [ --encoding <name> ] <inputfile(s)>");
}
else {
int firstFilePos = 0;
String encodingName = "UTF-8";
if (argv[0].equals("--encoding")) {
firstFilePos = 2;
encodingName = argv[1];
try {
java.nio.charset.Charset.forName(encodingName); // Side-effect: is encodingName valid?
} catch (Exception e) {
System.out.println("Invalid encoding '" + encodingName + "'");
return;
}
}
for (int i = firstFilePos; i < argv.length; i++) {
Patch scanner = null;
try {
java.io.FileInputStream stream = new java.io.FileInputStream(argv[i]);
java.io.Reader reader = new java.io.InputStreamReader(stream, encodingName);
scanner = new Patch(reader);
while ( !scanner.zzAtEOF ) scanner.yylex();
}
catch (java.io.FileNotFoundException e) {
System.out.println("File not found : \""+argv[i]+"\"");
}
catch (java.io.IOException e) {
System.out.println("IO error scanning file \""+argv[i]+"\"");
System.out.println(e);
}
catch (Exception e) {
System.out.println("Unexpected exception:");
e.printStackTrace();
}
}
}
}
}
The shell keep giving me the error
Error: Could not find or load main class Patch
when I use the command like:
java Patch SearchText.txt
Besides, there's no error when I run:
javac Patch.java
I tried to set the path explicitly:
java -cp. Patch SearchText.txt
and got something like this:
{\rtf1\ansi\ansicpg1252\cocoartf1348\cocoasubrtf170
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
\f0\fs24 \cf0 a+b+c+d}AmbertekiMacBook-Air:ADS4 amber95$ java Patch SearchText.txt
Error: Could not find or load main class Patch
Have you tried this?
java -cp ./ Patch file.txt
I have written a simple genetic algorithm program in java. What it is does is maximize the decimal value represented by the bits in the chromosome. Somehow mutation is not working as expected, e.g. causing two genes to mutate when just one is to change. The print statements I have included there show which to mutate, but in addition to that some more chromosomes get mutated. I can't figure out what the problem is :-(
Here are my java classes.
Gene.java
public class Gene {
private int value;
public Gene() {
value = Math.random() < 0.5 ? 0 : 1;
}
public Gene(int value) {
if (value != 0 && value != 1) {
throw new IllegalArgumentException("value must be either 0 or 1");
}
else {
this.value = value;
}
}
public void mutate() {
value = 1 - value;
}
public int value() {
return value;
}
#Override
public String toString() {
return String.valueOf(value);
}
}
Chromosome.java
import java.util.ArrayList;
import java.util.List;
public class Chromosome implements Comparable {
private ArrayList<Gene> genes;
private final int chromosomeLength;
public Chromosome(int length) {
this.genes = new ArrayList<>();
this.chromosomeLength = length > 0 ? length : 16;
for (int i = 0; i < chromosomeLength; i++) {
this.genes.add(i, new Gene());
}
}
public List<Gene> getAllele(int fromIndex, int toIndex) {
return new ArrayList<>(genes.subList(fromIndex, toIndex));
}
public void setAllele(int fromIndex, List<Gene> allele) {
int lastIndex = fromIndex + allele.size();
if (lastIndex > chromosomeLength) {
throw new IndexOutOfBoundsException("the allele exceeds beyond the size of the chromosome");
}
for (int i = fromIndex, j = 0; i < lastIndex; i++, j++) {
genes.set(i, allele.get(j));
}
}
public int getChromosomeLength() {
return chromosomeLength;
}
public void setGeneAt(int index, Gene gene) {
genes.set(index, gene);
}
public Gene getGeneAt(int index) {
return genes.get(index);
}
public int value() {
return Integer.parseInt(this.toString(), 2);
}
#Override
public String toString() {
StringBuilder chromosome = new StringBuilder("");
genes.stream().forEach((Gene g) -> chromosome.append(g));
return chromosome.toString();
}
#Override
public int compareTo(Object anotherChromosome) {
Chromosome c = (Chromosome) anotherChromosome;
return this.value() - c.value();
}
}
GenePool.java
import java.util.ArrayList;
import java.util.Arrays;
public class GenePool {
private final ArrayList<Chromosome> genePool;
private final int genePoolSize;
private final int chromosomeLength;
private final double crossOverRate;
private final double mutationRate;
private int[] crossPoints;
public GenePool(int numOfChromosome, int chromosomeLength, double crossOverRate, double mutationRate) {
this.genePoolSize = numOfChromosome;
this.chromosomeLength = chromosomeLength > 0 ? chromosomeLength : 16;
this.crossOverRate = crossOverRate;
this.mutationRate = mutationRate;
crossPoints = new int[1];
crossPoints[0] = this.chromosomeLength / 2;
genePool = new ArrayList<>();
for (int i = 0; i < numOfChromosome; i++) {
genePool.add(new Chromosome(chromosomeLength));
}
}
public int getGenePoolSize() {
return genePoolSize;
}
public Chromosome getChromosomeAt(int index) {
return genePool.get(index);
}
public void setChromosomeAt(int index, Chromosome c) {
genePool.set(index, c);
}
public int getChromosomeLength() {
return chromosomeLength;
}
public Chromosome[] crossOver(Chromosome c1, Chromosome c2) {
Chromosome[] offsprings = new Chromosome[2];
offsprings[0] = new Chromosome(c1.getChromosomeLength());
offsprings[1] = new Chromosome(c1.getChromosomeLength());
Chromosome[] parentChromosomes = {c1, c2};
int selector = 0;
for (int i = 0, start = 0; i <= crossPoints.length; i++) {
int crossPoint = i == crossPoints.length ? c1.getChromosomeLength() : crossPoints[i];
offsprings[0].setAllele(start, parentChromosomes[selector].getAllele(start, crossPoint));
offsprings[1].setAllele(start, parentChromosomes[1 - selector].getAllele(start, crossPoint));
selector = 1 - selector;
start = crossPoint;
}
return offsprings;
}
public void mutateGenePool() {
int totalGeneCount = genePoolSize * chromosomeLength;
System.out.println("Mutating genes:");
for (int i = 0; i < totalGeneCount; i++) {
double prob = Math.random();
if (prob < mutationRate) {
System.out.printf("Chromosome#: %d\tGene#: %d\n", i / chromosomeLength, i % chromosomeLength);
genePool.get(i / chromosomeLength).getGeneAt(i % chromosomeLength).mutate();
}
}
System.out.println("");
}
public int getLeastFitIndex() {
int index = 0;
int min = genePool.get(index).value();
int currentValue;
for (int i = 1; i < genePoolSize; i++) {
currentValue = genePool.get(i).value();
if (currentValue < min) {
index = i;
min = currentValue;
}
}
return index;
}
public void saveFittest(ArrayList<Chromosome> offsprings) {
// sort in ascending order
offsprings.sort(null);
offsprings.stream().forEach((offspring) -> {
int leastFitIndex = getLeastFitIndex();
if (offspring.value() > genePool.get(leastFitIndex).value()) {
genePool.set(leastFitIndex, offspring);
}
});
}
public void evolve(int noOfGeneration) {
for (int generation = 1; generation <= noOfGeneration; generation++) {
System.out.println("Generation :" + generation);
ArrayList<Integer> selection = new ArrayList<>();
for (int i = 0; i < genePoolSize; i++) {
if (Math.random() <= crossOverRate) {
selection.add(i);
}
}
if (selection.size() % 2 == 1) {
selection.remove(selection.size() - 1);
}
ArrayList<Chromosome> offsprings = new ArrayList<>();
for (int i = 0; i < selection.size(); i += 2) {
int index1 = selection.get(i);
int index2 = selection.get(i + 1);
offsprings.addAll(Arrays.asList(crossOver(genePool.get(index1), genePool.get(index2))));
}
System.out.println("Before saving the offsprings");
displayChromosomes(genePool, "GenePool");
displayChromosomes(offsprings, "Offsprings");
saveFittest(offsprings);
System.out.println("Before mutation:");
displayChromosomes(genePool, "GenePool");
mutateGenePool();
System.out.println("After mutation:");
displayChromosomes(genePool, "GenePool");
System.out.println("\n\n");
}
}
public void displayChromosomes(ArrayList<Chromosome> geneList, String name) {
System.out.println(name);
if (geneList.isEmpty()) {
System.out.println("Empty list");
}
geneList.stream().forEach((c) -> {
System.out.println(c + " -> " + c.value());
});
System.out.println("");
}
}
GADemo.java
public class GADemo {
public static void main(String[] args) {
GenePool gp = new GenePool(6, 8, 0.25, 0.01);
gp.evolve(10);
}
}
After evolving for a number of generations, the chromosomes all tend to become exactly the same, or very similar. And the problem is that that value is not the maximum for that many bits, and sometimes even a small value. For example, for 8 bits the values should (tend to) approach 255, but this doesn't do so in my code. Someone please provide a hint where/how to look for and solve the problem.
Focus on these lines and imagine the references. These are from setAllele()
for (int i = fromIndex, j = 0; i < lastIndex; i++, j++) {
genes.set(i, allele.get(j));
}
You are basically copying the reference from one onto the other. They are the same Gene so whatever mutation you do on those genes, will also affect even other Chromosomes.
You must produce a deep copy here.
Initially each chromosome has an own list of genes. But when you do the crossover operation you set gene objects from one chromosome into the gene list of other chromosome.
When you evolve the system, the number of shared genes will rise and therefore ultimately all chromosomes will share the same genes. No matter how you mutate a gene the chromosomes are not affected.
EDIT:
As Incognito also answered the setAllele method seems to be the culprit where gene sharing starts. You may want to introduce a method in the gene class where you can set its value given another gene.
I am trying to override the read method of FilterReader in Java. My goal is to substitute one run of characters for another. I have accomplished this, but the code fails to stop and eventually the jvm runs out of memory.
Here is my code so far.
public class Replacer extends FilterReader {
/** string to be replaced */
String find;
/** string that is inserted in place of string find */
String replace;
/** List functioning as a buffer, holds characters
* while checking if we have a match */
LinkedList<Character> buffer;
/**
* Constructs a Replacer with the string to be replaced,
* find, and the string it is replaced with, replace.
*
* #param find the string to be replaced
* #param replace the string to replace find with
* #param in a Reader Object
*/
public Replacer(String find, String replace, Reader in) {
super(in);
this.find = find;
this.replace = replace;
buffer = new LinkedList<Character>();
}
#Override
public int read() throws IOException {
if (this.buffer.size() > 0) return (int)buffer.poll();
boolean isMatch = true;
char x = (char)super.read();
for (int i=0; i < find.length(); i++) {
if (x != this.find.charAt(i)) {
isMatch = false;
this.buffer.add(x);
break;
} else {
this.buffer.add(x);
}
x = (char)super.read();
}
if(isMatch) {
buffer.clear();
for(int i=0; i < replace.length(); i++) {
this.buffer.add(replace.charAt(i));
}
this.buffer.add(x);
}
return (int)buffer.poll();
}
#Override
public int read(char[] b, int off, int len) throws IOException {
int result = -1;
int x = -1;
for (int i = off; i < len; i++) {
x = read();
if (x == -1) {
result = -1;
break;
}
b[i] = (char)x;
result++;
}
return result;
}
}
Here is the test code to run
public class Test {
public static void main(String[] args) throws IOException{
URL mun = new URL("http://www.mun.ca/");
BufferedReader in = new BufferedReader( new Replacer
("head", "balls", new InputStreamReader(mun.openStream())));
String inputLine;
while((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}
}
Any help would be greatly appreciated.
There are lots of components out there creating/parsing barcode images but i could not manage to find a library which parses a EAN-128 barcode-string and gives me simply a java-pojo object from which I can get EAN-128 groups if they were included in the barcode.
Example pseudocode:
EAN128Pojo pojo = EAN128Pojo.parse(some string got from scanner);
Date dueDate = pojo.getDueDate();
or
Object dueDate = pojo.get("12" /*application identifier for due date*/);
Is there any library capable of this?
I don't know of any, and neither does Google CodeSearch: http://www.google.com/codesearch?q=getAdditionalProductIdentification
Anyway, writing your own isn't that difficult. This one took me less than an hour:
package so5685964;
import java.util.Map;
import org.joda.time.DateMidnight;
import com.google.common.collect.Maps;
public class GS1Code128Data {
/** Maps the AI to the corresponding data from the barcode. */
private final Map<String, String> data = Maps.newHashMap();
private static final Map<String, AII> aiinfo = Maps.newHashMap();
static class AII {
final int minLength;
final int maxLength;
public AII(String id, int minLength, int maxLength) {
this.minLength = minLength;
this.maxLength = maxLength;
}
}
private static void ai(String id, int minLength, int maxLength) {
aiinfo.put(id, new AII(id, minLength, maxLength));
}
private static void ai(String id, int length) {
aiinfo.put(id, new AII(id, length, length));
}
static {
ai("00", 18, 18);
ai("01", 14);
ai("02", 14);
ai("10", 1, 20);
ai("11", 6);
ai("12", 6);
// TODO: continue according to http://en.wikipedia.org/wiki/GS1-128
}
/**
* Decodes a Unicode string from a Code128-like encoding.
*
* #param fnc1 The character that represents FNC1.
*/
public GS1Code128Data(String s, char fnc1) {
StringBuilder ai = new StringBuilder();
int index = 0;
while (index < s.length()) {
ai.append(s.charAt(index++));
AII info = aiinfo.get(ai.toString());
if (info != null) {
StringBuilder value = new StringBuilder();
for (int i = 0; i < info.maxLength && index < s.length(); i++) {
char c = s.charAt(index++);
if (c == fnc1) {
break;
}
value.append(c);
}
if (value.length() < info.minLength) {
throw new IllegalArgumentException("Short field for AI \"" + ai + "\": \"" + value + "\".");
}
data.put(ai.toString(), value.toString());
ai.setLength(0);
}
}
if (ai.length() > 0) {
throw new IllegalArgumentException("Unknown AI \"" + ai + "\".");
}
}
private static DateMidnight asDate(String s) {
if (s == null) {
return null;
}
String century = s.compareTo("500000") < 0 ? "20" : "19";
return new DateMidnight(century + s);
}
public DateMidnight getDueDate() {
return asDate(data.get("12"));
}
}
And some demonstration code:
package so5685964;
public class BarcodeDemo {
public static void main(String[] args) {
String barcode = "12110416";
GS1Code128Data data = new GS1Code128Data(barcode, '\f');
System.out.println(data.getDueDate());
}
}
When you assume that your input is already a String, pay attention to encoding issues. The FNC1 code does not have a corresponding Unicode Code Point, so it has to be encoded in some other way.
The GS1 Barcode Syntax Engine is a native library with a Java binding that is purpose built for processing GS1 Application Identifier syntax.
https://github.com/gs1/gs1-syntax-engine