# Is it possible to read a file saved in sandbox with InputStream?

**URL:** <https://forums.swift.org/t/is-it-possible-to-read-a-file-saved-in-sandbox-with-inputstream/37587>\
**Category:** Using Swift\
**Tags:** foundation, io\
**Created:** [June 15, 2020, 8:04am UTC](https://forums.swift.org/t/is-it-possible-to-read-a-file-saved-in-sandbox-with-inputstream/37587 "2020-06-15T08:04:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PatrickA](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@PatrickA](https://forums.swift.org/u/PatrickA)\
**Post date:** [June 15, 2020, 8:04am UTC](https://forums.swift.org/t/is-it-possible-to-read-a-file-saved-in-sandbox-with-inputstream/37587/1 "2020-06-15T08:04:47Z")

</div>

I write a file in this way:

```
let text = " Fußgänger"

if let dir = FileManager.default.urls(for: .documentDirectory,
                                      in: .userDomainMask).first {

let fileUrl = dir.appendingPathComponent("pietons.txt")

do {
       try text.write(to: fileUrl, atomically: false , encoding: .utf8)
  }
catch {
      print ("error: \(error).")
  }

Then, just after, I'm trying to read it using InputStream 

guard let iStr = InputStream(url: fileUrl) else {

throw MyError.runTimeError("Could not open \(fileUrl)")
 
     }
 
 guard iStr.hasBytesAvailable else {
 
 throw MyError.runTimeError("No data available for \(fileUrl)")
 
     }

```

I am able to open the InputStream `(InputStream(url: fileUrl)` does not error) But there seems to be no bytes available... while I do see these bytes if I use

> String(contentsOf: fileurl, encoding: .utf8)

Did I make a mistake? How could `inputStream` read the file I created with the `write()` above?

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [June 15, 2020, 9:10pm UTC](https://forums.swift.org/t/is-it-possible-to-read-a-file-saved-in-sandbox-with-inputstream/37587/2 "2020-06-15T21:10:59Z")

</div>

`InputStream` can be used synchronously or asynchronously. You generally:

- Work with file streams synchronously

- Don’t use `hasBytesAvailable` when using it synchronously

Try calling `read(_:maxLength:)` and see what you get back.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

---

<div class="post-metadata">

**Author:** ![PatrickA](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@PatrickA](https://forums.swift.org/u/PatrickA)\
**Post date:** [June 16, 2020, 12:11am UTC](https://forums.swift.org/t/is-it-possible-to-read-a-file-saved-in-sandbox-with-inputstream/37587/3 "2020-06-16T00:11:57Z")

</div>

I had tried that and got -1.

```
let buff = UnsafeMutablePointer<UInt8>.allocate(capacity: 8)
let readLen = iStr.read(buff, maxLength: 5 )
print("read \(readLen) bytes ")
guard iStr.hasBytesAvailable else {
   throw MyError.runTimeError("No data available for \(fileUrl)")
}

```

---

<div class="post-metadata">

**Author:** ![eskimo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/eskimo/32/13558_2.png) [@eskimo](https://forums.swift.org/u/eskimo)\
**Post date:** [June 16, 2020, 9:02am UTC](https://forums.swift.org/t/is-it-possible-to-read-a-file-saved-in-sandbox-with-inputstream/37587/4 "2020-06-16T09:02:47Z")

</div>

Three things:

- The result of -1 indicates an error. You can get details on the error via the `streamError` property. What does it return?

- You don’t need to manually allocate memory for the read. Rather, try this:

- You don’t seem to be calling `open` on the stream. Did you just elide that for the sake of this post? If not, that’s probably the immediate cause of your problem.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

---

<div class="post-metadata">

**Author:** ![PatrickA](https://avatars.discourse-cdn.com/v4/letter/p/ecd19e/32.png) [@PatrickA](https://forums.swift.org/u/PatrickA)\
**Post date:** [June 16, 2020, 9:23am UTC](https://forums.swift.org/t/is-it-possible-to-read-a-file-saved-in-sandbox-with-inputstream/37587/5 "2020-06-16T09:23:14Z")

</div>

Thank you, Eskimo, the missing open was indeed the problem. Yes, it's dumb of me.
