package textbender.a.u.transfer.clipboard; // Copyright 2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.io.*; import textbender.g.io.*; import textbender.g.lang.*; import textbender.o.*; /** Encodes/decodes clip-indeces to/from 'c' attribute values. * * @see * clipboard.mod */ @ThreadRestricted final class ClipIndexEncoder { ClipIndexEncoder() { byteArrayOut = new ByteArrayOutputStream( /*initial capacity*/bytesPerLong + 1 ); // + 1 spare base64Decoder = new Base64.OutputStream ( byteArrayOut, Base64.DECODE|Base64.ORDERED ); try { writer = new OutputStreamWriter( base64Decoder, "US-ASCII" ); } catch( UnsupportedEncodingException x ) { throw new RuntimeException( x ); } // not expected stringOut = new StringOutputStream ( new StringBuilder(/*initial capacity*/digitsPerLong + 1), "US-ASCII" ); // + 1 spare base64Encoder = new Base64.OutputStream ( stringOut, Base64.ENCODE|Base64.ORDERED|Base64.DONT_BREAK_LINES ); // DONT_BREAK_LINES is superfluous, given the short length of input } /** Returns the clip-index of a 'c' attribute value. * * @see #cOf(long) */ long clipIndexOf( final String c ) { byteArrayOut.reset(); try { for( int p = digitsPerLong - c.length(); p > 0; --p ) writer.write( '-' ); // restore leading 'zero' padding (to preserve framing; - 1 because of single frame pad character) writer.write( c ); // writer.write( '=' ); // the pad character that we trimmed (this is needed) [no longer] writer.flush(); } catch( IOException x ) { throw new RuntimeException( x ); } // not expected final byte[] byteArray = byteArrayOut.toByteArray(); // OPT with custom byte buffer output stream, instead of allocating a new array like this long clipIndex = 0L; // thus far for( byte b: byteArray ) { clipIndex <<= 8; long bUnsigned = 0xFFL & b; // 0..FF clipIndex |= bUnsigned; } return clipIndex; } /** Returns the clip-index encoded as a string * suitable for a 'c' attribute value. * * @see #clipIndexOf(String) */ String cOf( long clipIndex ) { if( clipIndex < 0 ) throw new IndexOutOfBoundsException(); stringOut.reset(); try { base64Encoder.write( 0 ); final int highByteIndex = 7; for( int bitIndex = highByteIndex * 8; bitIndex >= 0; bitIndex -= 8 ) // big endian { // base64Encoder.write( (byte)( clipIndex >> bitIndex )); base64Encoder.write( (int)( clipIndex >> bitIndex )); // write(int) writes the low byte } base64Encoder.flushBase64(); base64Encoder.flush(); } catch( IOException x ) { throw new RuntimeException( x ); } // not expected final StringBuilder b = stringOut.stringBuilder(); // assert b.charAt(b.length()-1) == '='; // b.deleteCharAt( b.length() - 1 ); // the trailing frame pad character (exactly 1 for 64 bits of input) // assert b.charAt(b.length()-1) != '='; while( b.charAt(0) == '-' && b.length() > 1 ) b.deleteCharAt( 0 ); // remove leading 'zero' padding return b.toString(); } // void test() // { // System.out.println( "Testing " + getClass() ); // long i = 0L; // for(; i < 130; ++i ) test( i ); // test( i = Long.MAX_VALUE ); // test( i /= 2 ); test( i /= 2 ); test( i /= 2 ); test( i /= 2 ); // test( i -= 1 ); test( i -= 1 ); test( i -= 1 ); // test( i /= 8 ); test( i /= 8 ); test( i /= 8 ); test( i /= 8 ); // test( i /= 8 ); test( i /= 8 ); test( i /= 8 ); test( i /= 8 ); // test( i -= 1 ); // test( i /= 4 ); test( i /= 4 ); test( i /= 4 ); test( i /= 4 ); // test( i /= 8 ); test( i /= 8 ); test( i /= 8 ); test( i /= 8 ); // test( i /= 8 ); test( i /= 8 ); test( i /= 8 ); test( i /= 8 ); // test( i -= 1 ); test( i -= 1 ); test( i -= 1 ); test( i -= 1 ); test( i -= 1 ); // test( 1L ); // test( 0L ); // } // void test( final long clipIndex ) // { // final String c = cOf( clipIndex ); // final long clipIndex2 = clipIndexOf( c ); // System.out.println // ( Long.toString(clipIndex) + " | " + c + " | " + Long.toString(clipIndex2) ); // assert clipIndex2 == clipIndex; // } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final Base64.OutputStream base64Encoder; private final StringOutputStream stringOut; private final OutputStreamWriter writer; private final Base64.OutputStream base64Decoder; private final ByteArrayOutputStream byteArrayOut; // ```````````````````````````````````````````````````````````````````````````````````` private static final int bytesPerLong; private static final int digitsPerLong; static { final int bitsPerLong = 64 + 8; // adding a byte to align perfectly with frames (and allow increments of 1 to encode as such, instead of jumping by 4's) and so to keep the shorter encodings as short as possible bytesPerLong = bitsPerLong / 8; final int bitsPerFrame = 24; // Base 64 outputs in frames final int bitsPerDigit = 6; // 64 = 2^6 final int digitsPerFrame = bitsPerFrame / bitsPerDigit; final int framesPerLong = (int)Math.ceil( (float)bitsPerLong / bitsPerFrame ); digitsPerLong = framesPerLong * digitsPerFrame; } }