Android Open Source - rfcx-guardian-android U T F8 Modified






From Project

Back to project page rfcx-guardian-android.

License

The source code is released under:

Apache License

If you think the Android project rfcx-guardian-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2010  Preston Lacey http://javaflacencoder.sourceforge.net/
 * All Rights Reserved.// w w  w. java 2  s  .  c o  m
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package net.sourceforge.javaFlacEncoder;

/**
 * This is a utility class that provides methods to both encode to and decode
 * from the extended version of UTF8 used by the FLAC format. All functions
 * should work with standard UTF8 as well, since this only extends it to handle
 * larger input values.
 *
 * @author Preston Lacey
 */
public class UTF8Modified {
  static final long oneByteLimit = (long)Math.pow(2, 7);
  static final long twoByteLimit = (long)Math.pow(2, 11);
  static final long threeByteLimit = (long)Math.pow(2, 16);
  static final long fourByteLimit = (long)Math.pow(2, 21);
  static final long fiveByteLimit = (long)Math.pow(2, 26);
  static final long sixByteLimit = (long)Math.pow(2, 31);
  static final long sevenByteLimit = (long)Math.pow(2, 36);
  static long[] limits = {
    oneByteLimit,
    twoByteLimit,
    threeByteLimit,
    fourByteLimit,
    fiveByteLimit,
    sixByteLimit,
    sevenByteLimit
  };

  /** For debugging: Higher value equals more output, generally by increments
   * of 10 */
  public static int DEBUG_LEV = 0;

  /**
   * Constructor. This Class provides only static methods and static fields.
   */
  public UTF8Modified() {
  }

  /**
   * Decode an extended UTF8(as used in FLAC), to a long value.
   * @param input extended UTF8 encoded value.
   * @return value represented by the UTF8 input.
   */
  public static long decodeFromExtendedUTF8(byte[] input) {
    int leadOnes = 0;
    int leadMask = 128;
    int work = input[0];
    while((work & leadMask) > 0) {
      leadOnes++;
      work = work << 1;
    }
    int valMask = 255 >>> (leadOnes+1);
    long val = input[0] & valMask;
    for(int i = 1; i < leadOnes; i++) {
      int midMask = 0x3F;
      val = val << 6;
      val = (input[i] & midMask) | val;
    }
    return val;
  }

  /**
   * Convert a value to an extended UTF8 format(as used in FLAC).
   * @param value value to convert to extended UTF8(value must be positive
   * and 36 bits or less in size)
   * @return extended UTF8 encoded value(array size is equal to the number of
   * usable bytes)
   */
  public static byte[] convertToExtendedUTF8(long value) {
    //calculate bytes needed
    int bytesNeeded = 1;
    for(int i = 0; i < 7; i++) {
      if(value >= limits[i] ) {
        bytesNeeded++;
      }
    }
    //create space
    byte [] result = new byte[bytesNeeded];
    int byteIndex = 0;
    int inputIndex = 0;
    int bytesLeft = bytesNeeded;
    while(bytesLeft > 1) {
      int midByteMarker = 0x80;//10 in leftmost bits
      int midByteMask = 0x3F;//00111111
      int val = ((int)(value >>> inputIndex) & midByteMask) | midByteMarker;
      result[byteIndex++] = (byte)val;
      inputIndex += 6;
      bytesLeft--;
    }
    int onesNeeded = inputIndex/6;
    if(onesNeeded > 0)
      onesNeeded++;
    int startMask = 255 >>> (onesNeeded + 1);
    int ones = 255 << (8-onesNeeded);
    int val = ((int)(value >>> inputIndex) & startMask) | ones;
    result[byteIndex++] = (byte)val;

    byte[] finalResult = new byte[bytesNeeded];
    for(int i = 0; i < bytesNeeded; i++) {
      int sourceIndex = bytesNeeded-1-i;
      int destIndex = i;
      finalResult[destIndex] = result[sourceIndex];
    }
    if(DEBUG_LEV > 10) {
      System.err.print("input:result_length:result :: " +value+":"+finalResult.length+"::");
      for(int i = 0; i < finalResult.length; i++)
        System.err.print(Integer.toHexString(finalResult[i])+":");
      System.err.println();
    }
    return finalResult;
  }
}




Java Source Code List

