AligningOutput.java 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* AligningOutput.java --
  2. Copyright (C) 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package gnu.CORBA.CDR;
  32. import java.io.ByteArrayOutputStream;
  33. import org.omg.CORBA.BAD_PARAM;
  34. /**
  35. * The input stream with the possibility to align on the
  36. * word (arbitrary size) boundary.
  37. *
  38. * @author Audrius Meskauskas (AudriusA@Bioinformatics.org)
  39. */
  40. public class AligningOutput
  41. extends ByteArrayOutputStream
  42. {
  43. /**
  44. * The alignment offset.
  45. */
  46. private int offset = 0;
  47. /**
  48. * Create a stream with the default intial buffer size.
  49. */
  50. public AligningOutput()
  51. {
  52. }
  53. /**
  54. * Create a stream with the given intial buffer size.
  55. */
  56. public AligningOutput(int initial_size)
  57. {
  58. super(initial_size);
  59. }
  60. /**
  61. * Set the alignment offset, if the index of the first byte in the
  62. * stream is different from 0.
  63. */
  64. public void setOffset(int an_offset)
  65. {
  66. offset = an_offset;
  67. }
  68. /**
  69. * Skip several bytes, aligning the internal pointer on the
  70. * selected boundary.
  71. *
  72. * @throws BAD_PARAM, minor code 0, the alignment is not possible,
  73. * usually due the wrong parameter value.
  74. */
  75. public void align(int alignment)
  76. {
  77. try
  78. {
  79. int d = (count + offset) % alignment;
  80. if (d > 0)
  81. {
  82. skip(alignment - d);
  83. }
  84. }
  85. catch (Exception ex)
  86. {
  87. BAD_PARAM p = new BAD_PARAM("Unable to align at " + alignment);
  88. p.initCause(ex);
  89. throw p;
  90. }
  91. }
  92. /**
  93. * Write the specified number of zero bytes.
  94. *
  95. * @param bytes the number of zero bytes to write.
  96. */
  97. public void skip(int bytes)
  98. {
  99. for (int i = 0; i < bytes; i++)
  100. {
  101. write(0);
  102. }
  103. }
  104. /**
  105. * Get the current position in the buffer.
  106. *
  107. * @return The position in the buffer, taking offset into consideration.
  108. */
  109. public int getPosition()
  110. {
  111. return size()+offset;
  112. }
  113. /**
  114. * Seek to the given position (not in use).
  115. */
  116. public void seek(int position)
  117. {
  118. count = position - offset;
  119. }
  120. /**
  121. * Get the buffer without copying it. Use with care.
  122. */
  123. public byte[] getBuffer()
  124. {
  125. return buf;
  126. }
  127. }