/******************************************************************************* * Copyright (c) 2007- VTT Technical Research Centre of Finland. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ /* * Created on Jan 21, 2005 * * Copyright Toni Kalajainen * * 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.simantics.utils.bytes; import org.simantics.utils.strings.EString; /** * Byte array <-> string conversions * * All conversions have length in the beginning (32-bit integer) and * followed by actual chars * * the length is in Little Endian * * @author Toni Kalajainen */ public class LEString { public static byte[] toBytes(String s) { int size = s.length(); byte array[] = new byte[size + 4]; // write length LEInt.write(size, array); // write chars for (int i=0; iarray.length) throw new IndexOutOfBoundsException(); LEInt.write(size, array, offset); for (int i=0; iarray.length) throw new IndexOutOfBoundsException(); LEInt.write(size, array); for (int i=0; iarray.length) throw new IndexOutOfBoundsException(); // read size int size = LEInt.toInt(array, offset); if (offset+4+size>array.length) throw new IndexOutOfBoundsException(); // read chars //return new String(array, 4+offset, size); char chars[] = new char[size]; for (int i=0; iarray.length) throw new IndexOutOfBoundsException(); // read size int size = LEInt.toInt(array); if (4+size>array.length) throw new IndexOutOfBoundsException(); // read chars //return new String(array, 4, size); char chars[] = new char[size]; for (int i=0; i>4) & 0xF] + result; value >>= 8; } return result; } public static void printByteArray(byte array[]) { for (int i=0; i