Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cap error output to avoid running out of memory
- Loading branch information
1 parent
6caec8f
commit 80b0ad5
Showing
6 changed files
with
206 additions
and
5 deletions.
There are no files selected for viewing
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
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
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
93 changes: 93 additions & 0 deletions
93
...eclipsefp.haskell.util.test/src/net/sf/eclipsefp/haskell/util/CappedStringWriterTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
/** | ||
* Copyright (c) 2013 by JP Moresmau | ||
* This code is made available under the terms of the Eclipse Public License, | ||
* version 1.0 (EPL). See http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package net.sf.eclipsefp.haskell.util; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.io.IOException; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
|
||
/** | ||
* Tests for CappedStringWriter | ||
* @author JP Moresmau | ||
* | ||
*/ | ||
public class CappedStringWriterTest { | ||
|
||
@Test | ||
public void testNormal() throws IOException{ | ||
CappedStringWriter w=new CappedStringWriter(100); | ||
w.write("toto"); | ||
w.write("titi"); | ||
Assert.assertEquals("tototiti", w.toString()); | ||
} | ||
|
||
@Test | ||
public void testClear() throws IOException{ | ||
CappedStringWriter w=new CappedStringWriter(4); | ||
w.write("toto"); | ||
w.clear(); | ||
w.write("titi"); | ||
Assert.assertEquals("titi", w.toString()); | ||
w=new CappedStringWriter(4); | ||
w.write("toto"); | ||
w.clear(); | ||
w.write("tutu"); | ||
w.write("titi"); | ||
Assert.assertEquals("titi", w.toString()); | ||
} | ||
|
||
|
||
@Test | ||
public void testNull() throws IOException{ | ||
CappedStringWriter w=new CappedStringWriter(100); | ||
w.write("toto"); | ||
w.write((String)null); | ||
assertEquals("toto", w.toString()); | ||
} | ||
|
||
@Test | ||
public void testLimit() throws IOException{ | ||
CappedStringWriter w=new CappedStringWriter(4); | ||
w.write("toto"); | ||
w.write("titi"); | ||
assertEquals("titi", w.toString()); | ||
} | ||
|
||
@Test | ||
public void testLimitMiddle() throws IOException{ | ||
CappedStringWriter w=new CappedStringWriter(6); | ||
w.write("toto"); | ||
w.write("titi"); | ||
assertEquals("titi", w.toString()); | ||
} | ||
|
||
@Test | ||
public void testLimitMiddleAll() throws IOException{ | ||
CappedStringWriter w=new CappedStringWriter(2); | ||
w.write("toto"); | ||
w.write("titi"); | ||
assertEquals("", w.toString()); | ||
} | ||
|
||
@Test | ||
public void testPerf() throws IOException{ | ||
CappedStringWriter w=new CappedStringWriter(100); | ||
StringBuilder sb=new StringBuilder(); | ||
for (int a=0;a<100;a++){ | ||
sb.append("a"); | ||
} | ||
String s=sb.toString(); | ||
assertEquals(100, s.length()); | ||
for (int a=0;a<10000;a++){ | ||
w.write(s); | ||
} | ||
assertEquals(s,w.toString()); | ||
} | ||
} |
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
107 changes: 107 additions & 0 deletions
107
net.sf.eclipsefp.haskell.util/src/net/sf/eclipsefp/haskell/util/CappedStringWriter.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/** | ||
* Copyright (c) 2013 by JP Moresmau | ||
* This code is made available under the terms of the Eclipse Public License, | ||
* version 1.0 (EPL). See http://www.eclipse.org/legal/epl-v10.html | ||
*/ | ||
package net.sf.eclipsefp.haskell.util; | ||
|
||
import java.io.IOException; | ||
import java.io.Writer; | ||
import java.util.LinkedList; | ||
|
||
/** | ||
* A string writer that only keeps n max characters, discarding the first received characters | ||
* @author JP Moresmau | ||
* | ||
*/ | ||
public class CappedStringWriter extends Writer { | ||
private int maxSize; | ||
private LinkedList<String> ls=new LinkedList<String>(); | ||
private int size; | ||
|
||
public CappedStringWriter(int max){ | ||
maxSize=max; | ||
lock=ls; | ||
} | ||
|
||
|
||
/* (non-Javadoc) | ||
* @see java.io.Writer#write(char[], int, int) | ||
*/ | ||
@Override | ||
public void write(char[] cbuf, int off, int len) throws IOException { | ||
synchronized (lock) { | ||
ls.add(new String(cbuf,off,len)); | ||
size+=len; | ||
checkSize(); | ||
} | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see java.io.Writer#write(java.lang.String) | ||
*/ | ||
@Override | ||
public void write(String str) throws IOException { | ||
synchronized (lock) { | ||
if (str!=null){ | ||
ls.add(str); | ||
size+=str.length(); | ||
checkSize(); | ||
} | ||
} | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see java.io.Writer#write(java.lang.String, int, int) | ||
*/ | ||
@Override | ||
public void write(String str, int off, int len) throws IOException { | ||
write(str.substring(off, off+len)); | ||
} | ||
|
||
private void checkSize(){ | ||
while (size>maxSize){ | ||
String s=ls.removeFirst(); | ||
size-=s.length(); | ||
} | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see java.io.Writer#flush() | ||
*/ | ||
@Override | ||
public void flush() throws IOException { | ||
// noop | ||
} | ||
|
||
/* (non-Javadoc) | ||
* @see java.io.Writer#close() | ||
*/ | ||
@Override | ||
public void close() throws IOException { | ||
// noop | ||
|
||
} | ||
|
||
/* (non-Javadoc) | ||
* @see java.lang.Object#toString() | ||
*/ | ||
@Override | ||
public String toString() { | ||
synchronized (lock) { | ||
StringBuilder sb=new StringBuilder(size); | ||
for (String s:ls){ | ||
sb.append(s); | ||
} | ||
return sb.toString(); | ||
} | ||
} | ||
|
||
public void clear(){ | ||
synchronized (lock) { | ||
ls.clear(); | ||
size=0; | ||
} | ||
|
||
} | ||
} |