1 package com.quadcap.sql.types; 2 3 40 41 import java.io.IOException ; 42 import java.io.OutputStream ; 43 44 import com.quadcap.util.Debug; 45 import com.quadcap.util.Util; 46 47 52 public final class KeyStream { 53 public static final int COMPOUND = 0; 54 public static final int INTEGER = 1; 55 public static final int STRING = 2; 56 public static final int NULL = 3; 57 58 OutputStream out; 59 boolean caseSensitive = false; 60 61 public KeyStream(OutputStream out, boolean caseSensitive) { 62 this.out = out; 63 this.caseSensitive = caseSensitive; 64 } 65 66 final void writeLength(int len) throws IOException { 67 writeLength(len, STRING); 68 } 69 70 final void writeLength(int len, int type) throws IOException { 71 if (len <= 31) { 73 out.write((len << 2) | type); 74 } else { 75 out.write(0x80 | ((len & 0x1f00) >> 6) | type); 76 out.write(len & 0xff); 77 } 78 } 79 80 public final void writeNull(boolean last) throws IOException { 81 int len = last ? 1 : 0; 83 out.write((len << 2) | NULL); 84 } 85 86 public final void write(int b) throws IOException { 87 writeByte((byte)b); 89 } 90 91 public final void write(byte[] b) throws IOException { 92 writeLength(b.length); 93 for (int i = 0; i < b.length; i++) { 94 out.write(b[i]); 95 } 96 } 97 98 public final void writeInt(byte[] b) throws IOException { 99 writeLength(b.length, INTEGER); 101 for (int i = 0; i < b.length; i++) { 102 out.write(b[i]); 103 } 104 } 105 106 public final void write(byte b[], int off, int len) throws IOException { 107 writeLength(len); 109 for (int i = 0; i < len; i++) { 110 out.write(b[off + i]); 111 } 112 } 113 114 public final void writeBoolean(boolean val) throws IOException { 115 write(val ? 1 : 0); 117 } 118 119 public final void writeByte(byte val) throws IOException { 120 out.write((1 << 2) | INTEGER); 122 out.write(val); 123 } 124 125 130 131 public final void writeShort(short val) throws IOException { 132 if (val == (byte)val) { 134 writeByte((byte)val); 135 } else { 136 out.write((2 << 2) | INTEGER); 137 out.write((val >> 8) & 0xff); 138 out.write(val & 0xff); 139 } 140 } 141 142 public final void writeInt(int val) throws IOException { 143 if (val == (short)val) { 145 writeShort((short)val); 146 } else { 147 out.write((4 << 2) | INTEGER); 148 out.write((val >> 24) & 0xff); 149 out.write((val >> 16) & 0xff); 150 out.write((val >> 8) & 0xff); 151 out.write(val & 0xff); 152 } 153 } 154 155 public final void writeLong(long val) throws IOException { 156 if (val == (int)val) { 158 writeInt((int)val); 159 } else { 160 out.write((8 << 2) | INTEGER); 161 out.write((int)((val >> 56) & 0xff)); 162 out.write((int)((val >> 48) & 0xff)); 163 out.write((int)((val >> 40) & 0xff)); 164 out.write((int)((val >> 32) & 0xff)); 165 out.write((int)((val >> 24) & 0xff)); 166 out.write((int)((val >> 16) & 0xff)); 167 out.write((int)((val >> 8) & 0xff)); 168 out.write((int)(val & 0xff)); 169 } 170 } 171 172 public final void writeFloat(float f) throws IOException { 173 writeInt(Float.floatToIntBits(f)); 175 } 176 177 public final void writeDouble(double d) throws IOException { 178 writeLong(Double.doubleToLongBits(d)); 180 } 181 182 public final void writeChars(String s) throws IOException { 183 final int len = s.length(); 184 writeLength(len * 2); 185 if (caseSensitive) { 186 for (int i = 0; i < len; i++) { 187 final char c = s.charAt(i); 188 out.write((c >> 8) & 0xff); 189 out.write(c & 0xff); 190 } 191 } else { 192 for (int i = 0; i < len; i++) { 193 final char c = Character.toLowerCase(s.charAt(i)); 194 out.write((c >> 8) & 0xff); 195 out.write(c & 0xff); 196 } 197 } 198 } 199 } 200 | Popular Tags |