0

I have a class CustomBuffer that implements Appendable, CharSequence and extends Buffer. It uses a charBuffer to access elements. Since it takes some time in computation I am planning to replace it with a StringBuilder. Unfortunately my current class uses methods like position, flip from Buffer class, which is not available in StringBuilder class. In order to use those I have two options,

  1. StringBuilder extend Buffer (I am not sure whether its a right option as I have not used Buffers much)
  2. Otherwise use the methods I mentioned my modifying the resources from StringBuilder.

I am providing sample code.

Current scenario:

public void write(Object object, CustomBuffer cba)
            throws Exception {
     CustomBuffer cb = new CustomBuffer();
     String str = getString(cb.position(), (String)object.size());
     cb.flip();
     cba.append(str);
     cba.append(cb.toString());
   }

If I have to replace with StringBuilder how can I reproduce the same effect. Definitely I cannot provide the index to get position according to the structure of the code, it would be great if I get some help, thanks!

yellow
  • 425
  • 7
  • 20
  • Why are you managing so low levels char maniplations ? What the problem you want to solve? – Woody Sep 09 '16 at 16:06
  • I have computation time to be very high for using CustomClass, I want to replace it using existing library classes and reduce time. – yellow Sep 09 '16 at 16:10
  • If you're just appending char or strings you should use StringBuilder. But I have not a clue of what you're doing with your CustomBuffer. Why do you need extends Buffer and what is the role of your CustomBuffer class ? – Woody Sep 09 '16 at 16:17
  • This method is a simple one, apart from this CustomBuffer is used to iterate over different levels of strings one within other, it consists of supporting methods for such actions. – yellow Sep 09 '16 at 16:19
  • That method makes no sense at all. The freshly created buffer should have the initial position of `0`, so `cb.position()` can be replaced by `0`. Then, your doing `(String)object.size()`… really? So `Object` is not `java.lang.Object` and it has a `size()` method that returns something that can be cast to `String`? Great, but what’s the point of the subsequent `cb.flip()`? Up to that point, you didn’t do anything with `cb` besides asking for its (initial) position. Unless some unspecified magic happens, on the last `cba.append(cb.toString());`, `cb` still is in its initial empty state. – Holger Dec 09 '16 at 15:50
  • So the entire method can be reduced to `cba.append(getString(0, (String)object.size()));`, whatever that means to the specified `Object` argument… – Holger Dec 09 '16 at 15:52

0 Answers0