PageRenderTime 149ms CodeModel.GetById 30ms RepoModel.GetById 8ms app.codeStats 0ms

/jEdit/tags/jedit-4-3-pre5/de/masters_of_disaster/ant/tasks/ar/ArUtils.java

#
Java | 155 lines | 76 code | 23 blank | 56 comment | 18 complexity | 5f5578f26d7a3a18b921c522916a8765 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. package de.masters_of_disaster.ant.tasks.ar;
  2. /**
  3. * This class provides static utility methods to work with byte streams.
  4. */
  5. public class ArUtils {
  6. /**
  7. * Parse an octal string from a header buffer. This is used for the
  8. * file permission mode value.
  9. *
  10. * @param header The header buffer from which to parse.
  11. * @param offset The offset into the buffer from which to parse.
  12. * @param length The number of header bytes to parse.
  13. * @return The long value of the octal string.
  14. */
  15. public static long parseOctal(byte[] header, int offset, int length) {
  16. long result = 0;
  17. int end = offset + length;
  18. for (int i=offset ; i<end ; i++) {
  19. if (header[i] == (byte) ' ') {
  20. break;
  21. }
  22. result = (result << 3) + (header[i] - '0');
  23. }
  24. return result;
  25. }
  26. /**
  27. * Parse an entry name from a header buffer.
  28. *
  29. * @param header The header buffer from which to parse.
  30. * @param offset The offset into the buffer from which to parse.
  31. * @param length The number of header bytes to parse.
  32. * @return The header's entry name.
  33. */
  34. public static StringBuffer parseName(byte[] header, int offset, int length) {
  35. StringBuffer result = new StringBuffer(length);
  36. int end = offset + length;
  37. for (int i=offset ; i<end ; i++) {
  38. if (header[i] == ' ') {
  39. break;
  40. }
  41. result.append((char) header[i]);
  42. }
  43. return result;
  44. }
  45. /**
  46. * Write a name into a byte array.
  47. *
  48. * @param name The name to write.
  49. * @param buf The byte array into which to write.
  50. * @param offset The offset into the buffer from which to write.
  51. * @param length The number of header bytes to write.
  52. * @return The number of bytes written to the buffer.
  53. */
  54. public static int getNameBytes(StringBuffer name, byte[] buf, int offset, int length) {
  55. int i;
  56. int c = name.length();
  57. for (i=0 ; i<length && i<c ; i++) {
  58. buf[offset+i] = (byte) name.charAt(i);
  59. }
  60. while (i<length) {
  61. buf[offset+i] = (byte) ' ';
  62. i++;
  63. }
  64. return offset + length;
  65. }
  66. /**
  67. * Write a long value into a byte array.
  68. *
  69. * @param value The value to write.
  70. * @param buf The byte array into which to write.
  71. * @param offset The offset into the buffer from which to write.
  72. * @param length The number of header bytes to write.
  73. * @return The number of bytes written to the buffer.
  74. */
  75. public static int getLongBytes(long value, byte[] buf, int offset, int length) {
  76. int i;
  77. String tmp = Long.toString(value);
  78. int c = tmp.length();
  79. for (i=0 ; i<length && i<c ; i++) {
  80. buf[offset+i] = (byte) tmp.charAt(i);
  81. }
  82. while (i<length) {
  83. buf[offset+i] = (byte) ' ';
  84. i++;
  85. }
  86. return offset + length;
  87. }
  88. /**
  89. * Write an int value into a byte array.
  90. *
  91. * @param value The value to write.
  92. * @param buf The byte array into which to write.
  93. * @param offset The offset into the buffer from which to write.
  94. * @param length The number of header bytes to write.
  95. * @return The number of bytes written to the buffer.
  96. */
  97. public static int getIntegerBytes(int value, byte[] buf, int offset, int length) {
  98. int i;
  99. String tmp = Integer.toString(value);
  100. int c = tmp.length();
  101. for (i=0 ; i<length && i<c ; i++) {
  102. buf[offset+i] = (byte) tmp.charAt(i);
  103. }
  104. while (i<length) {
  105. buf[offset+i] = (byte) ' ';
  106. i++;
  107. }
  108. return offset + length;
  109. }
  110. /**
  111. * Write an octal value into a byte array.
  112. *
  113. * @param value The value to write.
  114. * @param buf The byte array into which to write.
  115. * @param offset The offset into the buffer from which to write.
  116. * @param length The number of header bytes to write.
  117. * @return The number of bytes written to the buffer.
  118. */
  119. public static int getOctalBytes(long value, byte[] buf, int offset, int length) {
  120. int i;
  121. String tmp = Long.toOctalString(value);
  122. int c = tmp.length();
  123. for (i=0 ; i<length && i<c ; i++) {
  124. buf[offset+i] = (byte) tmp.charAt(i);
  125. }
  126. while (i<length) {
  127. buf[offset+i] = (byte) ' ';
  128. i++;
  129. }
  130. return offset + length;
  131. }
  132. }