From 13d87e3d40bf9fb1356eba5eed591ce665d05a50 Mon Sep 17 00:00:00 2001 From: rcollier Date: Sat, 20 Apr 2013 21:55:27 +0000 Subject: [PATCH] SMACK-361 Changed default for filename encoding to Base 32 (which should work on all file systems) and added a Base 64 filename and url safe implementation of the StringEncoder. git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/branches/smack_3_3_0@13619 b35dd754-fafc-0310-a699-88a17e54d16e --- .../org/jivesoftware/smack/util/Base64.java | 88 +------------------ .../smack/util/Base64Encoder.java | 1 + .../smack/util/Base64FileUrlEncoder.java | 48 ++++++++++ .../cache/SimpleDirectoryPersistentCache.java | 27 +++--- .../entitycaps/EntityCapsManagerTest.java | 3 +- 5 files changed, 67 insertions(+), 100 deletions(-) create mode 100644 source/org/jivesoftware/smack/util/Base64FileUrlEncoder.java diff --git a/source/org/jivesoftware/smack/util/Base64.java b/source/org/jivesoftware/smack/util/Base64.java index 940c55118..ba6eb371f 100644 --- a/source/org/jivesoftware/smack/util/Base64.java +++ b/source/org/jivesoftware/smack/util/Base64.java @@ -7,66 +7,9 @@ package org.jivesoftware.smack.util; /** - *

Encodes and decodes to and from Base64 notation.

- *

Homepage: http://iharder.net/base64.

+ *

Encodes and decodes to and from Base64 notation.

+ * This code was obtained from http://iharder.net/base64

* - *

- * Change Log: - *

- * - * - *

- * I am placing this code in the Public Domain. Do with it as you will. - * This software comes with no guarantees or warranties but with - * plenty of well-wishing instead! - * Please visit http://iharder.net/base64 - * periodically to check for updates or to contribute improvements. - *

* * @author Robert Harder * @author rob@iharder.net @@ -368,33 +311,6 @@ public class Base64 /** Defeats instantiation. */ private Base64(){} - - /** - * Encodes or decodes two files from the command line; - * feel free to delete this method (in fact you probably should) - * if you're embedding this code into a larger program. - */ - public final static void main( String[] args ) - { - if( args.length < 3 ){ - usage("Not enough arguments."); - } // end if: args.length < 3 - else { - String flag = args[0]; - String infile = args[1]; - String outfile = args[2]; - if( flag.equals( "-e" ) ){ - Base64.encodeFileToFile( infile, outfile ); - } // end if: encode - else if( flag.equals( "-d" ) ) { - Base64.decodeFileToFile( infile, outfile ); - } // end else if: decode - else { - usage( "Unknown flag: " + flag ); - } // end else - } // end else - } // end main - /** * Prints command line usage. * diff --git a/source/org/jivesoftware/smack/util/Base64Encoder.java b/source/org/jivesoftware/smack/util/Base64Encoder.java index 8d29f12c5..d53c0ed08 100644 --- a/source/org/jivesoftware/smack/util/Base64Encoder.java +++ b/source/org/jivesoftware/smack/util/Base64Encoder.java @@ -16,6 +16,7 @@ package org.jivesoftware.smack.util; /** + * A Base 64 encoding implementation. * @author Florian Schmaus */ public class Base64Encoder implements StringEncoder { diff --git a/source/org/jivesoftware/smack/util/Base64FileUrlEncoder.java b/source/org/jivesoftware/smack/util/Base64FileUrlEncoder.java new file mode 100644 index 000000000..190b37439 --- /dev/null +++ b/source/org/jivesoftware/smack/util/Base64FileUrlEncoder.java @@ -0,0 +1,48 @@ +/** + * All rights reserved. Licensed 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.jivesoftware.smack.util; + + +/** + * A Base 64 encoding implementation that generates filename and Url safe encodings. + * + *

+ * Note: This does NOT produce standard Base 64 encodings, but a variant as defined in + * Section 4 of RFC3548: + * http://www.faqs.org/rfcs/rfc3548.html. + * + * @author Robin Collier + */ +public class Base64FileUrlEncoder implements StringEncoder { + + private static Base64FileUrlEncoder instance = new Base64FileUrlEncoder(); + + private Base64FileUrlEncoder() { + // Use getInstance() + } + + public static Base64FileUrlEncoder getInstance() { + return instance; + } + + public String encode(String s) { + return Base64.encodeBytes(s.getBytes(), Base64.URL_SAFE); + } + + public String decode(String s) { + return new String(Base64.decode(s, Base64.URL_SAFE)); + } + +} diff --git a/source/org/jivesoftware/smackx/entitycaps/cache/SimpleDirectoryPersistentCache.java b/source/org/jivesoftware/smackx/entitycaps/cache/SimpleDirectoryPersistentCache.java index 329e4dce6..ae0e11633 100644 --- a/source/org/jivesoftware/smackx/entitycaps/cache/SimpleDirectoryPersistentCache.java +++ b/source/org/jivesoftware/smackx/entitycaps/cache/SimpleDirectoryPersistentCache.java @@ -27,6 +27,7 @@ import java.io.StringReader; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.provider.IQProvider; +import org.jivesoftware.smack.util.Base32Encoder; import org.jivesoftware.smack.util.Base64Encoder; import org.jivesoftware.smack.util.StringEncoder; import org.jivesoftware.smackx.entitycaps.EntityCapsManager; @@ -47,19 +48,20 @@ import org.xmlpull.v1.XmlPullParserException; public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache { private File cacheDir; - private StringEncoder stringEncoder; + private StringEncoder filenameEncoder; /** * Creates a new SimpleDirectoryPersistentCache Object. Make sure that the * cacheDir exists and that it's an directory. - * - * If your cacheDir is case insensitive then make sure to set the - * StringEncoder to Base32. + *

+ * Default filename encoder {@link Base32Encoder}, as this will work on all + * filesystems, both case sensitive and case insensitive. It does however + * produce longer filenames. * * @param cacheDir */ public SimpleDirectoryPersistentCache(File cacheDir) { - this(cacheDir, Base64Encoder.getInstance()); + this(cacheDir, Base32Encoder.getInstance()); } /** @@ -67,26 +69,25 @@ public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache * cacheDir exists and that it's an directory. * * If your cacheDir is case insensitive then make sure to set the - * StringEncoder to Base32. + * StringEncoder to {@link Base32Encoder} (which is the default). * - * @param cacheDir - * @param stringEncoder + * @param cacheDir The directory where the cache will be stored. + * @param filenameEncoder Encodes the node string into a filename. */ - public SimpleDirectoryPersistentCache(File cacheDir, StringEncoder stringEncoder) { + public SimpleDirectoryPersistentCache(File cacheDir, StringEncoder filenameEncoder) { if (!cacheDir.exists()) throw new IllegalStateException("Cache directory \"" + cacheDir + "\" does not exist"); if (!cacheDir.isDirectory()) throw new IllegalStateException("Cache directory \"" + cacheDir + "\" is not a directory"); this.cacheDir = cacheDir; - this.stringEncoder = stringEncoder; + this.filenameEncoder = filenameEncoder; } @Override public void addDiscoverInfoByNodePersistent(String node, DiscoverInfo info) { - String filename = stringEncoder.encode(node); + String filename = filenameEncoder.encode(node); File nodeFile = new File(cacheDir, filename); - try { if (nodeFile.createNewFile()) writeInfoToFile(nodeFile, info); @@ -99,7 +100,7 @@ public class SimpleDirectoryPersistentCache implements EntityCapsPersistentCache public void replay() throws IOException { File[] files = cacheDir.listFiles(); for (File f : files) { - String node = stringEncoder.decode(f.getName()); + String node = filenameEncoder.decode(f.getName()); DiscoverInfo info = restoreInfoFromFile(f); if (info == null) continue; diff --git a/test-unit/org/jivesoftware/smackx/entitycaps/EntityCapsManagerTest.java b/test-unit/org/jivesoftware/smackx/entitycaps/EntityCapsManagerTest.java index 5ae4642a9..158fe466b 100644 --- a/test-unit/org/jivesoftware/smackx/entitycaps/EntityCapsManagerTest.java +++ b/test-unit/org/jivesoftware/smackx/entitycaps/EntityCapsManagerTest.java @@ -10,6 +10,7 @@ import java.util.LinkedList; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.util.Base32Encoder; import org.jivesoftware.smack.util.Base64Encoder; +import org.jivesoftware.smack.util.Base64FileUrlEncoder; import org.jivesoftware.smack.util.StringEncoder; import org.jivesoftware.smackx.FormField; import org.jivesoftware.smackx.entitycaps.EntityCapsManager; @@ -37,7 +38,7 @@ public class EntityCapsManagerTest { @Test public void testSimpleDirectoryCacheBase64() throws IOException { EntityCapsManager.persistentCache = null; - testSimpleDirectoryCache(Base64Encoder.getInstance()); + testSimpleDirectoryCache(Base64FileUrlEncoder.getInstance()); } @Test