public static String byteArrayToBase64(byte[] a) { return byteArrayToBase64(a, 0, a.length); }
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)); int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt); int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt); result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4)); int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt); result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
public Field createField(SchemaField field, Object val, float boost) { if (val == null) return null; if (!field.stored()) { log.trace("Ignoring unstored binary field: " + field); return null; } byte[] buf = null; int offset = 0, len = 0; if (val instanceof byte[]) { buf = (byte[]) val; len = buf.length; } else if (val instanceof ByteBuffer && ((ByteBuffer)val).hasArray()) { ByteBuffer byteBuf = (ByteBuffer) val; buf = byteBuf.array(); offset = byteBuf.position(); len = byteBuf.limit() - byteBuf.position(); } else { String strVal = val.toString(); //the string has to be a base64 encoded string buf = Base64.base64ToByteArray(strVal); offset = 0; len = buf.length; } Field f = new Field(field.getName(), buf, offset, len, getFieldStore(field, null)); f.setBoost(boost); return f; } }
@Override public IndexableField createField(SchemaField field, Object val /*, float boost*/) { if (val == null) return null; if (!field.stored()) { return null; } byte[] buf = null; int offset = 0, len = 0; if (val instanceof byte[]) { buf = (byte[]) val; len = buf.length; } else if (val instanceof ByteBuffer && ((ByteBuffer)val).hasArray()) { ByteBuffer byteBuf = (ByteBuffer) val; buf = byteBuf.array(); offset = byteBuf.position(); len = byteBuf.limit() - byteBuf.position(); } else { String strVal = val.toString(); //the string has to be a base64 encoded string buf = Base64.base64ToByteArray(strVal); offset = 0; len = buf.length; } Field f = new org.apache.lucene.document.BinaryDocValuesField(field.getName(), new BytesRef(buf, offset, len)); // Field f = new org.apache.lucene.document.StoredField(field.getName(), buf, offset, len); //f.setBoost(boost); return f; } }
public static String byteArrayToBase64(byte[] a) { return byteArrayToBase64(a, 0, a.length); }
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)); int ch0 = base64toInt(s.charAt(inCursor++), alphaToInt); int ch1 = base64toInt(s.charAt(inCursor++), alphaToInt); result[outCursor++] = (byte) ((ch0 << 2) | (ch1 >> 4)); int ch2 = base64toInt(s.charAt(inCursor++), alphaToInt); result[outCursor++] = (byte) ((ch1 << 4) | (ch2 >> 2));
private String toBase64String(ByteBuffer buf) { return Base64.byteArrayToBase64(buf.array(), buf.position(), buf.limit()-buf.position()); }
private String toBase64String(ByteBuffer buf) { return Base64.byteArrayToBase64(buf.array(), buf.position(), buf.limit() - buf.position()); }
default void writeByteArr(String name, byte[] buf, int offset, int len) throws IOException { writeStr(name, Base64.byteArrayToBase64(buf, offset, len), false); }
/** * Performs login operation to ACT, setting Cookie and CSRF. * @throws IOException */ private void actLogin() throws IOException { Config loginConf = ConfigFactory .parseFile(new File("credentials.conf")); URL login = new URL( loginConf.getString( "act.login" ) ); LOG.info("Logging in at " + login); HttpURLConnection connection = ( HttpURLConnection ) login.openConnection(); StringBuilder credentials = new StringBuilder(); credentials.append( loginConf.getString( "act.username" ) ); credentials.append( ":" ); credentials.append( loginConf.getString( "act.password" ) ); connection.setRequestProperty("Authorization", "Basic " + Base64.byteArrayToBase64(credentials.toString().getBytes())); connection.setRequestProperty("Content-Type", "text/plain"); Scanner scanner; if( connection.getResponseCode() != 200 ) { scanner = new Scanner( connection.getErrorStream() ); scanner.useDelimiter( "\\Z" ); throw new IOException( scanner.next() ); } else { scanner = new Scanner( connection.getInputStream() ); } scanner.useDelimiter( "\\Z" ); this.csrf = scanner.next(); this.cookie = connection.getHeaderField( "set-cookie" ); }
/** * Performs login operation to ACT, setting Cookie and CSRF. * @throws IOException */ private void actLogin() throws IOException { Config loginConf = ConfigFactory .parseFile(new File("credentials.conf")); URL login = new URL( loginConf.getString( "act.login" ) ); LOG.info("Logging in at " + login); HttpURLConnection connection = ( HttpURLConnection ) login.openConnection(); StringBuilder credentials = new StringBuilder(); credentials.append( loginConf.getString( "act.username" ) ); credentials.append( ":" ); credentials.append( loginConf.getString( "act.password" ) ); connection.setRequestProperty("Authorization", "Basic " + Base64.byteArrayToBase64(credentials.toString().getBytes())); connection.setRequestProperty("Content-Type", "text/plain"); Scanner scanner; if( connection.getResponseCode() != 200 ) { scanner = new Scanner( connection.getErrorStream() ); scanner.useDelimiter( "\\Z" ); throw new IOException( scanner.next() ); } else { scanner = new Scanner( connection.getInputStream() ); } scanner.useDelimiter( "\\Z" ); this.csrf = scanner.next(); this.cookie = connection.getHeaderField( "set-cookie" ); }
private static void writeVal(Writer writer, String name, Object v, String update) throws IOException { if (v instanceof Date) { v = ((Date)v).toInstant().toString(); } else if (v instanceof byte[]) { byte[] bytes = (byte[]) v; v = Base64.byteArrayToBase64(bytes, 0, bytes.length); } else if (v instanceof ByteBuffer) { ByteBuffer bytes = (ByteBuffer) v; v = Base64.byteArrayToBase64(bytes.array(), bytes.position(),bytes.limit() - bytes.position()); } XML.Writable valWriter = null; if(v instanceof SolrInputDocument) { final SolrInputDocument solrDoc = (SolrInputDocument) v; valWriter = (writer1) -> writeXML(solrDoc, writer1); } else if(v != null) { final Object val = v; valWriter = (writer1) -> XML.escapeCharData(val.toString(), writer1); } if (update == null) { if (v != null) { XML.writeXML(writer, "field", valWriter, "name", name); } } else { if (v == null) { XML.writeXML(writer, "field", (XML.Writable) null, "name", name, "update", update, "null", true); } else { XML.writeXML(writer, "field", valWriter, "name", name, "update", update); } } }
public static void writeXML( SolrInputDocument doc, Writer writer ) throws IOException { writer.write("<doc boost=\""+doc.getDocumentBoost()+"\">"); for( SolrInputField field : doc ) { float boost = field.getBoost(); String name = field.getName(); for( Object v : field ) { if (v instanceof Date) { v = DateUtil.getThreadLocalDateFormat().format( (Date)v ); }else if (v instanceof byte[]) { byte[] bytes = (byte[]) v; v = Base64.byteArrayToBase64(bytes, 0,bytes.length); } else if (v instanceof ByteBuffer) { ByteBuffer bytes = (ByteBuffer) v; v = Base64.byteArrayToBase64(bytes.array(), bytes.position(),bytes.limit() - bytes.position()); } if( boost != 1.0f ) { XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost ); } else if (v != null) { XML.writeXML(writer, "field", v.toString(), "name", name ); } // only write the boost for the first multi-valued field // otherwise, the used boost is the product of all the boost values boost = 1.0f; } } writer.write("</doc>"); }
private void setBasicAuthHeader(SolrRequest request, HttpRequestBase method) throws UnsupportedEncodingException { if (request.getBasicAuthUser() != null && request.getBasicAuthPassword() != null) { String userPass = request.getBasicAuthUser() + ":" + request.getBasicAuthPassword(); String encoded = Base64.byteArrayToBase64(userPass.getBytes(UTF_8)); method.setHeader(new BasicHeader("Authorization", "Basic " + encoded)); } }
private static void writeVal(Writer writer, float boost, String name, Object v, String update) throws IOException { if (v instanceof Date) { v = DateUtil.getThreadLocalDateFormat().format( (Date)v ); } else if (v instanceof byte[]) { byte[] bytes = (byte[]) v; v = Base64.byteArrayToBase64(bytes, 0, bytes.length); } else if (v instanceof ByteBuffer) { ByteBuffer bytes = (ByteBuffer) v; v = Base64.byteArrayToBase64(bytes.array(), bytes.position(),bytes.limit() - bytes.position()); } if (update == null) { if( boost != 1.0f ) { XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost); } else if (v != null) { XML.writeXML(writer, "field", v.toString(), "name", name ); } } else { if( boost != 1.0f ) { XML.writeXML(writer, "field", v.toString(), "name", name, "boost", boost, "update", update); } else { if (v == null) { XML.writeXML(writer, "field", null, "name", name, "update", update, "null", true); } else { XML.writeXML(writer, "field", v.toString(), "name", name, "update", update); } } } }
out.writeObject(tupleStream); byte[] bytes = bout.toByteArray(); String encoded = Base64.byteArrayToBase64(bytes, 0, bytes.length); pushStream = URLEncoder.encode(encoded, "UTF-8"); } else {