// Takes and image_id, pulls in the image from cache, and goes about // encoding it to put it into the database in a transaction. The last // query in the transaction records the image having been stored. protected void storeImage(int image_id) throws Exception { PhotoImage p = new PhotoImage(image_id); SpyDB pdb = getDB(); Connection db = null; Statement st = null; Vector v = p.getImage(); System.err.println( "Storer: Got image for " + image_id + " " + v.size() + " lines of data to store."); try { int i = 0, n = 0; db = pdb.getConn(); db.setAutoCommit(false); st = db.createStatement(); BASE64Encoder base64 = new BASE64Encoder(); String data = ""; for (; i < v.size(); i++) { String tmp = base64.encodeBuffer((byte[]) v.elementAt(i)); tmp = tmp.trim(); if (data.length() < 2048) { data += tmp + "\n"; } else { storeQuery(image_id, n, st, data); data = tmp; n++; } } // OK, this is sick, but another one right now for the spare. if (data.length() > 0) { System.err.println("Storer: Storing spare."); storeQuery(image_id, n, st, data); n++; } System.err.println("Storer: Stored " + n + " lines of data for " + image_id + "."); st.executeUpdate( "update upload_log set stored=datetime(now())\n" + "\twhere photo_id = " + image_id); db.commit(); // Go ahead and generate a thumbnail. p.getThumbnail(); } catch (Exception e) { // If anything happens, roll it back. if (st != null) { try { db.rollback(); } catch (Exception e3) { // Nothing } } } finally { if (db != null) { try { db.setAutoCommit(true); } catch (Exception e) { System.err.println("Error: " + e); } } pdb.freeDBConn(); } }
public void testEncodeString() { String userName = "******"; String password = "******"; String result = BASE64Encoder.encode((userName + ":" + password)); assertEquals( "YSByZWFsbHkgc2VjcmV0ICEgTG9naW5AOmFuIF5ldmVuIG1vcmUmIHNlY3IzdCBwYXNzd2Q=", result); }
@SuppressWarnings("restriction") public static String encode(byte[] raw) { BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(raw); }