Beispiel #1
0
 public static void saveDecFile(byte[] encodedBytes, String path, string fileName)
 {
     try
     {
         System.IO.File.Copy(path, "/storage/emulated/0/movies/" + fileName);
         Java.IO.File         file = new Java.IO.File(path);
         System.IO.FileStream fs   = new System.IO.FileStream("/storage/emulated/0/movies/" + fileName, FileMode.Open);
         BufferedOutputStream bos  = new BufferedOutputStream(fs);
         bos.Write(encodedBytes);
         bos.Flush();
         bos.Close();
     }
     catch (Java.IO.FileNotFoundException e)
     {
         e.PrintStackTrace();
     }
     catch (Java.IO.IOException e)
     {
         e.PrintStackTrace();
     }
     catch (Exception e)
     {
         //  e.PrintStackTrace();
     }
 }
Beispiel #2
0
        /// <exception cref="System.IO.IOException"></exception>
        protected virtual void Copy(Socket4Adapter sock, IOutputStream rawout, int length
                                    , bool update)
        {
            var @out      = new BufferedOutputStream(rawout);
            var buffer    = new byte[BlobImpl.CopybufferLength];
            var totalread = 0;

            while (totalread < length)
            {
                var stilltoread = length - totalread;
                var readsize    = (stilltoread < buffer.Length ? stilltoread : buffer.Length);
                var curread     = sock.Read(buffer, 0, readsize);
                if (curread < 0)
                {
                    throw new IOException();
                }
                @out.Write(buffer, 0, curread);
                totalread += curread;
                if (update)
                {
                    _currentByte += curread;
                }
            }
            @out.Flush();
            @out.Close();
        }
Beispiel #3
0
        private Object Post(string parameters)
        {
            try
            {
                conn = (HttpURLConnection)url.OpenConnection();
                conn.RequestMethod = "POST";
                conn.DoOutput      = true;
                conn.DoInput       = true;
                conn.Connect();

                Android.Util.Log.Debug("SendToServer", "parametros " + parameters);

                byte[] bytes = Encoding.ASCII.GetBytes(parameters);

                OutputStream outputStream = new BufferedOutputStream(conn.OutputStream);
                outputStream.Write(bytes);
                outputStream.Flush();
                outputStream.Close();

                InputStream inputStream = new BufferedInputStream(conn.InputStream);

                return(ReadString(inputStream));
            }
            catch (IOException e)
            {
                Android.Util.Log.Debug("SendToServer", "Problems to send data to server!! " + e.Message);
                return(false);
            }
            finally
            {
                conn.Disconnect();
            }
        }
Beispiel #4
0
        private static string getData()
        {
            StringBuffer response = null;
            URL          obj      = new URL("http://192.168.1.17/easyserver/WebService.php");

            switch (mFunction)
            {
            case "setUser":
                data = "function=" + URLEncoder.Encode(mFunction, "UTF-8")
                       + "&name=" + URLEncoder.Encode(mName, "UTF-8")
                       + "&age=" + URLEncoder.Encode(mAge, "UTF-8");
                break;

            case "getUser":
                data = "function=" + URLEncoder.Encode(mFunction, "UTF-8");
                break;
            }
            //creación del objeto de conexión
            HttpURLConnection con = (HttpURLConnection)obj.OpenConnection();

            //Agregando la cabecera
            //Enviamos la petición por post
            con.RequestMethod = "POST";
            con.DoOutput      = true;
            con.DoInput       = true;
            con.SetRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            //Envio de datos
            //obtener el tamaño de los datos
            con.SetFixedLengthStreamingMode(data.Length);

            /*
             * OutputStream clase abstracta que es la superclase de todas las clases que
             * representan la salida de un flujo de bytes. Un flujo de salida acepta bytes de salida.
             * BufferOutputStream es la clase que implementa un flujo de salida con buffer
             */

            OutputStream ouT = new BufferedOutputStream(con.OutputStream);

            byte[] array = Encoding.ASCII.GetBytes(data);
            ouT.Write(array);
            ouT.Flush();
            ouT.Close();

            /*
             * Obteniendo datos
             * BufferedRead Lee texto de una corriente de caracteres de entrada,
             * Un InputStreamReader es un puente de flujos de streams de caracteres: se lee los
             * bytes y los decodifica en caracteres
             */
            BufferedReader iN = new BufferedReader(new InputStreamReader(con.InputStream));
            string         inputLine;

            response = new StringBuffer();
            while ((inputLine = iN.ReadLine()) != null)
            {
                response.Append(inputLine);
            }
            iN.Close();
            return(response.ToString());
        }
