http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d9a661cf/ranger_solrj/src/main/java/org/apache/solr/common/util/Base64.java ---------------------------------------------------------------------- diff --git a/ranger_solrj/src/main/java/org/apache/solr/common/util/Base64.java b/ranger_solrj/src/main/java/org/apache/solr/common/util/Base64.java deleted file mode 100644 index 8cc3381..0000000 --- a/ranger_solrj/src/main/java/org/apache/solr/common/util/Base64.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.solr.common.util; - -/** - * Static methods for translating Base64 encoded strings to byte arrays - * and vice-versa. - */ - -public class Base64 { - /** - * This array is a lookup table that translates 6-bit positive integer - * index values into their "Base64 Alphabet" equivalents as specified - * in Table 1 of RFC 2045. - */ - private static final char intToBase64[] = { - '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', - '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', - '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' - }; - - /** - * This array is a lookup table that translates unicode characters - * drawn from the "Base64 Alphabet" (as specified in Table 1 of RFC 2045) - * into their 6-bit positive integer equivalents. Characters that - * are not in the Base64 alphabet but fall within the bounds of the - * array are translated to -1. - */ - private static final byte base64ToInt[] = { - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, - 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, - 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, - 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, - 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 - }; - - public static String byteArrayToBase64(byte[] a, int offset, int len) { - int aLen = len; - int numFullGroups = aLen / 3; - int numBytesInPartialGroup = aLen - 3 * numFullGroups; - int resultLen = 4 * ((aLen + 2) / 3); - StringBuilder result = new StringBuilder(resultLen); - char[] intToAlpha = intToBase64; - - // Translate all full groups from byte array elements to Base64 - int inCursor = offset; - for (int i = 0; i < numFullGroups; i++) { - int byte0 = a[inCursor++] & 0xff; - int byte1 = a[inCursor++] & 0xff; - int byte2 = a[inCursor++] & 0xff; - result.append(intToAlpha[byte0 >> 2]); - result.append(intToAlpha[(byte0 << 4) & 0x3f | (byte1 >> 4)]); - result.append(intToAlpha[(byte1 << 2) & 0x3f | (byte2 >> 6)]); - result.append(intToAlpha[byte2 & 0x3f]); - } - - // Translate partial group if present - if (numBytesInPartialGroup != 0) { - int byte0 = a[inCursor++] & 0xff; - result.append(intToAlpha[byte0 >> 2]); - if (numBytesInPartialGroup == 1) { - result.append(intToAlpha[(byte0 << 4) & 0x3f]); - result.append("=="); - } else { - // assert numBytesInPartialGroup == 2; - int byte1 = a[inCursor++] & 0xff; - result.append(intToAlpha[(byte0 << 4) & 0x3f | (byte1 >> 4)]); - result.append(intToAlpha[(byte1 << 2) & 0x3f]); - result.append('='); - } - } - return result.toString(); - } - - public static byte[] base64ToByteArray(String s) { - byte[] alphaToInt = base64ToInt; - int sLen = s.length(); - int numGroups = sLen / 4; - if (4 * numGroups != sLen) - throw new IllegalArgumentException( - "String length must be a multiple of four."); - int missingBytesInLastGroup = 0; - int numFullGroups = numGroups; - if (sLen != 0) { - if (s.charAt(sLen - 1) == '=') { - missingBytesInLastGroup++; - numFullGroups--; - } - if (s.charAt(sLen - 2) == '=') - missingBytesInLastGroup++; - } - byte[] result = new byte[3 * numGroups - missingBytesInLastGroup]; - - // Translate all full groups from base64 to byte array elements - int inCursor = 0, outCursor = 0; - for (int i = 0; i < numFullGroups; i++) { - int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt); - int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt); - int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt); - int ch3 = base64toInt(s.charAt(inCursor++), alphaToInt); - result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4)); - result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2)); - result[outCursor++] = (byte) ((ch2 << 6) | ch3); - } - - // Translate partial group, if present - if (missingBytesInLastGroup != 0) { - int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt); - int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt); - result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4)); - - if (missingBytesInLastGroup == 1) { - int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt); - result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2)); - } - } - // assert inCursor == s.length()-missingBytesInLastGroup; - // assert outCursor == result.length; - return result; - } - - /** - * Translates the specified character, which is assumed to be in the - * "Base 64 Alphabet" into its equivalent 6-bit positive integer. - * - * @throw IllegalArgumentException or ArrayOutOfBoundsException if - * c is not in the Base64 Alphabet. - */ - private static int base64toInt(char c, byte[] alphaToInt) { - int result = alphaToInt[c]; - if (result < 0) - throw new IllegalArgumentException("Illegal character " + c); - return result; - } -}
http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d9a661cf/ranger_solrj/src/main/java/org/apache/solr/common/util/ByteUtils.java ---------------------------------------------------------------------- diff --git a/ranger_solrj/src/main/java/org/apache/solr/common/util/ByteUtils.java b/ranger_solrj/src/main/java/org/apache/solr/common/util/ByteUtils.java deleted file mode 100644 index 9ce607e..0000000 --- a/ranger_solrj/src/main/java/org/apache/solr/common/util/ByteUtils.java +++ /dev/null @@ -1,126 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.solr.common.util; - -import org.noggit.CharArr; - -public class ByteUtils { - - /** Converts utf8 to utf16 and returns the number of 16 bit Java chars written. - * Full characters are read, even if this reads past the length passed (and can result in - * an ArrayOutOfBoundsException if invalid UTF8 is passed). Explicit checks for valid UTF8 are not performed. - * The char[] out should probably have enough room to hold the worst case of each byte becoming a Java char. - */ - public static int UTF8toUTF16(byte[] utf8, int offset, int len, char[] out, int out_offset) { - int out_start = out_offset; - final int limit = offset + len; - while (offset < limit) { - int b = utf8[offset++]&0xff; - - if (b < 0xc0) { - assert b < 0x80; - out[out_offset++] = (char)b; - } else if (b < 0xe0) { - out[out_offset++] = (char)(((b&0x1f)<<6) + (utf8[offset++]&0x3f)); - } else if (b < 0xf0) { - out[out_offset++] = (char)(((b&0xf)<<12) + ((utf8[offset]&0x3f)<<6) + (utf8[offset+1]&0x3f)); - offset += 2; - } else { - assert b < 0xf8; - int ch = ((b&0x7)<<18) + ((utf8[offset]&0x3f)<<12) + ((utf8[offset+1]&0x3f)<<6) + (utf8[offset+2]&0x3f); - offset += 3; - if (ch < 0xffff) { - out[out_offset++] = (char)ch; - } else { - int chHalf = ch - 0x0010000; - out[out_offset++] = (char) ((chHalf >> 10) + 0xD800); - out[out_offset++] = (char) ((chHalf & 0x3FFL) + 0xDC00); - } - } - } - - return out_offset - out_start; - } - - /** Convert UTF8 bytes into UTF16 characters. */ - public static void UTF8toUTF16(byte[] utf8, int offset, int len, CharArr out) { - // TODO: do in chunks if the input is large - out.reserve(len); - int n = UTF8toUTF16(utf8, offset, len, out.getArray(), out.getEnd()); - out.setEnd(out.getEnd() + n); - } - - /** Convert UTF8 bytes into a String */ - public static String UTF8toUTF16(byte[] utf8, int offset, int len) { - char[] out = new char[len]; - int n = UTF8toUTF16(utf8, offset, len, out, 0); - return new String(out,0,n); - } - - - - /** Writes UTF8 into the byte array, starting at offset. The caller should ensure that - * there is enough space for the worst-case scenario. - * @return the number of bytes written - */ - public static int UTF16toUTF8(CharSequence s, int offset, int len, byte[] result, int resultOffset) { - final int end = offset + len; - - int upto = resultOffset; - for(int i=offset;i<end;i++) { - final int code = (int) s.charAt(i); - - if (code < 0x80) - result[upto++] = (byte) code; - else if (code < 0x800) { - result[upto++] = (byte) (0xC0 | (code >> 6)); - result[upto++] = (byte)(0x80 | (code & 0x3F)); - } else if (code < 0xD800 || code > 0xDFFF) { - result[upto++] = (byte)(0xE0 | (code >> 12)); - result[upto++] = (byte)(0x80 | ((code >> 6) & 0x3F)); - result[upto++] = (byte)(0x80 | (code & 0x3F)); - } else { - // surrogate pair - // confirm valid high surrogate - if (code < 0xDC00 && (i < end-1)) { - int utf32 = (int) s.charAt(i+1); - // confirm valid low surrogate and write pair - if (utf32 >= 0xDC00 && utf32 <= 0xDFFF) { - utf32 = ((code - 0xD7C0) << 10) + (utf32 & 0x3FF); - i++; - result[upto++] = (byte)(0xF0 | (utf32 >> 18)); - result[upto++] = (byte)(0x80 | ((utf32 >> 12) & 0x3F)); - result[upto++] = (byte)(0x80 | ((utf32 >> 6) & 0x3F)); - result[upto++] = (byte)(0x80 | (utf32 & 0x3F)); - continue; - } - } - // replace unpaired surrogate or out-of-order low surrogate - // with substitution character - result[upto++] = (byte) 0xEF; - result[upto++] = (byte) 0xBF; - result[upto++] = (byte) 0xBD; - } - } - - return upto - resultOffset; - } - - - -} http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d9a661cf/ranger_solrj/src/main/java/org/apache/solr/common/util/ContentStream.java ---------------------------------------------------------------------- diff --git a/ranger_solrj/src/main/java/org/apache/solr/common/util/ContentStream.java b/ranger_solrj/src/main/java/org/apache/solr/common/util/ContentStream.java deleted file mode 100644 index b455d80..0000000 --- a/ranger_solrj/src/main/java/org/apache/solr/common/util/ContentStream.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.solr.common.util; - -import java.io.IOException; -import java.io.InputStream; -import java.io.Reader; - -/** - * - * @since solr 1.2 - */ -public interface ContentStream { - String getName(); - String getSourceInfo(); - String getContentType(); - - /** - * @return the stream size or <code>null</code> if not known - */ - Long getSize(); // size if we know it, otherwise null - - /** - * Get an open stream. You are responsible for closing it. Consider using - * something like: - * <pre> - * InputStream stream = stream.getStream(); - * try { - * // use the stream... - * } - * finally { - * IOUtils.closeQuietly(stream); - * } - * </pre> - * - * Only the first call to <code>getStream()</code> or <code>getReader()</code> - * is guaranteed to work. The runtime behavior for additional calls is undefined. - * - * Note: you must call <code>getStream()</code> or <code>getReader()</code> before - * the attributes (name, contentType, etc) are guaranteed to be set. Streams may be - * lazy loaded only when this method is called. - */ - InputStream getStream() throws IOException; - - /** - * Get an open stream. You are responsible for closing it. Consider using - * something like: - * <pre> - * Reader reader = stream.getReader(); - * try { - * // use the reader... - * } - * finally { - * IOUtils.closeQuietly(reader); - * } - * </pre> - * - * Only the first call to <code>getStream()</code> or <code>getReader()</code> - * is guaranteed to work. The runtime behavior for additional calls is undefined. - * - * Note: you must call <code>getStream()</code> or <code>getReader()</code> before - * the attributes (name, contentType, etc) are guaranteed to be set. Streams may be - * lazy loaded only when this method is called. - */ - Reader getReader() throws IOException; -} http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d9a661cf/ranger_solrj/src/main/java/org/apache/solr/common/util/ContentStreamBase.java ---------------------------------------------------------------------- diff --git a/ranger_solrj/src/main/java/org/apache/solr/common/util/ContentStreamBase.java b/ranger_solrj/src/main/java/org/apache/solr/common/util/ContentStreamBase.java deleted file mode 100644 index 24549b6..0000000 --- a/ranger_solrj/src/main/java/org/apache/solr/common/util/ContentStreamBase.java +++ /dev/null @@ -1,260 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.solr.common.util; - -import java.io.ByteArrayInputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; -import java.io.StringReader; -import java.net.URL; -import java.net.URLConnection; -import java.nio.charset.StandardCharsets; -import java.util.Locale; - -/** - * Three concrete implementations for ContentStream - one for File/URL/String - * - * - * @since solr 1.2 - */ -public abstract class ContentStreamBase implements ContentStream -{ - public static final String DEFAULT_CHARSET = StandardCharsets.UTF_8.name(); - - protected String name; - protected String sourceInfo; - protected String contentType; - protected Long size; - - //--------------------------------------------------------------------- - //--------------------------------------------------------------------- - - public static String getCharsetFromContentType( String contentType ) - { - if( contentType != null ) { - int idx = contentType.toLowerCase(Locale.ROOT).indexOf( "charset=" ); - if( idx > 0 ) { - return contentType.substring( idx + "charset=".length() ).trim(); - } - } - return null; - } - - //------------------------------------------------------------------------ - //------------------------------------------------------------------------ - - /** - * Construct a <code>ContentStream</code> from a <code>URL</code> - * - * This uses a <code>URLConnection</code> to get the content stream - * @see URLConnection - */ - public static class URLStream extends ContentStreamBase - { - private final URL url; - - public URLStream( URL url ) { - this.url = url; - sourceInfo = "url"; - } - - @Override - public InputStream getStream() throws IOException { - URLConnection conn = this.url.openConnection(); - - contentType = conn.getContentType(); - name = url.toExternalForm(); - size = Long.valueOf( conn.getContentLength() ); - return conn.getInputStream(); - } - } - - /** - * Construct a <code>ContentStream</code> from a <code>File</code> - */ - public static class FileStream extends ContentStreamBase - { - private final File file; - - public FileStream( File f ) { - file = f; - - contentType = null; // ?? - name = file.getName(); - size = file.length(); - sourceInfo = file.toURI().toString(); - } - - @Override - public String getContentType() { - if(contentType==null) { - InputStream stream = null; - try { - stream = new FileInputStream(file); - char first = (char)stream.read(); - if(first == '<') { - return "application/xml"; - } - if(first == '{') { - return "application/json"; - } - } catch(Exception ex) { - } finally { - if (stream != null) try { - stream.close(); - } catch (IOException ioe) {} - } - } - return contentType; - } - - @Override - public InputStream getStream() throws IOException { - return new FileInputStream( file ); - } - } - - - /** - * Construct a <code>ContentStream</code> from a <code>String</code> - */ - public static class StringStream extends ContentStreamBase - { - private final String str; - - public StringStream( String str ) { - this.str = str; - - contentType = null; - name = null; - size = Long.valueOf( str.length() ); - sourceInfo = "string"; - } - - @Override - public String getContentType() { - if(contentType==null && str.length() > 0) { - char first = str.charAt(0); - if(first == '<') { - return "application/xml"; - } - if(first == '{') { - return "application/json"; - } - // find a comma? for CSV? - } - return contentType; - } - - @Override - public InputStream getStream() throws IOException { - return new ByteArrayInputStream( str.getBytes(DEFAULT_CHARSET) ); - } - - /** - * If an charset is defined (by the contentType) use that, otherwise - * use a StringReader - */ - @Override - public Reader getReader() throws IOException { - String charset = getCharsetFromContentType( contentType ); - return charset == null - ? new StringReader( str ) - : new InputStreamReader( getStream(), charset ); - } - } - - /** - * Base reader implementation. If the contentType declares a - * charset use it, otherwise use "utf-8". - */ - @Override - public Reader getReader() throws IOException { - String charset = getCharsetFromContentType( getContentType() ); - return charset == null - ? new InputStreamReader( getStream(), DEFAULT_CHARSET ) - : new InputStreamReader( getStream(), charset ); - } - - //------------------------------------------------------------------ - // Getters / Setters for overrideable attributes - //------------------------------------------------------------------ - - @Override - public String getContentType() { - return contentType; - } - - public void setContentType(String contentType) { - this.contentType = contentType; - } - - @Override - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - @Override - public Long getSize() { - return size; - } - - public void setSize(Long size) { - this.size = size; - } - - @Override - public String getSourceInfo() { - return sourceInfo; - } - - public void setSourceInfo(String sourceInfo) { - this.sourceInfo = sourceInfo; - } - - /** - * Construct a <code>ContentStream</code> from a <code>File</code> - */ - public static class ByteArrayStream extends ContentStreamBase - { - private final byte[] bytes; - - public ByteArrayStream( byte[] bytes, String source ) { - this.bytes = bytes; - - this.contentType = null; - name = source; - size = Long.valueOf(bytes.length); - sourceInfo = source; - } - - - @Override - public InputStream getStream() throws IOException { - return new ByteArrayInputStream( bytes ); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d9a661cf/ranger_solrj/src/main/java/org/apache/solr/common/util/DataInputInputStream.java ---------------------------------------------------------------------- diff --git a/ranger_solrj/src/main/java/org/apache/solr/common/util/DataInputInputStream.java b/ranger_solrj/src/main/java/org/apache/solr/common/util/DataInputInputStream.java deleted file mode 100644 index d412f40..0000000 --- a/ranger_solrj/src/main/java/org/apache/solr/common/util/DataInputInputStream.java +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.solr.common.util; - -import java.io.DataInput; -import java.io.InputStream; - -/** - * An abstract DataInput that extends InputStream - */ -public abstract class DataInputInputStream extends InputStream implements DataInput { -} http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d9a661cf/ranger_solrj/src/main/java/org/apache/solr/common/util/DateUtil.java ---------------------------------------------------------------------- diff --git a/ranger_solrj/src/main/java/org/apache/solr/common/util/DateUtil.java b/ranger_solrj/src/main/java/org/apache/solr/common/util/DateUtil.java deleted file mode 100644 index 6409dbc..0000000 --- a/ranger_solrj/src/main/java/org/apache/solr/common/util/DateUtil.java +++ /dev/null @@ -1,261 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.solr.common.util; - -import java.io.IOException; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Calendar; -import java.util.Collection; -import java.util.Date; -import java.util.Iterator; -import java.util.Locale; -import java.util.TimeZone; - - -/** - * This class has some code from HttpClient DateUtil. - */ -public class DateUtil { - //start HttpClient - /** - * Date format pattern used to parse HTTP date headers in RFC 1123 format. - */ - public static final String PATTERN_RFC1123 = "EEE, dd MMM yyyy HH:mm:ss zzz"; - - /** - * Date format pattern used to parse HTTP date headers in RFC 1036 format. - */ - public static final String PATTERN_RFC1036 = "EEEE, dd-MMM-yy HH:mm:ss zzz"; - - /** - * Date format pattern used to parse HTTP date headers in ANSI C - * <code>asctime()</code> format. - */ - public static final String PATTERN_ASCTIME = "EEE MMM d HH:mm:ss yyyy"; - //These are included for back compat - private static final Collection<String> DEFAULT_HTTP_CLIENT_PATTERNS = Arrays.asList( - PATTERN_ASCTIME, PATTERN_RFC1036, PATTERN_RFC1123); - - private static final Date DEFAULT_TWO_DIGIT_YEAR_START; - - static { - Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ROOT); - calendar.set(2000, Calendar.JANUARY, 1, 0, 0); - DEFAULT_TWO_DIGIT_YEAR_START = calendar.getTime(); - } - - private static final TimeZone GMT = TimeZone.getTimeZone("GMT"); - - //end HttpClient - - //--------------------------------------------------------------------------------------- - - /** - * A suite of default date formats that can be parsed, and thus transformed to the Solr specific format - */ - public static final Collection<String> DEFAULT_DATE_FORMATS = new ArrayList<>(); - - static { - DEFAULT_DATE_FORMATS.add("yyyy-MM-dd'T'HH:mm:ss'Z'"); - DEFAULT_DATE_FORMATS.add("yyyy-MM-dd'T'HH:mm:ss"); - DEFAULT_DATE_FORMATS.add("yyyy-MM-dd"); - DEFAULT_DATE_FORMATS.add("yyyy-MM-dd hh:mm:ss"); - DEFAULT_DATE_FORMATS.add("yyyy-MM-dd HH:mm:ss"); - DEFAULT_DATE_FORMATS.add("EEE MMM d hh:mm:ss z yyyy"); - DEFAULT_DATE_FORMATS.addAll(DEFAULT_HTTP_CLIENT_PATTERNS); - } - - /** - * Returns a formatter that can be use by the current thread if needed to - * convert Date objects to the Internal representation. - * - * @param d The input date to parse - * @return The parsed {@link java.util.Date} - * @throws java.text.ParseException If the input can't be parsed - */ - public static Date parseDate(String d) throws ParseException { - return parseDate(d, DEFAULT_DATE_FORMATS); - } - - public static Date parseDate(String d, Collection<String> fmts) throws ParseException { - // 2007-04-26T08:05:04Z - if (d.endsWith("Z") && d.length() > 20) { - return getThreadLocalDateFormat().parse(d); - } - return parseDate(d, fmts, null); - } - - /** - * Slightly modified from org.apache.commons.httpclient.util.DateUtil.parseDate - * <p> - * Parses the date value using the given date formats. - * - * @param dateValue the date value to parse - * @param dateFormats the date formats to use - * @param startDate During parsing, two digit years will be placed in the range - * <code>startDate</code> to <code>startDate + 100 years</code>. This value may - * be <code>null</code>. When <code>null</code> is given as a parameter, year - * <code>2000</code> will be used. - * @return the parsed date - * @throws ParseException if none of the dataFormats could parse the dateValue - */ - public static Date parseDate( - String dateValue, - Collection<String> dateFormats, - Date startDate - ) throws ParseException { - - if (dateValue == null) { - throw new IllegalArgumentException("dateValue is null"); - } - if (dateFormats == null) { - dateFormats = DEFAULT_HTTP_CLIENT_PATTERNS; - } - if (startDate == null) { - startDate = DEFAULT_TWO_DIGIT_YEAR_START; - } - // trim single quotes around date if present - // see issue #5279 - if (dateValue.length() > 1 - && dateValue.startsWith("'") - && dateValue.endsWith("'") - ) { - dateValue = dateValue.substring(1, dateValue.length() - 1); - } - - SimpleDateFormat dateParser = null; - Iterator formatIter = dateFormats.iterator(); - - while (formatIter.hasNext()) { - String format = (String) formatIter.next(); - if (dateParser == null) { - dateParser = new SimpleDateFormat(format, Locale.ROOT); - dateParser.setTimeZone(GMT); - dateParser.set2DigitYearStart(startDate); - } else { - dateParser.applyPattern(format); - } - try { - return dateParser.parse(dateValue); - } catch (ParseException pe) { - // ignore this exception, we will try the next format - } - } - - // we were unable to parse the date - throw new ParseException("Unable to parse the date " + dateValue, 0); - } - - - /** - * Returns a formatter that can be use by the current thread if needed to - * convert Date objects to the Internal representation. - * - * @return The {@link java.text.DateFormat} for the current thread - */ - public static DateFormat getThreadLocalDateFormat() { - return fmtThreadLocal.get(); - } - - public static TimeZone UTC = TimeZone.getTimeZone("UTC"); - private static ThreadLocalDateFormat fmtThreadLocal = new ThreadLocalDateFormat(); - - private static class ThreadLocalDateFormat extends ThreadLocal<DateFormat> { - DateFormat proto; - - public ThreadLocalDateFormat() { - super(); - //2007-04-26T08:05:04Z - SimpleDateFormat tmp = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.ROOT); - tmp.setTimeZone(UTC); - proto = tmp; - } - - @Override - protected DateFormat initialValue() { - return (DateFormat) proto.clone(); - } - } - - /** Formats the date and returns the calendar instance that was used (which may be reused) */ - public static Calendar formatDate(Date date, Calendar cal, Appendable out) throws IOException { - // using a stringBuilder for numbers can be nice since - // a temporary string isn't used (it's added directly to the - // builder's buffer. - - StringBuilder sb = out instanceof StringBuilder ? (StringBuilder)out : new StringBuilder(); - if (cal==null) cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ROOT); - cal.setTime(date); - - int i = cal.get(Calendar.YEAR); - sb.append(i); - sb.append('-'); - i = cal.get(Calendar.MONTH) + 1; // 0 based, so add 1 - if (i<10) sb.append('0'); - sb.append(i); - sb.append('-'); - i=cal.get(Calendar.DAY_OF_MONTH); - if (i<10) sb.append('0'); - sb.append(i); - sb.append('T'); - i=cal.get(Calendar.HOUR_OF_DAY); // 24 hour time format - if (i<10) sb.append('0'); - sb.append(i); - sb.append(':'); - i=cal.get(Calendar.MINUTE); - if (i<10) sb.append('0'); - sb.append(i); - sb.append(':'); - i=cal.get(Calendar.SECOND); - if (i<10) sb.append('0'); - sb.append(i); - i=cal.get(Calendar.MILLISECOND); - if (i != 0) { - sb.append('.'); - if (i<100) sb.append('0'); - if (i<10) sb.append('0'); - sb.append(i); - - // handle canonical format specifying fractional - // seconds shall not end in '0'. Given the slowness of - // integer div/mod, simply checking the last character - // is probably the fastest way to check. - int lastIdx = sb.length()-1; - if (sb.charAt(lastIdx)=='0') { - lastIdx--; - if (sb.charAt(lastIdx)=='0') { - lastIdx--; - } - sb.setLength(lastIdx+1); - } - - } - sb.append('Z'); - - if (out != sb) - out.append(sb); - - return cal; - } - - -} http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d9a661cf/ranger_solrj/src/main/java/org/apache/solr/common/util/ExecutorUtil.java ---------------------------------------------------------------------- diff --git a/ranger_solrj/src/main/java/org/apache/solr/common/util/ExecutorUtil.java b/ranger_solrj/src/main/java/org/apache/solr/common/util/ExecutorUtil.java deleted file mode 100644 index f17316c..0000000 --- a/ranger_solrj/src/main/java/org/apache/solr/common/util/ExecutorUtil.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.solr.common.util; - -import java.util.concurrent.ExecutorService; -import java.util.concurrent.TimeUnit; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - - -public class ExecutorUtil { - public static Logger log = LoggerFactory.getLogger(ExecutorUtil.class); - - public static void shutdownNowAndAwaitTermination(ExecutorService pool) { - pool.shutdown(); // Disable new tasks from being submitted - pool.shutdownNow(); // Cancel currently executing tasks - boolean shutdown = false; - while (!shutdown) { - try { - // Wait a while for existing tasks to terminate - shutdown = pool.awaitTermination(5, TimeUnit.SECONDS); - } catch (InterruptedException ie) { - // Preserve interrupt status - Thread.currentThread().interrupt(); - } - if (!shutdown) { - pool.shutdownNow(); // Cancel currently executing tasks - } - } - } - - public static void shutdownAndAwaitTermination(ExecutorService pool) { - pool.shutdown(); // Disable new tasks from being submitted - boolean shutdown = false; - while (!shutdown) { - try { - // Wait a while for existing tasks to terminate - shutdown = pool.awaitTermination(60, TimeUnit.SECONDS); - } catch (InterruptedException ie) { - // Preserve interrupt status - Thread.currentThread().interrupt(); - } - if (!shutdown) { - pool.shutdownNow(); // Cancel currently executing tasks - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d9a661cf/ranger_solrj/src/main/java/org/apache/solr/common/util/FastInputStream.java ---------------------------------------------------------------------- diff --git a/ranger_solrj/src/main/java/org/apache/solr/common/util/FastInputStream.java b/ranger_solrj/src/main/java/org/apache/solr/common/util/FastInputStream.java deleted file mode 100644 index 8a2ecee..0000000 --- a/ranger_solrj/src/main/java/org/apache/solr/common/util/FastInputStream.java +++ /dev/null @@ -1,253 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.solr.common.util; - -import java.io.*; - -/** Single threaded buffered InputStream - * Internal Solr use only, subject to change. - */ -public class FastInputStream extends DataInputInputStream { - protected final InputStream in; - protected final byte[] buf; - protected int pos; - protected int end; - protected long readFromStream; // number of bytes read from the underlying inputstream - - public FastInputStream(InputStream in) { - // use default BUFSIZE of BufferedOutputStream so if we wrap that - // it won't cause double buffering. - this(in, new byte[8192], 0, 0); - } - - public FastInputStream(InputStream in, byte[] tempBuffer, int start, int end) { - this.in = in; - this.buf = tempBuffer; - this.pos = start; - this.end = end; - } - - - public static FastInputStream wrap(InputStream in) { - return (in instanceof FastInputStream) ? (FastInputStream)in : new FastInputStream(in); - } - - @Override - public int read() throws IOException { - if (pos >= end) { - refill(); - if (pos >= end) return -1; - } - return buf[pos++] & 0xff; - } - - public int peek() throws IOException { - if (pos >= end) { - refill(); - if (pos >= end) return -1; - } - return buf[pos] & 0xff; - } - - - @Override - public int readUnsignedByte() throws IOException { - if (pos >= end) { - refill(); - if (pos >= end) { - throw new EOFException(); - } - } - return buf[pos++] & 0xff; - } - - public int readWrappedStream(byte[] target, int offset, int len) throws IOException { - return in.read(target, offset, len); - } - - public long position() { - return readFromStream - (end - pos); - } - - public void refill() throws IOException { - // this will set end to -1 at EOF - end = readWrappedStream(buf, 0, buf.length); - if (end > 0) readFromStream += end; - pos = 0; - } - - @Override - public int available() throws IOException { - return end - pos; - } - - @Override - public int read(byte b[], int off, int len) throws IOException { - int r=0; // number of bytes we have read - - // first read from our buffer; - if (end-pos > 0) { - r = Math.min(end-pos, len); - System.arraycopy(buf, pos, b, off, r); - pos += r; - } - - if (r == len) return r; - - // amount left to read is >= buffer size - if (len-r >= buf.length) { - int ret = readWrappedStream(b, off+r, len-r); - if (ret >= 0) { - readFromStream += ret; - r += ret; - return r; - } else { - // negative return code - return r > 0 ? r : -1; - } - } - - refill(); - - // read rest from our buffer - if (end-pos > 0) { - int toRead = Math.min(end-pos, len-r); - System.arraycopy(buf, pos, b, off+r, toRead); - pos += toRead; - r += toRead; - return r; - } - - return r > 0 ? r : -1; - } - - @Override - public void close() throws IOException { - in.close(); - } - - @Override - public void readFully(byte b[]) throws IOException { - readFully(b, 0, b.length); - } - - @Override - public void readFully(byte b[], int off, int len) throws IOException { - while (len>0) { - int ret = read(b, off, len); - if (ret==-1) { - throw new EOFException(); - } - off += ret; - len -= ret; - } - } - - @Override - public int skipBytes(int n) throws IOException { - if (end-pos >= n) { - pos += n; - return n; - } - - if (end-pos<0) return -1; - - int r = end-pos; - pos = end; - - while (r < n) { - refill(); - if (end-pos <= 0) return r; - int toRead = Math.min(end-pos, n-r); - r += toRead; - pos += toRead; - } - - return r; - } - - @Override - public boolean readBoolean() throws IOException { - return readByte()==1; - } - - @Override - public byte readByte() throws IOException { - if (pos >= end) { - refill(); - if (pos >= end) throw new EOFException(); - } - return buf[pos++]; - } - - - @Override - public short readShort() throws IOException { - return (short)((readUnsignedByte() << 8) | readUnsignedByte()); - } - - @Override - public int readUnsignedShort() throws IOException { - return (readUnsignedByte() << 8) | readUnsignedByte(); - } - - @Override - public char readChar() throws IOException { - return (char)((readUnsignedByte() << 8) | readUnsignedByte()); - } - - @Override - public int readInt() throws IOException { - return ((readUnsignedByte() << 24) - |(readUnsignedByte() << 16) - |(readUnsignedByte() << 8) - | readUnsignedByte()); - } - - @Override - public long readLong() throws IOException { - return (((long)readUnsignedByte()) << 56) - | (((long)readUnsignedByte()) << 48) - | (((long)readUnsignedByte()) << 40) - | (((long)readUnsignedByte()) << 32) - | (((long)readUnsignedByte()) << 24) - | (readUnsignedByte() << 16) - | (readUnsignedByte() << 8) - | (readUnsignedByte()); - } - - @Override - public float readFloat() throws IOException { - return Float.intBitsToFloat(readInt()); - } - - @Override - public double readDouble() throws IOException { - return Double.longBitsToDouble(readLong()); - } - - @Override - public String readLine() throws IOException { - throw new UnsupportedOperationException(); - } - - @Override - public String readUTF() throws IOException { - return new DataInputStream(this).readUTF(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d9a661cf/ranger_solrj/src/main/java/org/apache/solr/common/util/FastOutputStream.java ---------------------------------------------------------------------- diff --git a/ranger_solrj/src/main/java/org/apache/solr/common/util/FastOutputStream.java b/ranger_solrj/src/main/java/org/apache/solr/common/util/FastOutputStream.java deleted file mode 100644 index 09a7441..0000000 --- a/ranger_solrj/src/main/java/org/apache/solr/common/util/FastOutputStream.java +++ /dev/null @@ -1,233 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.solr.common.util; - -import java.io.*; - -/** Single threaded buffered OutputStream - * Internal Solr use only, subject to change. - */ -public class FastOutputStream extends OutputStream implements DataOutput { - protected final OutputStream out; - protected byte[] buf; - protected long written; // how many bytes written to the underlying stream - protected int pos; - - public FastOutputStream(OutputStream w) { - // use default BUFSIZE of BufferedOutputStream so if we wrap that - // it won't cause double buffering. - this(w, new byte[8192], 0); - } - - public FastOutputStream(OutputStream sink, byte[] tempBuffer, int start) { - this.out = sink; - this.buf = tempBuffer; - this.pos = start; - } - - - public static FastOutputStream wrap(OutputStream sink) { - return (sink instanceof FastOutputStream) ? (FastOutputStream)sink : new FastOutputStream(sink); - } - - @Override - public void write(int b) throws IOException { - write((byte)b); - } - - @Override - public void write(byte b[]) throws IOException { - write(b,0,b.length); - } - - public void write(byte b) throws IOException { - if (pos >= buf.length) { - written += pos; - flush(buf, 0, buf.length); - pos=0; - } - buf[pos++] = b; - } - - @Override - public void write(byte arr[], int off, int len) throws IOException { - - for(;;) { - int space = buf.length - pos; - - if (len <= space) { - System.arraycopy(arr, off, buf, pos, len); - pos += len; - return; - } else if (len > buf.length) { - if (pos>0) { - flush(buf,0,pos); // flush - written += pos; - pos=0; - } - // don't buffer, just write to sink - flush(arr, off, len); - written += len; - return; - } - - // buffer is too big to fit in the free space, but - // not big enough to warrant writing on its own. - // write whatever we can fit, then flush and iterate. - - System.arraycopy(arr, off, buf, pos, space); - written += buf.length; // important to do this first, since buf.length can change after a flush! - flush(buf, 0, buf.length); - pos = 0; - off += space; - len -= space; - } - } - - - /** reserve at least len bytes at the end of the buffer. - * Invalid if len > buffer.length - */ - public void reserve(int len) throws IOException { - if (len > (buf.length - pos)) - flushBuffer(); - } - - ////////////////// DataOutput methods /////////////////// - @Override - public void writeBoolean(boolean v) throws IOException { - write(v ? 1:0); - } - - @Override - public void writeByte(int v) throws IOException { - write((byte)v); - } - - @Override - public void writeShort(int v) throws IOException { - write((byte)(v >>> 8)); - write((byte)v); - } - - @Override - public void writeChar(int v) throws IOException { - writeShort(v); - } - - @Override - public void writeInt(int v) throws IOException { - reserve(4); - buf[pos] = (byte)(v>>>24); - buf[pos+1] = (byte)(v>>>16); - buf[pos+2] = (byte)(v>>>8); - buf[pos+3] = (byte)(v); - pos+=4; - } - - @Override - public void writeLong(long v) throws IOException { - reserve(8); - buf[pos] = (byte)(v>>>56); - buf[pos+1] = (byte)(v>>>48); - buf[pos+2] = (byte)(v>>>40); - buf[pos+3] = (byte)(v>>>32); - buf[pos+4] = (byte)(v>>>24); - buf[pos+5] = (byte)(v>>>16); - buf[pos+6] = (byte)(v>>>8); - buf[pos+7] = (byte)(v); - pos+=8; - } - - @Override - public void writeFloat(float v) throws IOException { - writeInt(Float.floatToRawIntBits(v)); - } - - @Override - public void writeDouble(double v) throws IOException { - writeLong(Double.doubleToRawLongBits(v)); - } - - @Override - public void writeBytes(String s) throws IOException { - // non-optimized version, but this shouldn't be used anyway - for (int i=0; i<s.length(); i++) - write((byte)s.charAt(i)); - } - - @Override - public void writeChars(String s) throws IOException { - // non-optimized version - for (int i=0; i<s.length(); i++) - writeChar(s.charAt(i)); - } - - @Override - public void writeUTF(String s) throws IOException { - // non-optimized version, but this shouldn't be used anyway - DataOutputStream daos = new DataOutputStream(this); - daos.writeUTF(s); - } - - - @Override - public void flush() throws IOException { - flushBuffer(); - if (out != null) out.flush(); - } - - @Override - public void close() throws IOException { - flushBuffer(); - if (out != null) out.close(); - } - - /** Only flushes the buffer of the FastOutputStream, not that of the - * underlying stream. - */ - public void flushBuffer() throws IOException { - if (pos > 0) { - written += pos; - flush(buf, 0, pos); - pos=0; - } - } - - /** All writes to the sink will go through this method */ - public void flush(byte[] buf, int offset, int len) throws IOException { - out.write(buf, offset, len); - } - - public long size() { - return written + pos; - } - - /** Returns the number of bytes actually written to the underlying OutputStream, not including - * anything currently buffered by this class itself. - */ - public long written() { - return written; - } - - /** Resets the count returned by written() */ - public void setWritten(long written) { - this.written = written; - } - -} http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d9a661cf/ranger_solrj/src/main/java/org/apache/solr/common/util/Hash.java ---------------------------------------------------------------------- diff --git a/ranger_solrj/src/main/java/org/apache/solr/common/util/Hash.java b/ranger_solrj/src/main/java/org/apache/solr/common/util/Hash.java deleted file mode 100644 index f541fe1..0000000 --- a/ranger_solrj/src/main/java/org/apache/solr/common/util/Hash.java +++ /dev/null @@ -1,422 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.solr.common.util; - -/** - * <p>Fast, well distributed, cross-platform hash functions. - * </p> - * - * <p>Development background: I was surprised to discovered that there isn't a good cross-platform hash function defined for strings. MD5, SHA, FVN, etc, all define hash functions over bytes, meaning that it's under-specified for strings. - * </p> - * - * <p>So I set out to create a standard 32 bit string hash that would be well defined for implementation in all languages, have very high performance, and have very good hash properties such as distribution. After evaluating all the options, I settled on using Bob Jenkins' lookup3 as a base. It's a well studied and very fast hash function, and the hashword variant can work with 32 bits at a time (perfect for hashing unicode code points). It's also even faster on the latest JVMs which can translate pairs of shifts into native rotate instructions. - * </p> - * <p>The only problem with using lookup3 hashword is that it includes a length in the initial value. This would suck some performance out since directly hashing a UTF8 or UTF16 string (Java) would require a pre-scan to get the actual number of unicode code points. The solution was to simply remove the length factor, which is equivalent to biasing initVal by -(numCodePoints*4). This slightly modified lookup3 I define as lookup3ycs. - * </p> - * <p>So the definition of the cross-platform string hash lookup3ycs is as follows: - * </p> - * <p>The hash value of a character sequence (a string) is defined to be the hash of its unicode code points, according to lookup3 hashword, with the initval biased by -(length*4). - * </p> - *<p>So by definition - *</p> - * <pre> - * lookup3ycs(k,offset,length,initval) == lookup3(k,offset,length,initval-(length*4)) - * - * AND - * - * lookup3ycs(k,offset,length,initval+(length*4)) == lookup3(k,offset,length,initval) - * </pre> - * <p>An obvious advantage of this relationship is that you can use lookup3 if you don't have an implementation of lookup3ycs. - * </p> - */ -public class Hash { - /** - * A Java implementation of hashword from lookup3.c by Bob Jenkins - * (<a href="http://burtleburtle.net/bob/c/lookup3.c">original source</a>). - * - * @param k the key to hash - * @param offset offset of the start of the key - * @param length length of the key - * @param initval initial value to fold into the hash - * @return the 32 bit hash code - */ - @SuppressWarnings("fallthrough") - public static int lookup3(int[] k, int offset, int length, int initval) { - int a,b,c; - a = b = c = 0xdeadbeef + (length<<2) + initval; - - int i=offset; - while (length > 3) - { - a += k[i]; - b += k[i+1]; - c += k[i+2]; - - // mix(a,b,c)... Java needs "out" parameters!!! - // Note: recent JVMs (Sun JDK6) turn pairs of shifts (needed to do a rotate) - // into real x86 rotate instructions. - { - a -= c; a ^= (c<<4)|(c>>>-4); c += b; - b -= a; b ^= (a<<6)|(a>>>-6); a += c; - c -= b; c ^= (b<<8)|(b>>>-8); b += a; - a -= c; a ^= (c<<16)|(c>>>-16); c += b; - b -= a; b ^= (a<<19)|(a>>>-19); a += c; - c -= b; c ^= (b<<4)|(b>>>-4); b += a; - } - - length -= 3; - i += 3; - } - - switch(length) { - case 3 : c+=k[i+2]; // fall through - case 2 : b+=k[i+1]; // fall through - case 1 : a+=k[i+0]; // fall through - // final(a,b,c); - { - c ^= b; c -= (b<<14)|(b>>>-14); - a ^= c; a -= (c<<11)|(c>>>-11); - b ^= a; b -= (a<<25)|(a>>>-25); - c ^= b; c -= (b<<16)|(b>>>-16); - a ^= c; a -= (c<<4)|(c>>>-4); - b ^= a; b -= (a<<14)|(a>>>-14); - c ^= b; c -= (b<<24)|(b>>>-24); - } - case 0: - break; - } - return c; - } - - - /** - * Identical to lookup3, except initval is biased by -(length<<2). - * This is equivalent to leaving out the length factor in the initial state. - * {@code lookup3ycs(k,offset,length,initval) == lookup3(k,offset,length,initval-(length<<2))} - * and - * {@code lookup3ycs(k,offset,length,initval+(length<<2)) == lookup3(k,offset,length,initval)} - */ - public static int lookup3ycs(int[] k, int offset, int length, int initval) { - return lookup3(k, offset, length, initval-(length<<2)); - } - - - /** - * <p>The hash value of a character sequence is defined to be the hash of - * its unicode code points, according to {@link #lookup3ycs(int[] k, int offset, int length, int initval)} - * </p> - * <p>If you know the number of code points in the {@code CharSequence}, you can - * generate the same hash as the original lookup3 - * via {@code lookup3ycs(s, start, end, initval+(numCodePoints<<2))} - */ - public static int lookup3ycs(CharSequence s, int start, int end, int initval) { - int a,b,c; - a = b = c = 0xdeadbeef + initval; - // only difference from lookup3 is that "+ (length<<2)" is missing - // since we don't know the number of code points to start with, - // and don't want to have to pre-scan the string to find out. - - int i=start; - boolean mixed=true; // have the 3 state variables been adequately mixed? - for(;;) { - if (i>= end) break; - mixed=false; - char ch; - ch = s.charAt(i++); - a += Character.isHighSurrogate(ch) && i< end ? Character.toCodePoint(ch, s.charAt(i++)) : ch; - if (i>= end) break; - ch = s.charAt(i++); - b += Character.isHighSurrogate(ch) && i< end ? Character.toCodePoint(ch, s.charAt(i++)) : ch; - if (i>= end) break; - ch = s.charAt(i++); - c += Character.isHighSurrogate(ch) && i< end ? Character.toCodePoint(ch, s.charAt(i++)) : ch; - if (i>= end) break; - - // mix(a,b,c)... Java needs "out" parameters!!! - // Note: recent JVMs (Sun JDK6) turn pairs of shifts (needed to do a rotate) - // into real x86 rotate instructions. - { - a -= c; a ^= (c<<4)|(c>>>-4); c += b; - b -= a; b ^= (a<<6)|(a>>>-6); a += c; - c -= b; c ^= (b<<8)|(b>>>-8); b += a; - a -= c; a ^= (c<<16)|(c>>>-16); c += b; - b -= a; b ^= (a<<19)|(a>>>-19); a += c; - c -= b; c ^= (b<<4)|(b>>>-4); b += a; - } - mixed=true; - } - - - if (!mixed) { - // final(a,b,c) - c ^= b; c -= (b<<14)|(b>>>-14); - a ^= c; a -= (c<<11)|(c>>>-11); - b ^= a; b -= (a<<25)|(a>>>-25); - c ^= b; c -= (b<<16)|(b>>>-16); - a ^= c; a -= (c<<4)|(c>>>-4); - b ^= a; b -= (a<<14)|(a>>>-14); - c ^= b; c -= (b<<24)|(b>>>-24); - } - - return c; - } - - - /**<p>This is the 64 bit version of lookup3ycs, corresponding to Bob Jenkin's - * lookup3 hashlittle2 with initval biased by -(numCodePoints<<2). It is equivalent - * to lookup3ycs in that if the high bits of initval==0, then the low bits of the - * result will be the same as lookup3ycs. - * </p> - */ - public static long lookup3ycs64(CharSequence s, int start, int end, long initval) { - int a,b,c; - a = b = c = 0xdeadbeef + (int)initval; - c += (int)(initval>>>32); - // only difference from lookup3 is that "+ (length<<2)" is missing - // since we don't know the number of code points to start with, - // and don't want to have to pre-scan the string to find out. - - int i=start; - boolean mixed=true; // have the 3 state variables been adequately mixed? - for(;;) { - if (i>= end) break; - mixed=false; - char ch; - ch = s.charAt(i++); - a += Character.isHighSurrogate(ch) && i< end ? Character.toCodePoint(ch, s.charAt(i++)) : ch; - if (i>= end) break; - ch = s.charAt(i++); - b += Character.isHighSurrogate(ch) && i< end ? Character.toCodePoint(ch, s.charAt(i++)) : ch; - if (i>= end) break; - ch = s.charAt(i++); - c += Character.isHighSurrogate(ch) && i< end ? Character.toCodePoint(ch, s.charAt(i++)) : ch; - if (i>= end) break; - - // mix(a,b,c)... Java needs "out" parameters!!! - // Note: recent JVMs (Sun JDK6) turn pairs of shifts (needed to do a rotate) - // into real x86 rotate instructions. - { - a -= c; a ^= (c<<4)|(c>>>-4); c += b; - b -= a; b ^= (a<<6)|(a>>>-6); a += c; - c -= b; c ^= (b<<8)|(b>>>-8); b += a; - a -= c; a ^= (c<<16)|(c>>>-16); c += b; - b -= a; b ^= (a<<19)|(a>>>-19); a += c; - c -= b; c ^= (b<<4)|(b>>>-4); b += a; - } - mixed=true; - } - - - if (!mixed) { - // final(a,b,c) - c ^= b; c -= (b<<14)|(b>>>-14); - a ^= c; a -= (c<<11)|(c>>>-11); - b ^= a; b -= (a<<25)|(a>>>-25); - c ^= b; c -= (b<<16)|(b>>>-16); - a ^= c; a -= (c<<4)|(c>>>-4); - b ^= a; b -= (a<<14)|(a>>>-14); - c ^= b; c -= (b<<24)|(b>>>-24); - } - - return c + (((long)b) << 32); - } - - - /** Returns the MurmurHash3_x86_32 hash. - * Original source/tests at https://github.com/yonik/java_util/ - */ - public static int murmurhash3_x86_32(byte[] data, int offset, int len, int seed) { - - final int c1 = 0xcc9e2d51; - final int c2 = 0x1b873593; - - int h1 = seed; - int roundedEnd = offset + (len & 0xfffffffc); // round down to 4 byte block - - for (int i=offset; i<roundedEnd; i+=4) { - // little endian load order - int k1 = (data[i] & 0xff) | ((data[i+1] & 0xff) << 8) | ((data[i+2] & 0xff) << 16) | (data[i+3] << 24); - k1 *= c1; - k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15); - k1 *= c2; - - h1 ^= k1; - h1 = (h1 << 13) | (h1 >>> 19); // ROTL32(h1,13); - h1 = h1*5+0xe6546b64; - } - - // tail - int k1 = 0; - - switch(len & 0x03) { - case 3: - k1 = (data[roundedEnd + 2] & 0xff) << 16; - // fallthrough - case 2: - k1 |= (data[roundedEnd + 1] & 0xff) << 8; - // fallthrough - case 1: - k1 |= (data[roundedEnd] & 0xff); - k1 *= c1; - k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15); - k1 *= c2; - h1 ^= k1; - } - - // finalization - h1 ^= len; - - // fmix(h1); - h1 ^= h1 >>> 16; - h1 *= 0x85ebca6b; - h1 ^= h1 >>> 13; - h1 *= 0xc2b2ae35; - h1 ^= h1 >>> 16; - - return h1; - } - - - - /** Returns the MurmurHash3_x86_32 hash of the UTF-8 bytes of the String without actually encoding - * the string to a temporary buffer. This is more than 2x faster than hashing the result - * of String.getBytes(). - */ - public static int murmurhash3_x86_32(CharSequence data, int offset, int len, int seed) { - - final int c1 = 0xcc9e2d51; - final int c2 = 0x1b873593; - - int h1 = seed; - - int pos = offset; - int end = offset + len; - int k1 = 0; - int k2 = 0; - int shift = 0; - int bits = 0; - int nBytes = 0; // length in UTF8 bytes - - - while (pos < end) { - int code = data.charAt(pos++); - if (code < 0x80) { - k2 = code; - bits = 8; - - /*** - // optimized ascii implementation (currently slower!!! code size?) - if (shift == 24) { - k1 = k1 | (code << 24); - - k1 *= c1; - k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15); - k1 *= c2; - - h1 ^= k1; - h1 = (h1 << 13) | (h1 >>> 19); // ROTL32(h1,13); - h1 = h1*5+0xe6546b64; - - shift = 0; - nBytes += 4; - k1 = 0; - } else { - k1 |= code << shift; - shift += 8; - } - continue; - ***/ - - } - else if (code < 0x800) { - k2 = (0xC0 | (code >> 6)) - | ((0x80 | (code & 0x3F)) << 8); - bits = 16; - } - else if (code < 0xD800 || code > 0xDFFF || pos>=end) { - // we check for pos>=end to encode an unpaired surrogate as 3 bytes. - k2 = (0xE0 | (code >> 12)) - | ((0x80 | ((code >> 6) & 0x3F)) << 8) - | ((0x80 | (code & 0x3F)) << 16); - bits = 24; - } else { - // surrogate pair - // int utf32 = pos < end ? (int) data.charAt(pos++) : 0; - int utf32 = (int) data.charAt(pos++); - utf32 = ((code - 0xD7C0) << 10) + (utf32 & 0x3FF); - k2 = (0xff & (0xF0 | (utf32 >> 18))) - | ((0x80 | ((utf32 >> 12) & 0x3F))) << 8 - | ((0x80 | ((utf32 >> 6) & 0x3F))) << 16 - | (0x80 | (utf32 & 0x3F)) << 24; - bits = 32; - } - - - k1 |= k2 << shift; - - // int used_bits = 32 - shift; // how many bits of k2 were used in k1. - // int unused_bits = bits - used_bits; // (bits-(32-shift)) == bits+shift-32 == bits-newshift - - shift += bits; - if (shift >= 32) { - // mix after we have a complete word - - k1 *= c1; - k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15); - k1 *= c2; - - h1 ^= k1; - h1 = (h1 << 13) | (h1 >>> 19); // ROTL32(h1,13); - h1 = h1*5+0xe6546b64; - - shift -= 32; - // unfortunately, java won't let you shift 32 bits off, so we need to check for 0 - if (shift != 0) { - k1 = k2 >>> (bits-shift); // bits used == bits - newshift - } else { - k1 = 0; - } - nBytes += 4; - } - - } // inner - - // handle tail - if (shift > 0) { - nBytes += shift >> 3; - k1 *= c1; - k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15); - k1 *= c2; - h1 ^= k1; - } - - // finalization - h1 ^= nBytes; - - // fmix(h1); - h1 ^= h1 >>> 16; - h1 *= 0x85ebca6b; - h1 ^= h1 >>> 13; - h1 *= 0xc2b2ae35; - h1 ^= h1 >>> 16; - - return h1; - } - - -} http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d9a661cf/ranger_solrj/src/main/java/org/apache/solr/common/util/IOUtils.java ---------------------------------------------------------------------- diff --git a/ranger_solrj/src/main/java/org/apache/solr/common/util/IOUtils.java b/ranger_solrj/src/main/java/org/apache/solr/common/util/IOUtils.java deleted file mode 100644 index c9d7274..0000000 --- a/ranger_solrj/src/main/java/org/apache/solr/common/util/IOUtils.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.solr.common.util; - -import java.io.Closeable; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -public class IOUtils { - public static Logger LOG = LoggerFactory.getLogger(IOUtils.class); - - public static void closeQuietly(Closeable closeable) { - try { - if (closeable != null) { - closeable.close(); - } - } catch (Exception e) { - LOG.error("Error while closing", e); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-ranger/blob/d9a661cf/ranger_solrj/src/main/java/org/apache/solr/common/util/IteratorChain.java ---------------------------------------------------------------------- diff --git a/ranger_solrj/src/main/java/org/apache/solr/common/util/IteratorChain.java b/ranger_solrj/src/main/java/org/apache/solr/common/util/IteratorChain.java deleted file mode 100644 index 8a0bb1d..0000000 --- a/ranger_solrj/src/main/java/org/apache/solr/common/util/IteratorChain.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.solr.common.util; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; - -/** Chain several Iterators, so that this iterates - * over all of them in sequence. - * - * @deprecated This class is no longer used by Solr, and may be removed in future versions - */ -@Deprecated -public class IteratorChain<E> implements Iterator<E> { - - private final List<Iterator<E>> iterators = new ArrayList<>(); - private Iterator<Iterator<E>> itit; - private Iterator<E> current; - - public void addIterator(Iterator<E> it) { - if(itit!=null) throw new RuntimeException("all Iterators must be added before calling hasNext()"); - iterators.add(it); - } - - @Override - public boolean hasNext() { - if(itit==null) itit = iterators.iterator(); - return recursiveHasNext(); - } - - /** test if current iterator hasNext(), and if not try the next - * one in sequence, recursively - */ - private boolean recursiveHasNext() { - // return false if we have no more iterators - if(current==null) { - if(itit.hasNext()) { - current=itit.next(); - } else { - return false; - } - } - - boolean result = current.hasNext(); - if(!result) { - current = null; - result = recursiveHasNext(); - } - - return result; - } - - /** hasNext() must ALWAYS be called before calling this - * otherwise it's a bit hard to keep track of what's happening - */ - @Override - public E next() { - if(current==null) { - throw new RuntimeException("For an IteratorChain, hasNext() MUST be called before calling next()"); - } - return current.next(); - } - - @Override - public void remove() { - // we just need this class - // to iterate in readonly mode - throw new UnsupportedOperationException(); - } - -}
