PositionableStream.st
changeset 23895 d49a46365ed5
parent 22514 e7e21b6d5ea6
child 24744 2d27b0eb2d4a
equal deleted inserted replaced
23894:2dcb46224ee8 23895:d49a46365ed5
       
     1 "{ Encoding: utf8 }"
       
     2 
     1 "
     3 "
     2  COPYRIGHT (c) 1989 by Claus Gittinger
     4  COPYRIGHT (c) 1989 by Claus Gittinger
     3 	      All Rights Reserved
     5 	      All Rights Reserved
     4 
     6 
     5  This software is furnished under a license and may be used
     7  This software is furnished under a license and may be used
   126 !PositionableStream class methodsFor:'testing'!
   128 !PositionableStream class methodsFor:'testing'!
   127 
   129 
   128 isAbstract
   130 isAbstract
   129     ^ self == PositionableStream
   131     ^ self == PositionableStream
   130 ! !
   132 ! !
       
   133 
   131 
   134 
   132 !PositionableStream methodsFor:'Compatibility-Dolphin'!
   135 !PositionableStream methodsFor:'Compatibility-Dolphin'!
   133 
   136 
   134 endChunk
   137 endChunk
   135     self nextPutChunkSeparator
   138     self nextPutChunkSeparator
   251      s contents.
   254      s contents.
   252 
   255 
   253     "
   256     "
   254 
   257 
   255     "Modified: / 04-06-2007 / 17:21:55 / cg"
   258     "Modified: / 04-06-2007 / 17:21:55 / cg"
   256 ! !
       
   257 
       
   258 !PositionableStream methodsFor:'misc functions'!
       
   259 
       
   260 copy:numberOfBytes into:aWriteStream
       
   261     "read from the receiver, and write numberOfBytes data to another aWriteStream.
       
   262      Return the number of bytes which have been transferred.
       
   263      Redefined here to avoid intermediate buffers/garbage."
       
   264 
       
   265     |endPosition cnt|
       
   266 
       
   267     collection notNil ifTrue:[
       
   268         endPosition := (position+numberOfBytes) min:readLimit.
       
   269         aWriteStream nextPutAll:collection startingAt:position+1 to:endPosition.
       
   270         cnt := endPosition - position.
       
   271         position := endPosition.
       
   272         ^ cnt.
       
   273     ].
       
   274 
       
   275     ^ super copy:numberOfBytes into:aWriteStream.
       
   276 
       
   277 
       
   278     "
       
   279       'hello world' readStream copy:5 into:'/tmp/mist' asFilename writeStream.
       
   280       'hello world' readStream 
       
   281                         copy:5 into:Transcript;
       
   282                         copy:20 into:Transcript.
       
   283       'hello world' readStream copy:5 into:'' writeStream inspect.
       
   284       #[1 2 3 4 5 6 7] readStream copy:2 into:'/tmp/mist' asFilename writeStream binary.
       
   285       #[1 2 3 4 5 6 7] readStream copy:3 into:#[] writeStream.
       
   286     "
       
   287 
       
   288     "
       
   289      |rs ws cnt|
       
   290 
       
   291      ws := #() writeStream.
       
   292      rs := #( 1 2 3 4 a nil true) readWriteStream.
       
   293      rs next.
       
   294      cnt := rs copyToEndInto:ws bufferSize:0.
       
   295      Transcript show:cnt; show:' '; showCR:ws contents.
       
   296     "
       
   297 !
       
   298 
       
   299 copy:numberOfBytes into:aWriteStream bufferSize:bufferSize
       
   300     "read from the receiver, and write numberOfBytes data to another aWriteStream.
       
   301      Return the number of bytes which have been transferred.
       
   302      Redefined here to avoid intermediate buffers/garbage.
       
   303      bufferSize does not matter here."
       
   304 
       
   305     collection notNil ifTrue:[
       
   306         ^ self copy:numberOfBytes into:aWriteStream.
       
   307     ].
       
   308     ^ super copy:numberOfBytes into:aWriteStream bufferSize:bufferSize.
       
   309 !
       
   310 
       
   311 copyToEndInto:aWriteStream
       
   312     "read from the receiver, and write all data up to the end to another stream.
       
   313      Return the number of bytes which have been transferred.
       
   314      Redefined here to avoid intermediate buffers/garbage."
       
   315 
       
   316     |cnt|
       
   317 
       
   318     collection notNil ifTrue:[
       
   319         aWriteStream nextPutAll:collection startingAt:position+1 to:readLimit.
       
   320         cnt := readLimit - position.
       
   321         position := readLimit.
       
   322         ^ cnt.
       
   323     ].
       
   324     ^ super copyToEndInto:aWriteStream.
       
   325 
       
   326     "
       
   327      |rs ws cnt|
       
   328 
       
   329      ws := #() writeStream.
       
   330      rs := #( 1 2 3 4 a nil true) readWriteStream.
       
   331      rs next.
       
   332      cnt := rs copyToEndInto:ws bufferSize:0.
       
   333      Transcript show:cnt; show:' '; showCR:ws contents.
       
   334     "
       
   335 !
       
   336 
       
   337 copyToEndInto:aWriteStream bufferSize:bufferSize
       
   338     "read from the receiver, and write all data up to the end to another stream.
       
   339      Return the number of bytes which have been transferred.
       
   340      Redefined here to avoid intermediate buffers/garbage.
       
   341      bufferSize does not matter here."
       
   342 
       
   343     collection notNil ifTrue:[
       
   344         ^ self copyToEndInto:aWriteStream.
       
   345     ].
       
   346     ^ super copyToEndInto:aWriteStream bufferSize:bufferSize.
       
   347 
       
   348     "
       
   349      |rs ws cnt|
       
   350 
       
   351      ws := #() writeStream.
       
   352      rs := #( 1 2 3 4 a nil true) readWriteStream.
       
   353      rs next.
       
   354      cnt := rs copyToEndInto:ws bufferSize:0.
       
   355      Transcript show:cnt; show:' '; showCR:ws contents.
       
   356     "
       
   357 ! !
   259 ! !
   358 
   260 
   359 !PositionableStream methodsFor:'non homogenous reading'!
   261 !PositionableStream methodsFor:'non homogenous reading'!
   360 
   262 
   361 nextBytes:numBytes into:aCollection startingAt:initialIndex
   263 nextBytes:numBytes into:aCollection startingAt:initialIndex
  1034         (Character return, Character cr) asString readStream nextLine
   936         (Character return, Character cr) asString readStream nextLine
  1035         Character return asString readStream nextLine; nextLine
   937         Character return asString readStream nextLine; nextLine
  1036     "
   938     "
  1037 ! !
   939 ! !
  1038 
   940 
       
   941 !PositionableStream methodsFor:'stream-to-stream copy'!
       
   942 
       
   943 copy:numberOfElementsOrNil into:aWriteStream
       
   944     "read from the receiver, and write numberOfElementsOrNil data to another aWriteStream.
       
   945      If numberOfElementsOrNil is nil, copy until the end of myself.
       
   946      Return the number of elements which have been transferred.
       
   947      Redefined here to avoid intermediate buffers/garbage."
       
   948 
       
   949     |endPosition cnt|
       
   950 
       
   951     collection notNil ifTrue:[
       
   952         numberOfElementsOrNil isNil ifTrue:[
       
   953             endPosition := readLimit.   "/ read until end
       
   954         ] ifFalse:[
       
   955             endPosition := (position+numberOfElementsOrNil) min:readLimit.
       
   956         ].
       
   957         aWriteStream nextPutAll:collection startingAt:position+1 to:endPosition.
       
   958         cnt := endPosition - position.
       
   959         position := endPosition.
       
   960         ^ cnt.
       
   961     ].
       
   962 
       
   963     ^ super copy:numberOfElementsOrNil into:aWriteStream.
       
   964 
       
   965 
       
   966     "
       
   967       'hello world' readStream copy:5 into:'/tmp/mist' asFilename writeStream.
       
   968       'hello world' readStream 
       
   969                         copy:5 into:Transcript;
       
   970                         copy:20 into:Transcript.
       
   971       'hello world' readStream copy:5 into:'' writeStream inspect.
       
   972       #[1 2 3 4 5 6 7] readStream copy:2 into:'/tmp/mist' asFilename writeStream binary.
       
   973       #[1 2 3 4 5 6 7] readStream copy:3 into:#[] writeStream.
       
   974     "
       
   975 
       
   976     "
       
   977      |rs ws cnt|
       
   978 
       
   979      ws := #() writeStream.
       
   980      rs := #( 1 2 3 4 a nil true) readWriteStream.
       
   981      rs next.
       
   982      cnt := rs copyToEndInto:ws bufferSize:0.
       
   983      Transcript show:cnt; show:' '; showCR:ws contents.
       
   984     "
       
   985 
       
   986     "Modified (comment): / 13-03-2019 / 16:59:18 / Stefan Vogel"
       
   987 !
       
   988 
       
   989 copy:numberOfElementsOrNil into:aWriteStream bufferSize:bufferSize
       
   990     "read from the receiver, and write numberOfElementsOrNil to another aWriteStream.
       
   991      If numberOfElementsOrNil is nil, copy until the end of myself.
       
   992      Return the number of elements which have been transferred.
       
   993      Redefined here to avoid intermediate buffers/garbage
       
   994      - bufferSize does not matter here."
       
   995 
       
   996     collection notNil ifTrue:[
       
   997         ^ self copy:numberOfElementsOrNil into:aWriteStream.
       
   998     ].
       
   999     ^ super copy:numberOfElementsOrNil into:aWriteStream bufferSize:bufferSize.
       
  1000 
       
  1001     "Modified (comment): / 13-03-2019 / 17:00:11 / Stefan Vogel"
       
  1002 ! !
       
  1003 
  1039 !PositionableStream methodsFor:'testing'!
  1004 !PositionableStream methodsFor:'testing'!
  1040 
  1005 
  1041 atEnd
  1006 atEnd
  1042     "return true, if the read-position is at the end"
  1007     "return true, if the read-position is at the end"
  1043 
  1008