net.sourceforge.javaFlacEncoder.ArrayRecycler.java
net.sourceforge.javaFlacEncoder.BlockEncodeRequest.java
net.sourceforge.javaFlacEncoder.BlockThreadManager.java
net.sourceforge.javaFlacEncoder.CRC16.java
net.sourceforge.javaFlacEncoder.CRC8.java
net.sourceforge.javaFlacEncoder.ChannelData.java
net.sourceforge.javaFlacEncoder.EncodedElement_32.java
net.sourceforge.javaFlacEncoder.EncodedElement.java
net.sourceforge.javaFlacEncoder.EncodingConfiguration.java
net.sourceforge.javaFlacEncoder.FLACEncoder.java
net.sourceforge.javaFlacEncoder.FLACFileOutputStream.java
net.sourceforge.javaFlacEncoder.FLACOutputStream.java
net.sourceforge.javaFlacEncoder.FLACStreamController.java
net.sourceforge.javaFlacEncoder.FLACStreamIdentifier.java
net.sourceforge.javaFlacEncoder.FLACStreamOutputStream.java
net.sourceforge.javaFlacEncoder.FLAC_ConsoleFileEncoder.java
net.sourceforge.javaFlacEncoder.FLAC_FileEncoder.java
net.sourceforge.javaFlacEncoder.FLAC_MD5.java
net.sourceforge.javaFlacEncoder.FrameHeader.java
net.sourceforge.javaFlacEncoder.FrameThread.java
net.sourceforge.javaFlacEncoder.Frame.java
net.sourceforge.javaFlacEncoder.LPC.java
net.sourceforge.javaFlacEncoder.MetadataBlockHeader.java
net.sourceforge.javaFlacEncoder.MetadataBlockStreamInfo.java
net.sourceforge.javaFlacEncoder.RiceEncoder.java
net.sourceforge.javaFlacEncoder.StreamConfiguration.java
net.sourceforge.javaFlacEncoder.Subframe_Constant.java
net.sourceforge.javaFlacEncoder.Subframe_Fixed.java
net.sourceforge.javaFlacEncoder.Subframe_LPC.java
net.sourceforge.javaFlacEncoder.Subframe_Verbatim.java
net.sourceforge.javaFlacEncoder.Subframe.java
net.sourceforge.javaFlacEncoder.UTF8Modified.java
org.rfcx.guardian.RfcxGuardianPrefs.java
org.rfcx.guardian.RfcxGuardian.java
org.rfcx.guardian.activity.MainActivity.java
org.rfcx.guardian.activity.PrefsActivity.java
org.rfcx.guardian.api.ApiCore.java
org.rfcx.guardian.audio.AudioCore.java
org.rfcx.guardian.database.AlertDb.java
org.rfcx.guardian.database.AudioDb.java
org.rfcx.guardian.database.DeviceStateDb.java
org.rfcx.guardian.database.SmsDb.java
org.rfcx.guardian.device.AirplaneMode.java
org.rfcx.guardian.device.CpuUsage.java
org.rfcx.guardian.device.DeviceState.java
org.rfcx.guardian.intentservice.ApiCheckInTriggerIntentService.java
org.rfcx.guardian.intentservice.AudioEncodeIntentService.java
org.rfcx.guardian.intentservice.ServiceMonitorIntentService.java
org.rfcx.guardian.receiver.AirplaneModeReceiver.java
org.rfcx.guardian.receiver.BootReceiver.java
org.rfcx.guardian.receiver.ConnectivityReceiver.java
org.rfcx.guardian.receiver.SmsReceiver.java
org.rfcx.guardian.service.ApiCheckInService.java
org.rfcx.guardian.service.AudioCaptureService.java
org.rfcx.guardian.service.CarrierCodeService.java
org.rfcx.guardian.service.DeviceStateService.java
org.rfcx.guardian.telecom.CarrierInteraction.java
org.rfcx.guardian.utility.DateTimeUtils.java
org.rfcx.guardian.utility.DeviceGuid.java
org.rfcx.guardian.utility.ExtAudioRecorderModified.java
org.rfcx.guardian.utility.FileUtils.java
org.rfcx.guardian.utility.HttpGet.java
org.rfcx.guardian.utility.HttpPostMultipart.java
org.rfcx.guardian.utility.TimeOfDay.java