litesoft
@ 942
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 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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
package org.litesoft.codec; import org.litesoft.commonfoundation.base.*; import org.litesoft.commonfoundation.charstreams.*; /** * Implementation that supports an encoding model of encoded Deltas from one * character to the next with a Time based "Random" starting character. */ public class StringCodecVersion0 implements StringCodec { private final MillisecTimeSource mMillisecTimeSource; public StringCodecVersion0( MillisecTimeSource pMillisecTimeSource ) { mMillisecTimeSource = MillisecTimeSource.deNull( pMillisecTimeSource ); } public StringCodecVersion0() { this( null ); } /** * Encode the String. * * @return null if null passed in, "" if "" passed in, otherwise the Encoded String. */ public final String encode( String pUnencoded ) { if ( pUnencoded == null ) { return null; } if ( pUnencoded.length() == 0 ) { return ""; } return createHelper( new CharSourceFromSequence( pUnencoded ), getInitialCharacter() ).encode(); } /** * Decode the previously "encoded" data, null or bad encoding will return * null. * * @return null indicates that the data was either null or contained * unacceptable characters for decoding. */ public final String decode( String pEncoded ) { if ( pEncoded == null ) { return null; } if ( pEncoded.length() == 0 ) { return ""; } CharSource zCS = new CharSourceFromSequence( pEncoded ); return createHelper( zCS, zCS.getRequired() ).decode(); } protected char getInitialCharacter() { return SixBitCodec.encode( (int) (63 & mMillisecTimeSource.now()) ); } protected int determinePerCharacterAdjustment( char pInitialCharacter ) { return (pInitialCharacter & 7) + 1; } protected Helper createHelper( CharSource pCharSource, char pInitialCharacter ) { return new Helper( pCharSource, pInitialCharacter, determinePerCharacterAdjustment( pInitialCharacter ) ); } protected static class Helper { protected final StringBuilder mSB = new StringBuilder(); protected final CharSource mCharSource; protected final int mPerCharAdjustment; protected int mLastClearTextChar; protected int mDeltaRunning = 0; protected int mCharDeltasRemaining = 0; public Helper( CharSource pCharSource, char pFirstChar, int pPerCharAdjustment ) { mCharSource = pCharSource; mPerCharAdjustment = pPerCharAdjustment; mLastClearTextChar = pFirstChar; } public String encode() { mSB.append( (char) mLastClearTextChar ); while ( mCharSource.anyRemaining() ) { encode( mCharSource.get() ); } return mSB.toString(); } public String decode() { while ( mCharSource.anyRemaining() ) { if ( !decode( mCharSource.get() ) ) { return null; } } // Check for Ran off the End! return (mCharDeltasRemaining == 0) ? mSB.toString() : null; } private void encode( int chr ) { int zDelta = (chr - mLastClearTextChar) + mPerCharAdjustment; if ( zDelta < 0 ) { LLencode( -zDelta, 'a' ); } else { LLencode( zDelta, 'A' ); } mLastClearTextChar = chr; } private void LLencode( int pDelta, int pBase ) { if ( div4 <= pDelta ) { mSB.append( '4' ); pDelta = addFactor( pDelta, pBase, div4 ); pDelta = addFactor( pDelta, pBase, div3 ); pDelta = addFactor( pDelta, pBase, div2 ); } else if ( div3 <= pDelta ) { mSB.append( '3' ); pDelta = addFactor( pDelta, pBase, div3 ); pDelta = addFactor( pDelta, pBase, div2 ); } else if ( div2 <= pDelta ) { mSB.append( '2' ); pDelta = addFactor( pDelta, pBase, div2 ); } LLencodeChar( pDelta, pBase ); } private int addFactor( int pDelta, int pBase, int pDivisor ) { int zDelta = 0; while ( pDelta >= pDivisor ) { zDelta++; pDelta -= pDivisor; } LLencodeChar( zDelta, pBase ); return pDelta; } private void LLencodeChar( int pDelta, int pBase ) { mSB.append( (pDelta == 0) ? '0' : (char) (pBase + (pDelta - 1)) ); } // 0 = No Change / same letter // A-Z = Delta: +1 to +26 // a-z = Delta: -1 to -26 // 2-4 = Delta: that many letters/'0' follow, witch are in base 27 private static final int div4 = 19683; // 27 * 27 * 27; private static final int div3 = 729; // 27 * 27; private static final int div2 = 27; public boolean decode( int pChar ) { if ( mCharDeltasRemaining > 0 ) { Integer delta = LLdecode( pChar ); if ( delta == null ) { return false; } mDeltaRunning = (mDeltaRunning * 27) + delta; if ( --mCharDeltasRemaining == 0 ) { if ( !LLdecodeChar( mDeltaRunning ) ) { return false; } mDeltaRunning = 0; } return true; } if ( ('2' <= pChar) && (pChar <= '4') ) { mCharDeltasRemaining = pChar - '0'; return true; } return LLdecodeChar( LLdecode( pChar ) ); } private boolean LLdecodeChar( Integer pDelta ) { if ( pDelta == null ) { return false; } mLastClearTextChar += pDelta - mPerCharAdjustment; mSB.append( (char) mLastClearTextChar ); return true; } private Integer LLdecode( int pChar ) { if ( pChar == '0' ) { return 0; } if ( ('a' <= pChar) && (pChar <= 'z') ) { return -(1 + (pChar - 'a')); } if ( ('A' <= pChar) && (pChar <= 'Z') ) { return (1 + (pChar - 'A')); } return null; // Unacceptable Char } } } |
Commits for litesoft/trunk/Java/core/Anywhere/src/org/litesoft/codec/StringCodecVersion0.java
Revision | Author | Commited | Message |
---|---|---|---|
942 Diff | GeorgeS | Mon 02 Jun, 2014 23:41:46 +0000 | Extracting commonfoundation |
858 Diff | GeorgeS | Sun 04 Nov, 2012 18:40:40 +0000 | |
779 Diff | GeorgeS | Mon 16 Jul, 2012 04:34:33 +0000 | |
772 Diff | GeorgeS | Sun 15 Jul, 2012 16:55:51 +0000 | ! |
766 Diff | GeorgeS | Sat 14 Jul, 2012 16:38:04 +0000 | ! |
755 | GeorgeS | Sun 08 Jul, 2012 21:59:08 +0000 |