/// <summary> /// Convert Int buffer to ShortBuffer. /// </summary> /// <param name="intBuffer">source buffer</param> /// <returns>ShortBuffer</returns> public static ShortBuffer ConvertToShortBuffer(IntBuffer intBuffer) { ShortBuffer shortBuffer = CreateDirectShortBuffer(intBuffer.Capacity()); for (int i = 0; i < intBuffer.Capacity(); i++) { shortBuffer.Put(i, (short)intBuffer.Get()); } return(shortBuffer); }
public static int BinarySearch(string key, bool broad) { return(BinarySearch(key, 0, entries.Capacity() - 1, broad)); }
public static void LoadDict(Context context) { try { bool resaveEntries = false; dictParts = new List <byte[]>(); dictIndexes = new List <int>(); File dictFd = new File(context.FilesDir, "dict.db"); if (!dictFd.Exists()) { // || dictFd.length() != 4961308) { System.Console.WriteLine("DOES NOT EXIST!!!!!"); CopyFile(context, "dict.db"); dictFd = new File(context.FilesDir, "dict.db"); resaveEntries = true; } dictFile = new RandomAccessFile(dictFd, "r"); File idxFd = new File(context.FilesDir, "idx.db"); if (!idxFd.Exists()) { // || idxFd.length() != 3145553) { CopyFile(context, "idx.db"); idxFd = new File(context.FilesDir, "idx.db"); resaveEntries = true; } FileInputStream idxBuf = new FileInputStream(idxFd); if (!new File(context.FilesDir, "entries.bin").Exists() || !new File(context.FilesDir, "parts.bin").Exists()) { resaveEntries = true; } entries = IntBuffer.Allocate(1649830); int index = 0; //System.Console.WriteLine("LoadDict STEP 1"); if (idxBuf != null) { int readLen, offset = 0, partLen = 200000; byte[] dictPart = new byte[partLen]; int totalRead = 0; int totalLen = (int)idxFd.Length(); while (totalRead < totalLen && (readLen = idxBuf.Read(dictPart, offset, dictPart.Length - offset)) > 0) { //System.Console.WriteLine("LoadDict \ntotalRead = " + totalRead + "\ntotalLen = " + totalLen + "\nreadLen = " + readLen + "\nidxBuf.Read = " + idxBuf.Read(dictPart, offset, dictPart.Length - offset)); totalRead += readLen; int j = offset + readLen - 1; byte[] newDictPart = null; if (readLen == partLen - offset) { //System.Console.WriteLine("LoadDict STEP 4.1 " + dictPart[j] + " :: j => " + j); while (dictPart[j] > 0) { //System.Console.WriteLine("j = " + j + "\ndictPart[j] = " + dictPart[j]); j--; } //System.Console.WriteLine("LoadDict STEP 4.2"); while (dictPart[j] < 0) { System.Console.WriteLine("j = " + j); j--; } //System.Console.WriteLine("LoadDict STEP 4.3"); offset = partLen - j - 1; //System.Console.WriteLine("LoadDict STEP 4.4"); newDictPart = new byte[Math.Min(totalLen - totalRead + offset, partLen)]; //System.Console.WriteLine("LoadDict STEP 4.5"); Java.Lang.JavaSystem.Arraycopy(dictPart, j + 1, newDictPart, 0, offset); //Array.Copy(dictPart, j + 1, newDictPart, 0, offset); } else { offset = 0; } //System.Console.WriteLine("LoadDict STEP 5"); if (resaveEntries) { dictIndexes.Add(index); //System.Console.WriteLine("LoadDict STEP 6"); int i = 0; while (i <= j) { entries.Put(index++, i); while (i <= j && dictPart[i] < 0) { i++; } while (i <= j && dictPart[i] >= 0) { i++; } } } //System.Console.WriteLine("LoadDict STEP 7"); dictParts.Add(dictPart); dictPart = newDictPart; //System.Console.WriteLine("LoadDict STEP 8"); } idxBuf.Close(); } if (resaveEntries) { //System.Console.WriteLine("LoadDict STEP 9"); DataOutputStream entriesOut = null, partsOut = null; //System.Console.WriteLine("LoadDict STEP 10"); entriesOut = new DataOutputStream(context.OpenFileOutput("entries.bin", FileCreationMode.Private)); int count = entries.Capacity(); for (int i = 0; i < count; i++) { entriesOut.WriteInt(entries.Get(i)); } //System.Console.WriteLine("LoadDict STEP 11"); partsOut = new DataOutputStream(context.OpenFileOutput("parts.bin", FileCreationMode.Private)); foreach (int i in dictIndexes) { partsOut.WriteInt(i); } //System.Console.WriteLine("LoadDict STEP 12"); if (entriesOut != null) { entriesOut.Flush(); entriesOut.Close(); } if (partsOut != null) { partsOut.Flush(); partsOut.Close(); } } else { //System.Console.WriteLine("LoadDict NOW RESAVING ENTRIES"); string documentpath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); Java.IO.File sdpath = global::Android.OS.Environment.GetExternalStoragePublicDirectory(global::Android.OS.Environment.DirectoryDownloads); File entriesDB = new File(documentpath, "entries.bin"); File partsDB = new File(documentpath, "parts.bin"); FileInputStream entriesIn = null, partsIn = null; //entriesIn = context.OpenFileInput("entries.bin"); entriesIn = new FileInputStream(entriesDB); //entriesIn = new FileInputStream(new File("entries.bin")); FileChannel file = entriesIn.Channel; ByteBuffer bb = ByteBuffer.Allocate(4 * 1649830); file.Read(bb); bb.Rewind(); entries = bb.AsIntBuffer(); file.Close(); partsIn = new FileInputStream(partsDB); //partsIn = new FileInputStream(new File("parts.bin")); //partsIn = (context.OpenFileInput("parts.bin"); file = partsIn.Channel; bb = ByteBuffer.Allocate((int)file.Size()); file.Read(bb); bb.Rewind(); IntBuffer ib = bb.AsIntBuffer(); int count = ib.Capacity(); //System.Console.WriteLine("LoadDict STEP 99 " + count); for (int i = 0; i < count; i++) { dictIndexes.Add(ib.Get(i)); } file.Close(); if (entriesIn != null) { entriesIn.Close(); } if (partsIn != null) { partsIn.Close(); } } } catch (Exception e) { System.Console.WriteLine("Dict LoadDict ERROR => " + e.Message); Log.Equals("chinesreader", e.Message); } byteBuffer = new byte[1090]; sharedPrefs = PreferenceManager.GetDefaultSharedPreferences(context); }