enjoy 3.3 release ^_^
This commit is contained in:
126
src/main/java/com/jfinal/template/io/IntegerWriter.java
Normal file
126
src/main/java/com/jfinal/template/io/IntegerWriter.java
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*/
|
||||
|
||||
package com.jfinal.template.io;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class IntegerWriter {
|
||||
|
||||
final static int [] sizeTable = { 9, 99, 999, 9999, 99999, 999999, 9999999,
|
||||
99999999, 999999999, Integer.MAX_VALUE };
|
||||
|
||||
final static char [] DigitOnes = {
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
|
||||
} ;
|
||||
|
||||
final static char [] DigitTens = {
|
||||
'0', '0', '0', '0', '0', '0', '0', '0', '0', '0',
|
||||
'1', '1', '1', '1', '1', '1', '1', '1', '1', '1',
|
||||
'2', '2', '2', '2', '2', '2', '2', '2', '2', '2',
|
||||
'3', '3', '3', '3', '3', '3', '3', '3', '3', '3',
|
||||
'4', '4', '4', '4', '4', '4', '4', '4', '4', '4',
|
||||
'5', '5', '5', '5', '5', '5', '5', '5', '5', '5',
|
||||
'6', '6', '6', '6', '6', '6', '6', '6', '6', '6',
|
||||
'7', '7', '7', '7', '7', '7', '7', '7', '7', '7',
|
||||
'8', '8', '8', '8', '8', '8', '8', '8', '8', '8',
|
||||
'9', '9', '9', '9', '9', '9', '9', '9', '9', '9',
|
||||
} ;
|
||||
|
||||
final static char[] digits = {
|
||||
'0' , '1' , '2' , '3' , '4' , '5' ,
|
||||
'6' , '7' , '8' , '9' , 'a' , 'b' ,
|
||||
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
|
||||
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
|
||||
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
|
||||
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
|
||||
};
|
||||
|
||||
private static final byte[] minValueBytes = "-2147483648".getBytes();
|
||||
private static final char[] minValueChars = "-2147483648".toCharArray();
|
||||
|
||||
public static void write(ByteWriter byteWriter, int i) throws IOException {
|
||||
if (i == Integer.MIN_VALUE) {
|
||||
byteWriter.out.write(minValueBytes, 0, minValueBytes.length);
|
||||
return ;
|
||||
}
|
||||
|
||||
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
|
||||
char[] chars = byteWriter.chars;
|
||||
byte[] bytes = byteWriter.bytes;
|
||||
getChars(i, size, chars);
|
||||
|
||||
// int len = Utf8Encoder.me.encode(chars, 0, size, bytes);
|
||||
// byteWriter.out.write(bytes, 0, len);
|
||||
|
||||
for (int j=0; j<size; j++) {
|
||||
bytes[j] = (byte)chars[j];
|
||||
}
|
||||
byteWriter.out.write(bytes, 0, size);
|
||||
}
|
||||
|
||||
public static void write(CharWriter charWriter, int i) throws IOException {
|
||||
if (i == Integer.MIN_VALUE) {
|
||||
charWriter.out.write(minValueChars, 0, minValueChars.length);
|
||||
return ;
|
||||
}
|
||||
|
||||
int size = (i < 0) ? stringSize(-i) + 1 : stringSize(i);
|
||||
char[] chars = charWriter.chars;
|
||||
getChars(i, size, chars);
|
||||
charWriter.out.write(chars, 0, size);
|
||||
}
|
||||
|
||||
static int stringSize(int x) {
|
||||
for (int i=0; ; i++)
|
||||
if (x <= sizeTable[i])
|
||||
return i+1;
|
||||
}
|
||||
|
||||
static void getChars(int i, int index, char[] buf) {
|
||||
int q, r;
|
||||
int charPos = index;
|
||||
char sign = 0;
|
||||
|
||||
if (i < 0) {
|
||||
sign = '-';
|
||||
i = -i;
|
||||
}
|
||||
|
||||
// Generate two digits per iteration
|
||||
while (i >= 65536) {
|
||||
q = i / 100;
|
||||
// really: r = i - (q * 100);
|
||||
r = i - ((q << 6) + (q << 5) + (q << 2));
|
||||
i = q;
|
||||
buf [--charPos] = DigitOnes[r];
|
||||
buf [--charPos] = DigitTens[r];
|
||||
}
|
||||
|
||||
// Fall thru to fast mode for smaller numbers
|
||||
// assert(i <= 65536, i);
|
||||
for (;;) {
|
||||
q = (i * 52429) >>> (16+3);
|
||||
r = i - ((q << 3) + (q << 1)); // r = i-(q*10) ...
|
||||
buf [--charPos] = digits [r];
|
||||
i = q;
|
||||
if (i == 0) break;
|
||||
}
|
||||
if (sign != 0) {
|
||||
buf [--charPos] = sign;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user