public byte[] bytesValue() { if (token == JSONToken.HEX) { int start = np + 1, len = sp; if (len % 2 != 0) { throw new JSONException("illegal state. " + len); } byte[] bytes = new byte[len / 2]; for (int i = 0; i < bytes.length; ++i) { char c0 = text.charAt(start + i * 2); char c1 = text.charAt(start + i * 2 + 1); int b0 = c0 - (c0 <= 57 ? 48 : 55); int b1 = c1 - (c1 <= 57 ? 48 : 55); bytes[i] = (byte) ((b0 << 4) | b1); } return bytes; } return IOUtils.decodeBase64(text, np + 1, sp); }
private String getJsonpParameterValue(HttpServletRequest request) { if (this.jsonpParameterNames != null) { for (String name : this.jsonpParameterNames) { String value = request.getParameter(name); if (IOUtils.isValidJsonpQueryParam(value)) { return value; } if (logger.isDebugEnabled()) { logger.debug("Ignoring invalid jsonp parameter value: " + value); } } } return null; }
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { ResponseJSONP responseJsonp = returnType.getMethodAnnotation(ResponseJSONP.class); if(responseJsonp == null){ responseJsonp = returnType.getContainingClass().getAnnotation(ResponseJSONP.class); } HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest(); String callbackMethodName = servletRequest.getParameter(responseJsonp.callback()); if (!IOUtils.isValidJsonpQueryParam(callbackMethodName)) { if (logger.isDebugEnabled()) { logger.debug("Invalid jsonp parameter value:" + callbackMethodName); } callbackMethodName = null; } JSONPObject jsonpObject = new JSONPObject(callbackMethodName); jsonpObject.addParameter(body); beforeBodyWriteInternal(jsonpObject, selectedContentType, returnType, request, response); return jsonpObject; }
private int encodeToUTF8(OutputStream out) throws IOException { int bytesLength = (int) (count * (double) 3); byte[] bytes = bytesBufLocal.get(); if (bytes == null) { bytes = new byte[1024 * 8]; bytesBufLocal.set(bytes); } if (bytes.length < bytesLength) { bytes = new byte[bytesLength]; } int position = IOUtils.encodeUTF8(buf, 0, count, bytes); out.write(bytes, 0, position); return position; }
private byte[] encodeToUTF8Bytes() { int bytesLength = (int) (count * (double) 3); byte[] bytes = bytesBufLocal.get(); if (bytes == null) { bytes = new byte[1024 * 8]; bytesBufLocal.set(bytes); } if (bytes.length < bytesLength) { bytes = new byte[bytesLength]; } int position = IOUtils.encodeUTF8(buf, 0, count, bytes); byte[] copy = new byte[position]; System.arraycopy(bytes, 0, copy, 0, position); return copy; }
public void writeInt(int i) { if (i == Integer.MIN_VALUE) { write("-2147483648"); return; } int size = (i < 0) ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i); int newcount = count + size; if (newcount > buf.length) { if (writer == null) { expandCapacity(newcount); } else { char[] chars = new char[size]; IOUtils.getChars(i, size, chars); write(chars, 0, chars.length); return; } } IOUtils.getChars(i, newcount, buf); count = newcount; }
/** * @since 1.2.11 */ @SuppressWarnings("unchecked") public static <T> T parseObject(byte[] bytes, int offset, int len, Charset charset, Type clazz, Feature... features) { if (charset == null) { charset = IOUtils.UTF8; } String strVal; if (charset == IOUtils.UTF8) { char[] chars = allocateChars(bytes.length); int chars_len = IOUtils.decodeUTF8(bytes, offset, len, chars); if (chars_len < 0) { return null; } strVal = new String(chars, 0, chars_len); } else { if (len < 0) { return null; } strVal = new String(bytes, offset, len, charset); } return (T) parseObject(strVal, clazz, features); }
@SuppressWarnings("unchecked") public static <T> T parseObject(byte[] input, // int off, // int len, // CharsetDecoder charsetDecoder, // Type clazz, // Feature... features) { charsetDecoder.reset(); int scaleLength = (int) (len * (double) charsetDecoder.maxCharsPerByte()); char[] chars = allocateChars(scaleLength); ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len); CharBuffer charByte = CharBuffer.wrap(chars); IOUtils.decode(charsetDecoder, byteBuf, charByte); int position = charByte.position(); return (T) parseObject(chars, position, clazz, features); }
public void test_config() throws Exception { new IOUtils(); DefaultJSONParser parser = new DefaultJSONParser(""); Assert.assertEquals(false, parser.isEnabled(Feature.AllowComment)); Assert.assertEquals(true, parser.isEnabled(Feature.AllowSingleQuotes)); Assert.assertEquals(true, parser.isEnabled(Feature.AllowUnQuotedFieldNames)); Assert.assertEquals(true, parser.isEnabled(Feature.AutoCloseSource)); Assert.assertEquals(true, parser.isEnabled(Feature.InternFieldNames)); parser.config(Feature.AllowComment, true); Assert.assertEquals(true, parser.isEnabled(Feature.AllowComment)); parser.config(Feature.InternFieldNames, false); Assert.assertEquals(false, parser.isEnabled(Feature.InternFieldNames)); }
public final String scanSymbolUnQuoted(SymbolTable symbolTable) { boolean[] firstIdentifierFlags = IOUtils.firstIdentifierFlags; char first = this.ch; boolean firstFlag = this.ch >= firstIdentifierFlags.length || firstIdentifierFlags[first]; if (firstFlag) { boolean[] identifierFlags = IOUtils.identifierFlags; int hash = first; this.np = this.bp; this.sp = 1; while (true) { char chLocal = next(); if (chLocal < identifierFlags.length && !identifierFlags[chLocal]) { break; } hash = (hash * 31) + chLocal; this.sp++; } this.ch = charAt(this.bp); this.token = 18; if (this.sp == 4 && hash == 3392903 && charAt(this.np) == 'n' && charAt(this.np + 1) == 'u' && charAt(this.np + 2) == 'l' && charAt(this.np + 3) == 'l') { return null; } return addSymbol(this.np, this.sp, hash, symbolTable); } throw new JSONException("illegal identifier : " + this.ch); }
public void writeInt(int i) { if (i == Integer.MIN_VALUE) { write("-2147483648"); return; } int size = i < 0 ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i); int newcount = this.count + size; if (newcount > this.buf.length) { if (this.writer == null) { expandCapacity(newcount); } else { char[] chars = new char[size]; IOUtils.getChars(i, size, chars); write(chars, 0, chars.length); return; } } IOUtils.getChars(i, newcount, this.buf); this.count = newcount; }
public void writeIntAndChar(int i, char c) { if (i == Integer.MIN_VALUE) { write("-2147483648"); write(c); return; } int newcount0 = this.count + (i < 0 ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i)); int newcount1 = newcount0 + 1; if (newcount1 > this.buf.length) { if (this.writer != null) { writeInt(i); write(c); return; } expandCapacity(newcount1); } IOUtils.getChars(i, newcount0, this.buf); this.buf[newcount0] = c; this.count = newcount1; }
public void writeLongAndChar(long i, char c) throws IOException { if (i == Long.MIN_VALUE) { write("-9223372036854775808"); write(c); return; } int newcount0 = this.count + (i < 0 ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i)); int newcount1 = newcount0 + 1; if (newcount1 > this.buf.length) { if (this.writer != null) { writeLong(i); write(c); return; } expandCapacity(newcount1); } IOUtils.getChars(i, newcount0, this.buf); this.buf[newcount0] = c; this.count = newcount1; }
public void writeLong(long i) { if (i == Long.MIN_VALUE) { write("-9223372036854775808"); return; } int size = i < 0 ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i); int newcount = this.count + size; if (newcount > this.buf.length) { if (this.writer == null) { expandCapacity(newcount); } else { char[] chars = new char[size]; IOUtils.getChars(i, size, chars); write(chars, 0, chars.length); return; } } IOUtils.getChars(i, newcount, this.buf); this.count = newcount; }
public void writeFieldValue(char seperator, String name, int value) { if (value == Integer.MIN_VALUE || !isEnabled(SerializerFeature.QuoteFieldNames)) { writeFieldValue1(seperator, name, value); return; } char keySeperator = isEnabled(SerializerFeature.UseSingleQuotes) ? '\'' : '\"'; int intSize = value < 0 ? IOUtils.stringSize(-value) + 1 : IOUtils.stringSize(value); int nameLen = name.length(); int newcount = ((this.count + nameLen) + 4) + intSize; if (newcount > this.buf.length) { if (this.writer != null) { writeFieldValue1(seperator, name, value); return; } expandCapacity(newcount); } int start = this.count; this.count = newcount; this.buf[start] = seperator; int nameEnd = (start + nameLen) + 1; this.buf[start + 1] = keySeperator; name.getChars(0, nameLen, this.buf, start + 2); this.buf[nameEnd + 1] = keySeperator; this.buf[nameEnd + 2] = ':'; IOUtils.getChars(value, this.count, this.buf); }
public void writeIntAndChar(int i, char c) { if (i == Integer.MIN_VALUE) { write("-2147483648"); write(c); return; } int size = (i < 0) ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i); int newcount0 = count + size; int newcount1 = newcount0 + 1; if (newcount1 > buf.length) { if (writer != null) { writeInt(i); write(c); return; } expandCapacity(newcount1); } IOUtils.getChars(i, newcount0, buf); buf[newcount0] = c; count = newcount1; }
public void writeLongAndChar(long i, char c) throws IOException { if (i == Long.MIN_VALUE) { write("-9223372036854775808"); write(c); return; } int size = (i < 0) ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i); int newcount0 = count + size; int newcount1 = newcount0 + 1; if (newcount1 > buf.length) { if (writer != null) { writeLong(i); write(c); return; } expandCapacity(newcount1); } IOUtils.getChars(i, newcount0, buf); buf[newcount0] = c; count = newcount1; }
public void writeLong(long i) { if (i == Long.MIN_VALUE) { write("-9223372036854775808"); return; } int size = (i < 0) ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i); int newcount = count + size; if (newcount > buf.length) { if (writer == null) { expandCapacity(newcount); } else { char[] chars = new char[size]; IOUtils.getChars(i, size, chars); write(chars, 0, chars.length); return; } } IOUtils.getChars(i, newcount, buf); count = newcount; }
@SuppressWarnings("unchecked") public static final <T> T parseObject(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Type clazz, Feature... features) { charsetDecoder.reset(); int scaleLength = (int) (len * (double) charsetDecoder.maxCharsPerByte()); char[] chars = ThreadLocalCache.getChars(scaleLength); ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len); CharBuffer charByte = CharBuffer.wrap(chars); IOUtils.decode(charsetDecoder, byteBuf, charByte); int position = charByte.position(); return (T) parseObject(chars, position, clazz, features); }
public static String readFile(String name) throws IOException { StringBuilder mapFileStr = new StringBuilder(); String path = ZookeeperPath.ZK_LOCAL_WRITE_PATH.getKey() + name; InputStream input = ResourceUtil.getResourceAsStreamFromRoot(path); checkNotNull(input, "read file curr Path :" + path + " is null! It must be not null"); byte[] buffers = new byte[256]; try { int readIndex; while ((readIndex = input.read(buffers)) != -1) { mapFileStr.append(new String(buffers, 0, readIndex)); } } finally { IOUtils.close(input); } return mapFileStr.toString(); }
public static void writeFile(String name, String value) throws IOException { String path = ResourceUtil.getResourcePathFromRoot(ZookeeperPath.ZK_LOCAL_WRITE_PATH.getKey()); checkNotNull(path, "write ecache file curr Path :" + path + " is null! It must be not null"); path = new File(path).getPath() + File.separator + name; ByteArrayInputStream input = null; byte[] buffers = new byte[256]; FileOutputStream output = null; try { int readIndex; input = new ByteArrayInputStream(value.getBytes()); output = new FileOutputStream(path); while ((readIndex = input.read(buffers)) != -1) { output.write(buffers, 0, readIndex); } } finally { IOUtils.close(output); IOUtils.close(input); } }
private <T> T toParseObject(byte[] input, int off, int len, CharsetDecoder charsetDecoder, Type clazz) { charsetDecoder.reset(); int scaleLength = (int) (len * (double) charsetDecoder.maxCharsPerByte()); char[] chars = ThreadLocalCache.getChars(scaleLength); ByteBuffer byteBuf = ByteBuffer.wrap(input, off, len); CharBuffer charByte = CharBuffer.wrap(chars); IOUtils.decode(charsetDecoder, byteBuf, charByte); int position = charByte.position(); if (chars.length == 0) { return null; } DefaultJSONParser parser = new DefaultJSONParser(chars, position, parseConfig, DEFAULT_PARSER_FEATURE); T value = parser.parseObject(clazz); parser.handleResovleTask(value); parser.close(); return value; }
public void writeInt(int i) { if (i == Integer.MIN_VALUE) { write("-2147483648"); return; } int size = (i < 0) ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i); int newcount = count + size; if (newcount > buf.length) { expandCapacity(newcount); } IOUtils.getChars(i, newcount, buf); count = newcount; }
public void writeIntAndChar(int i, char c) { if (i == Integer.MIN_VALUE) { write("-2147483648"); write(c); return; } int size = (i < 0) ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i); int newcount0 = count + size; int newcount1 = newcount0 + 1; if (newcount1 > buf.length) { expandCapacity(newcount1); } IOUtils.getChars(i, newcount0, buf); buf[newcount0] = c; count = newcount1; }
public void writeLongAndChar(long i, char c) throws IOException { if (i == Long.MIN_VALUE) { write("-9223372036854775808"); write(c); return; } int size = (i < 0) ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i); int newcount0 = count + size; int newcount1 = newcount0 + 1; if (newcount1 > buf.length) { expandCapacity(newcount1); } IOUtils.getChars(i, newcount0, buf); buf[newcount0] = c; count = newcount1; }
public void writeLong(long i) { if (i == Long.MIN_VALUE) { write("-9223372036854775808"); return; } int size = (i < 0) ? IOUtils.stringSize(-i) + 1 : IOUtils.stringSize(i); int newcount = count + size; if (newcount > buf.length) { expandCapacity(newcount); } IOUtils.getChars(i, newcount, buf); count = newcount; }
public static boolean save(byte[] bytes, String savePath){ FileOutputStream out = null; try { File saveFile = new File(savePath); if(saveFile.isDirectory()){ saveFile = new File(savePath,String.valueOf(System.currentTimeMillis())); } if(!saveFile.exists()){ saveFile.getParentFile().mkdirs(); } out = new FileOutputStream(saveFile); out.write(bytes); return true; } catch (Exception e) { return false; } finally { IOUtils.close(out); } }
public byte[] bytesValue() { if (token == JSONToken.HEX) { throw new JSONException("TODO"); } return IOUtils.decodeBase64(buf, np + 1, sp); }