PageRenderTime 24ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/jEdit/tags/jedit-4-2-pre14/org/gjt/sp/util/SegmentBuffer.java

#
Java | 70 lines | 32 code | 8 blank | 30 comment | 4 complexity | 45f66a2c55fbea1be2dd3e4e19ab78cd MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
  1. /*
  2. * SegmentBuffer.java - A Segment you can append stuff to
  3. * :tabSize=8:indentSize=8:noTabs=false:
  4. * :folding=explicit:collapseFolds=1:
  5. *
  6. * Copyright (C) 2001 Slava Pestov
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License
  10. * as published by the Free Software Foundation; either version 2
  11. * of the License, or any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. package org.gjt.sp.util;
  23. import javax.swing.text.Segment;
  24. /**
  25. * An extended segment that you can append text to.
  26. */
  27. public class SegmentBuffer extends Segment
  28. {
  29. //{{{ SegmentBuffer constructor
  30. public SegmentBuffer(int capacity)
  31. {
  32. ensureCapacity(capacity);
  33. } //}}}
  34. //{{{ append() method
  35. public void append(char ch)
  36. {
  37. ensureCapacity(count + 1);
  38. array[offset + count] = ch;
  39. count++;
  40. } //}}}
  41. //{{{ append() method
  42. public void append(char[] text, int off, int len)
  43. {
  44. ensureCapacity(count + len);
  45. System.arraycopy(text,off,array,count,len);
  46. count += len;
  47. } //}}}
  48. //{{{ Private members
  49. //{{{ ensureCapacity() method
  50. private void ensureCapacity(int capacity)
  51. {
  52. if(array == null)
  53. array = new char[capacity];
  54. else if(capacity >= array.length)
  55. {
  56. char[] arrayN = new char[capacity * 2];
  57. System.arraycopy(array,0,arrayN,0,count);
  58. array = arrayN;
  59. }
  60. } //}}}
  61. //}}}
  62. }