Beispiel #5
0
        /// <summary>
        /// 解压文件
        /// </summary>
        /// <param name="inputZip"></param>
        /// <param name="destinationDirectory"></param>
        public static void UnzipFile(string inputZip, string destinationDirectory)
        {
            int           buffer         = 2048;
            List <string> zipFiles       = new List <string>();
            File          sourceZipFile  = new File(inputZip);
            File          unzipDirectory = new File(destinationDirectory);

            CreateDir(unzipDirectory.AbsolutePath);

            ZipFile zipFile;

            zipFile = new ZipFile(sourceZipFile, ZipFile.OpenRead);
            IEnumeration zipFileEntries = zipFile.Entries();

            while (zipFileEntries.HasMoreElements)
            {
                ZipEntry entry        = (ZipEntry)zipFileEntries.NextElement();
                string   currentEntry = entry.Name;
                File     destFile     = new File(unzipDirectory, currentEntry);

                if (currentEntry.EndsWith(Constant.SUFFIX_ZIP))
                {
                    zipFiles.Add(destFile.AbsolutePath);
                }

                File destinationParent = destFile.ParentFile;
                CreateDir(destinationParent.AbsolutePath);

                if (!entry.IsDirectory)
                {
                    if (destFile != null && destFile.Exists())
                    {
                        continue;
                    }

                    BufferedInputStream inputStream = new BufferedInputStream(zipFile.GetInputStream(entry));
                    int currentByte;
                    // buffer for writing file
                    byte[] data = new byte[buffer];

                    var fos = new System.IO.FileStream(destFile.AbsolutePath, System.IO.FileMode.OpenOrCreate);
                    BufferedOutputStream dest = new BufferedOutputStream(fos, buffer);

                    while ((currentByte = inputStream.Read(data, 0, buffer)) != -1)
                    {
                        dest.Write(data, 0, currentByte);
                    }
                    dest.Flush();
                    dest.Close();
                    inputStream.Close();
                }
            }
            zipFile.Close();

            foreach (var zipName in zipFiles)
            {
                UnzipFile(zipName, destinationDirectory + File.SeparatorChar
                          + zipName.Substring(0, zipName.LastIndexOf(Constant.SUFFIX_ZIP)));
            }
        }
Beispiel #6
0
        /**
         * 解压文件
         *
         * @param filePath 压缩文件路径
         */
        public static void unzip(string filePath)
        {
            //isSuccess = false;
            File source = new File(filePath);

            if (source.Exists())
            {
                ZipInputStream       zis = null;
                BufferedOutputStream bos = null;
                try
                {
                    zis = new ZipInputStream(new System.IO.FileStream(source.Path, System.IO.FileMode.Append));
                    ZipEntry entry = null;
                    while ((entry = zis.NextEntry) != null &&
                           !entry.IsDirectory)
                    {
                        File target = new File(source.Parent, entry.Name);
                        if (!target.ParentFile.Exists())
                        {
                            // 创建文件父目录
                            target.ParentFile.Mkdirs();
                        }
                        // 写入文件
                        bos = new BufferedOutputStream(new System.IO.FileStream(target.Path, System.IO.FileMode.Append));
                        int    read   = 0;
                        byte[] buffer = new byte[1024 * 10];
                        while ((read = zis.Read(buffer, 0, buffer.Length)) != -1)
                        {
                            bos.Write(buffer, 0, read);
                        }
                        bos.Flush();
                    }
                    zis.CloseEntry();
                }
                catch (IOException e)
                {
                    throw new RuntimeException(e);
                }
                finally
                {
                    zis.Close();
                    bos.Close();
                    // isSuccess = true;
                }
            }
        }
Beispiel #7
0
 public static void saveFile(byte[] encodedBytes, String path)
 {
     try
     {
         Java.IO.File         file = new Java.IO.File(path);
         System.IO.FileStream fs   = new System.IO.FileStream(path, FileMode.Open);
         BufferedOutputStream bos  = new BufferedOutputStream(fs);
         bos.Write(encodedBytes);
         bos.Flush();
         bos.Close();
     }
     catch (Java.IO.FileNotFoundException e)
     {
         e.PrintStackTrace();
     }
     catch (Java.IO.IOException e)
     {
         e.PrintStackTrace();
     }
     catch (Exception e)
     {
         //  e.PrintStackTrace();
     }
 }
Beispiel #8
0
 /// <exception cref="System.IO.IOException"/>
 public sealed override void writeBuffered(BufferedOutputStream writer, MessageBuilder message)
 {
     Serialize.Write(writer, message);
     writer.Flush();
 }
Beispiel #9
0
        public virtual void StarredExportPleco()
        {
            string stars = sharedPrefs.GetString("stars", "");

            if (stars.Length < 2)
            {
                Toast.MakeText(this, "Starred list is empty. Nothing to export.", ToastLength.Long).Show();
                return;
            }

            try
            {
                File dir = new File(global::Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, "/ChineseReader");

                dir.Mkdirs();
                System.IO.FileStream os  = new System.IO.FileStream(dir.AbsolutePath + "/chinesereader_starred.txt", System.IO.FileMode.Create, System.IO.FileAccess.Write);
                BufferedOutputStream bos = new BufferedOutputStream(os);

                StringBuilder english = new StringBuilder();

                int oldIndex = 0, nIndex;
                while ((nIndex = stars.IndexOf("\n", oldIndex)) > -1)
                {
                    english.Length = 0;

                    int entry = Dict.BinarySearch(stars.SubstringSpecial(oldIndex, nIndex), false);
                    oldIndex = nIndex + 1;

                    if (entry == -1)
                    {
                        continue;
                    }

                    english.Append(Dict.GetCh(entry)).Append("\t").Append(Regex.Replace(Dict.GetPinyin(entry), @"(\\d)", "$1 ")).Append("\t");

                    string[] parts = Regex.Split(Regex.Replace(Dict.GetEnglish(entry), @"/", "; "), @"\\$");
                    int      j     = 0;
                    foreach (string str in parts)
                    {
                        if (j++ % 2 == 1)
                        {
                            english.Append(" [ ").Append(Regex.Replace(str, @"(\\d)", "$1 ")).Append("] ");
                        }
                        else
                        {
                            english.Append(str);
                        }
                    }
                    english.Append("\n");

                    bos.Write(Encoding.UTF8.GetBytes(english.ToString()));
                }
                os.Flush();
                bos.Flush();
                bos.Close();
                os.Close();

                Toast.MakeText(this, "Successfully exported to " + dir.AbsolutePath + "/chinesereader_starred.txt", ToastLength.Long).Show();
            }
            catch (Exception e)
            {
                Toast.MakeText(this, "Could not export: " + e.Message, ToastLength.Long).Show();
            }
        }