写入输出流使用ParcelFileDescriptor管道时发生Java.IO.IOException

时间:2019-02-13 09:37:36

标签: android io xamarin.android google-nearby

尝试写入输出流> 128kb时总是出现错误

我每8192个字节的字符串写入输出流,并且每次字节超过128kb时,代码始终停留在os.write()上,然后大约5分钟后我得到IOException。

            ParcelFileDescriptor[] payloadPipe = ParcelFileDescriptor.CreatePipe();

            Payload payload = Payload.FromStream(payloadPipe[0]);

            String deviceId = EndpointId;

            var result = ConnectionsClient.SendPayload(deviceId, payload);

                os = new ParcelFileDescriptor.AutoCloseOutputStream(payloadPipe[1]);
                int textlength = Text.Length;
                int cutLength = 8192;
                try
                {
                    for (int i = 0; i < textlength; i += cutLength)
                    {
                        string newtext = "";

                        if (i + cutLength < textlength)
                        {
                            try
                            {

                                newtext = Text.Substring(i, cutLength);
                            }
                            catch (System.Exception)
                            {

                                throw;
                            }
                        }
                        else
                        {
                            newtext = Text.Substring(i, textlength % cutLength);
                        }

                        try
                        {
                            System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                            os.Write(encoding.GetBytes(newtext));
                            os.Flush();
                        }
                        catch (Java.IO.IOException ex)
                        {
                            string asd = ex.ToString();
                            sendQueue2.Add(PayloadsQueue);
                        }
                    }
                }
                catch
                {
                    INearbyListener.OnPayloadSendFailure();
                }
                finally
                {
                    try
                    {
                        os.Close();
                    }
                    catch
                    {
                    }
                }

在android studio中,它的工作正常,但是当我在xamarin中尝试时,总是会出现错误

0 个答案:

没有答案