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