Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:49:25+00:00 2026-06-05T12:49:25+00:00

I am using the asynchronous I/O library of the playframework which uses Iteratees and

  • 0

I am using the asynchronous I/O library of the playframework which uses Iteratees and Enumerators. I now have an Iterator[T] as data sink (for simplification say it’s an Iterator[Byte] which stores its content into a file). This Iterator[Byte] is passed to the function which handles the writing.

But before writing I want to add some statistical information at the file begin (for simplification say it’s one Byte), so I transfer the iterator the following way before passing it to the write function:

def write(value: Byte, output: Iteratee[Byte]): Iteratee[Byte] =
    Iteratee.flatten(output.feed(Input.El(value)))

When I now read the stored file from the disk, I get an Enumerator[Byte] for it.
At first I want to read and remove the additional data and then I want to pass the rest of the Enumerator[Byte] to a function which handles the reading.
So I also need to transform the enumerator:

def read(input: Enumerator[Byte]): (Byte, Enumerator[Byte]) = {
   val firstEnumeratorEntry = ...
   val remainingEnumerator = ...
   (firstEnumeratorEntry, remainingEnumerator)
}

But I have no idea, how to do this. How can I read some bytes from an Enumerator and get the remaining Enumerator?

Replacing Iteratee[Byte] with OutputStream and Enumerator[Byte] with InputStream, this would be very easy:

def write(value: Byte, output: OutputStream) = {
    output.write(value)
    output
}
def read(input: InputStream) = (input.read,input)

But I need the asynchronous I/O of the play framework.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:49:27+00:00Added an answer on June 5, 2026 at 12:49 pm

    Here is one way to achieve this by folding within the Iteratee and an appropriate (kind-of) State accumulator (a tuple here)

    I go read the routes file, the first byte will be read as a Char and the other will be appended to a String as UTF-8 bytestrings.

      def index = Action {
        /*let's do everything asyncly*/
        Async {
          /*for comprehension for read-friendly*/
          for (
            i <- read; /*read the file */
            (r:(Option[Char], String)) <- i.run /*"create" the related Promise and run it*/
          ) yield Ok("first : " + r._1.get + "\n" + "rest" + r._2) /* map the Promised result in a correct Request's Result*/
        }
      }
    
    
      def read = {
        //get the routes file in an Enumerator
        val file: Enumerator[Array[Byte]] = Enumerator.fromFile(Play.getFile("/conf/routes"))
    
        //apply the enumerator with an Iteratee that folds the data as wished
        file(Iteratee.fold((None, ""):(Option[Char], String)) { (acc, b) =>
           acc._1 match {
             /*on the first chunk*/ case None => (Some(b(0).toChar), acc._2 + new String(b.tail, Charset.forName("utf-8")))
             /*on other chunks*/ case x => (x, acc._2 + new String(b, Charset.forName("utf-8")))
           }
        })
    
      }
    

    EDIT

    I found yet another way using Enumeratee but it needs to create 2 Enumerator s (one short lived). However is it a bit more elegant. We use a “kind-of” Enumeratee but the Traversal one which works at a finer level than Enumeratee (chunck level).
    We use take 1 that will take only 1 byte and then close the stream. On the other one, we use drop that simply drops the first byte (because we’re using a Enumerator[Array[Byte]])

    Furthermore, now read2 has a signature much more closer than what you wished, because it returns 2 enumerators (not so far from Promise, Enumerator)

    def index = Action {
      Async {
        val (first, rest) = read2
        val enee = Enumeratee.map[Array[Byte]] {bs => new String(bs, Charset.forName("utf-8"))}
    
        def useEnee(enumor:Enumerator[Array[Byte]]) = Iteratee.flatten(enumor &> enee |>> Iteratee.consume[String]()).run.asInstanceOf[Promise[String]]
    
        for {
          f <- useEnee(first);
          r <- useEnee(rest)
        } yield Ok("first : " + f + "\n" + "rest" + r)
      }
    }
    
    def read2 = {
      def create = Enumerator.fromFile(Play.getFile("/conf/routes"))
    
      val file: Enumerator[Array[Byte]] = create
      val file2: Enumerator[Array[Byte]] = create
    
      (file &> Traversable.take[Array[Byte]](1), file2 &> Traversable.drop[Array[Byte]](1))
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now, my application is using the c-ares asynchronous DNS resolver library on Windows
say I have an asynchronous library, written in native C++, with an interface similar
I have created a library which can download JSON data which is then placed
I have a library that uses the BeginXxx EndXxx asynchronous pattern (obviously, the following
I'm using this fork of the Twitter Bootstrap typeahead library , which allows asynchronous
What's the best way/library for handling multiple asynchronous callbacks? Right now, I have something
I am using nested Asynchronous callbacks to save my front-end data to the back-end
I've written a function which makes an asynchronous request using jQuery. var Site =
I'm writing an event driven application using the libevent library for asynchronous I/O. Essentially,
I have a problem with a socket library that uses WSAASyncSelect to put the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.