| 1 | /* |
| 2 | * This file is part of JON. |
| 3 | * |
| 4 | * JON is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Lesser General Public License as published by |
| 6 | * the Free Software Foundation; either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * JON is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public License |
| 15 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | * |
| 17 | * Copyright 2007 Michael Doberenz |
| 18 | */ |
| 19 | package org.fuwjax.jon.type; |
| 20 | |
| 21 | import org.fuwjax.jon.ClassCastStrategy; |
| 22 | import org.fuwjax.jon.ObjectAccessException; |
| 23 | import org.fuwjax.jon.ReferenceStrategy; |
| 24 | import org.fuwjax.jon.accessor.Literal; |
| 25 | import org.fuwjax.jon.accessor.LiteralAccessor; |
| 26 | import org.fuwjax.jon.accessor.Symbol; |
| 27 | import org.fuwjax.util.BaseConverter; |
| 28 | |
| 29 | /** |
| 30 | * An abstraction layer over byte array. |
| 31 | * @author michaeldoberenz |
| 32 | */ |
| 33 | public class ByteArrayType extends AbstractIndirectType implements LiteralAccessor{ |
| 34 | private static final BaseConverter CONVERTER = new BaseConverter( |
| 35 | "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_$"); //$NON-NLS-1$ |
| 36 | private static final int STRING_BLOCK_LENGTH = 4; |
| 37 | private static final int BYTE_BLOCK_LENGTH = 3; |
| 38 | private static final int BYTE_WIDTH = 8; |
| 39 | private static final int BYTE_MASK = (1 << BYTE_WIDTH) - 1; |
| 40 | |
| 41 | /** |
| 42 | * Creates an abstraction layer for byte[]. |
| 43 | */ |
| 44 | public ByteArrayType(){ |
| 45 | super(byte[].class); |
| 46 | } |
| 47 | |
| 48 | public ClassCastStrategy getClassCastStrategy(){ |
| 49 | return ClassCastStrategy.AlwaysCast; |
| 50 | } |
| 51 | |
| 52 | public ReferenceStrategy getReferenceStrategy(){ |
| 53 | return ReferenceStrategy.AssignedReference; |
| 54 | } |
| 55 | |
| 56 | public Symbol getSymbolSpec(){ |
| 57 | return Symbol.Literal; |
| 58 | } |
| 59 | |
| 60 | public Object createInstance(final CharSequence literal, final Object source) throws ObjectAccessException{ |
| 61 | final int stringLength = literal.length(); |
| 62 | final byte[] bytes = createByteArray(stringLength, source); |
| 63 | for(int stringPos = 0; stringPos < stringLength; stringPos += STRING_BLOCK_LENGTH){ |
| 64 | final int blockPos = stringPos / STRING_BLOCK_LENGTH * BYTE_BLOCK_LENGTH; |
| 65 | final int stringBlockLength = Math.min(STRING_BLOCK_LENGTH, stringLength - stringPos); |
| 66 | final CharSequence blockString = literal.subSequence(stringPos, stringPos + stringBlockLength); |
| 67 | final int block = CONVERTER.toInt(blockString); |
| 68 | for(int blockOffset = 0; blockOffset < stringBlockLength - 1; ++blockOffset){ |
| 69 | bytes[blockPos + blockOffset] = (byte)((block >> (blockOffset * BYTE_WIDTH)) & BYTE_MASK); |
| 70 | } |
| 71 | } |
| 72 | return bytes; |
| 73 | } |
| 74 | |
| 75 | private byte[] createByteArray(final int length, final Object source){ |
| 76 | if(source instanceof byte[]){ |
| 77 | return (byte[])source; |
| 78 | } |
| 79 | if(length <= 0){ |
| 80 | return new byte[0]; |
| 81 | } |
| 82 | final int size = (length - 1) / STRING_BLOCK_LENGTH * BYTE_BLOCK_LENGTH + ((length - 1) % STRING_BLOCK_LENGTH); |
| 83 | return new byte[size]; |
| 84 | } |
| 85 | |
| 86 | public Literal getLiteral(){ |
| 87 | return Literal.String; |
| 88 | } |
| 89 | |
| 90 | public CharSequence toString(final Object object) throws ObjectAccessException{ |
| 91 | final StringBuilder builder = new StringBuilder(); |
| 92 | final byte[] bytes = (byte[])object; |
| 93 | for(int i = 0; i < bytes.length; i += BYTE_BLOCK_LENGTH){ |
| 94 | int block = 0; |
| 95 | int blockLength = 0; |
| 96 | for(; isValidIndex(bytes.length, i, blockLength); ++blockLength){ |
| 97 | block |= bytes[i + blockLength] << (BYTE_WIDTH * blockLength); |
| 98 | } |
| 99 | final CharSequence blockString = CONVERTER.toCharSequence(block, blockLength + 1); |
| 100 | builder.append(blockString); |
| 101 | } |
| 102 | return builder; |
| 103 | } |
| 104 | |
| 105 | private static boolean isValidIndex(final int arrayLength, final int blockIndex, final int blockPosition){ |
| 106 | return blockPosition < BYTE_BLOCK_LENGTH && blockPosition + blockIndex < arrayLength; |
| 107 | } |
| 108 | } |