This is an aside but your question gave rise to an idea.
The cost of creating "printed" Strings can be avoided
Premise is, creation of Strings could largely be avoided if it were not
for things like toString and System.out.println() and Logger.debug(Object)
An extended and perhaps more efficient api might be.
Object {
public void toString(PrintVisitor)
public String toString() {
ToStringVisitor visitor = new ToStringVisitor()
toString(visitor);
return visitor.toString()
}
}
FooBar {
public void toString(PrintVisitor) {
visitor.visit("foo:").visit(foo); // "foo".accept(visitor)
visitor.visit(',')
visitor.visit("bar:").visit(bar);
}
}
Printer implements PrintVisitor { // could be PrintStream
public void print(Printable) throws IOException {
printable.accept(this)
}
...
public void visit(char[]) throws IOException {
print(chars)
}
...
public void print(char[]) throws IOException {
out.print(chars)
}
}
In your case creating the String can be avoided (see below for further
details)
IoBuffer {
public void toHex(PrintVisitor, int max) {
chars[] chars;
while (check max && (chars = ...) != null) {
visitor.visit(chars);
}
}
}
* Full concept/outline below
PrintVisitor {
public Visitor visit(boolean) throws IOException
public Visitor visit(byte) throws IOException
public Visitor visit(byte[]) throws IOException
public Visitor visit(byte[], int, int) throws IOException
public Visitor visit(char) throws IOException
public Visitor visit(char[]) throws IOException
public Visitor visit(char[], int, int) throws IOException
public Visitor visit(double) throws IOException
public Visitor visit(float) throws IOException
public Visitor visit(int) throws IOException
public Visitor visit(long) throws IOException
public Visitor visit(short) throws IOException
public Visitor visit(String) throws IOException
public Visitor visit(Object) throws IOException
//visitln, visitf
}
Printer implements PrintVisitor { // could be PrintStream
public void print(Printable) throws IOException {
printable.accept(this)
}
...
public void visit(char[]) throws IOException {
print(chars)
}
...
public void print(char[]) throws IOException {
out.print(chars)
}
}
Printable {
public void accept(Visitor visitor) throws IOException
}
Stream implements Printable {
public void accept(Visitor visitor) throws IOException {
chars[] chars;
while ((chars = ...) != null) {
visitor.visit(chars);
}
}
}
Main {
System.out.println(new FooBar())
}
Object {
public String toString()
}
FooBar {
public String toString() {
StringBuilder b = new StringBuilder()
b.append("foo:").append(foo);
b.append(',')
b.append("bar:").append(bar);
return b.toString()
}
}
vs.
Object {
public void toString(PrintVisitor)
public String toString() {
ToStringVisitor visitor = new ToStringVisitor()
toString(visitor);
return visitor.toString()
}
}
FooBar {
public void toString(PrintVisitor) {
visitor.visit("foo:").visit(foo); // "foo".accept(visitor)
visitor.visit(',')
visitor.visit("bar:").visit(bar);
}
}
IoBuffer {
public void toHex(PrintVisitor, int max) {
chars[] chars;
while (check max && (chars = ...) != null) {
visitor.visit(chars);
}
}
}
On 3/22/2012 12:50 PM, Emmanuel Lecharny wrote:
Le 22 mars 2012 19:49, "David R Robison"<[email protected]>
a écrit :
Yes, but I'm seeing a OutOfMemory exception when calling this routine.
again, the OOM is not due to the Integer.MAX_VALUE, but to the size of your
buffer. Also you can call the method with an extra parameter to limit the
size of what is being dumped. Just use it.
Any thoughts about reducing that to Short.MAX_VALUE? I assume that this
dump is for debug purposes only. Thanks, David
On 3/22/2012 2:29 PM, Emmanuel Lécharny wrote:
Le 3/22/12 6:51 PM, David R Robison a écrit :
The getHexDump function in AbstractIoBuffer is
/**
* {@inheritDoc}
*/
@Override
public String getHexDump() {
return this.getHexDump(Integer.MAX_VALUE);
}
I am wondering if maybe Integer.MAX_VALUE should be changed to
Short.MAX_VALUE to save on heap space. Any thoughts? David
This is just a configuration parameter. It will be used only if the
ByteBuffer to dump is bigger than Integer.MAX_VALUE, something that is
*very* unlikely to happen...
--
David R Robison
Open Roads Consulting, Inc.
103 Watson Road, Chesapeake, VA 23320
phone: (757) 546-3401
e-mail: [email protected]
web: http://openroadsconsulting.com
blog: http://therobe.blogspot.com
book:
http://www.xulonpress.com/bookstore/bookdetail.php?PB_ISBN=9781597816526
This email communication (including any attachments) may contain
confidential and/or privileged material intended solely for the individual
or entity to which it is addressed.
If you are not the intended recipient, please delete this email
immediately.