1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
|
"======================================================================
|
| Abstract NetClient framework
|
|
======================================================================"
"======================================================================
|
| NetUser and NetEnvironment are Copyright 2000 Cincom, Inc.
| NetResponse, PluggableReporter and *Error are (c) 1995 Kazuki Yasumatsu
| and in the public domain.
|
| The rest is copyright 2002 Free Software Foundation, Inc.
| and written by Paolo Bonzini.
|
| This file is part of the GNU Smalltalk class library.
|
| The GNU Smalltalk class library is free software; you can redistribute it
| and/or modify it under the terms of the GNU Lesser General Public License
| as published by the Free Software Foundation; either version 2.1, or (at
| your option) any later version.
|
| The GNU Smalltalk class library is distributed in the hope that it will be
| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
| General Public License for more details.
|
| You should have received a copy of the GNU Lesser General Public License
| along with the GNU Smalltalk class library; see the file COPYING.LIB.
| If not, write to the Free Software Foundation, 59 Temple Place - Suite
| 330, Boston, MA 02111-1307, USA.
|
======================================================================"
Namespace current: NetClients!
NetClients at: #LineEndCR put: #cr!
NetClients at: #LineEndLF put: #nl!
NetClients at: #LineEndCRLF put: #crnl!
NetClients at: #LineEndTransparent put: #yourself!
Object subclass: #NetUser
instanceVariableNames: 'username password '
classVariableNames: ''
poolDictionaries: ''
category: 'NetClients-Framework'!
NetUser comment:
'Instances of this class hold the username and password used to login to a mail server.
Instance Variables:
username <String> username string
password <String> password string'!
Object subclass: #NetEnvironment
instanceVariableNames: 'debugStream debugCategories debugClasses trace logFileName '
classVariableNames: 'Environment '
poolDictionaries: ''
category: 'NetClients-Framework'!
NetEnvironment comment:
nil!
Object subclass: #NetClient
instanceVariableNames: 'hostName portNumber user reporter clientPI '
classVariableNames: ''
poolDictionaries: 'TCP '
category: 'NetClients-Framework'!
NetClient comment:
nil!
Object subclass: #NetProtocolInterpreter
instanceVariableNames: 'client connectionStream '
classVariableNames: ''
poolDictionaries: 'TCP '
category: 'NetClients-Framework'!
NetClient comment:
'
Copyright (c) Kazuki Yasumatsu, 1995. All rights reserved.
'!
Object subclass: #NetResponse
instanceVariableNames: 'status statusMessage '
classVariableNames: ''
poolDictionaries: ''
category: 'NetClients-Framework'!
NetResponse comment:
'
Copyright (c) Kazuki Yasumatsu, 1995. All rights reserved.
'!
Object subclass: #Reporter
instanceVariableNames: 'totalByte readByte '
classVariableNames: ''
poolDictionaries: ''
category: 'NetClients-URIResolver'!
Reporter comment:
nil!
Reporter subclass: #PluggableReporter
instanceVariableNames: 'startTime currentTime totalByte readByte statusBlock '
classVariableNames: ''
poolDictionaries: ''
category: 'NetClients-Framework'!
PluggableReporter comment:
'
Copyright (c) Kazuki Yasumatsu, 1995. All rights reserved.
'!
Stream subclass: #RemoveDotStream
instanceVariableNames: 'stream ch atStart '
classVariableNames: ''
poolDictionaries: ''
category: 'NetClients-Framework'!
RemoveDotStream comment:
nil!
Stream subclass: #PrependDotStream
instanceVariableNames: 'stream atStart '
classVariableNames: ''
poolDictionaries: ''
category: 'NetClients-Framework'!
PrependDotStream comment:
'A PrependDotStream removes a dot to each line starting with a dot, and
ends when its input has a lonely dot.'!
Stream subclass: #CrLfStream
instanceVariableNames: 'stream readStatus eatLf '
classVariableNames: 'Lf Cr '
poolDictionaries: ''
category: 'NetClients-Framework'!
CrLfStream comment:
'A CrLfStream acts as a pipe which transforms incoming data into LF-separated
lines, and outgoing data into CRLF-separated lines.'!
Error subclass: #NetClientError
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'NetClients-Framework'!
NetClientError comment:
nil!
NetClientError subclass: #ConnectionFailedError
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'NetClients-Framework'!
ConnectionFailedError comment:
nil!
NetClientError subclass: #ConnectionClosedError
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'NetClients-Framework'!
ConnectionClosedError comment:
nil!
NetClientError subclass: #ProtocolError
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'NetClients-Framework'!
ProtocolError comment:
nil!
NetClientError subclass: #LoginIncorrectError
instanceVariableNames: ''
classVariableNames: ''
poolDictionaries: ''
category: 'NetClients-Framework'!
LoginIncorrectError comment:
nil!
!NetUser class methodsFor: 'instance creation'!
username: aUsername password: aPassword
"NetUser username: 'foo' password: 'foo'"
|user|
user := self new.
^user
username: aUsername;
password: aPassword
yourself! !
!NetUser methodsFor: 'accessing'!
password
^password!
password: aString
password := aString!
username
^username!
username: aString
username := aString! !
!NetEnvironment class methodsFor: 'accessing'!
default
^ Environment isNil
ifTrue: [ Environment := self new ]
ifFalse: [ Environment ].! !
!NetEnvironment methodsFor: 'accessing'!
debugCategories
debugCategories isNil
ifTrue: [ debugCategories := Set new.
debugCategories add: #general ].
^debugCategories!
debugClasses
^debugClasses isNil
ifTrue: [ debugClasses := Set new.]
ifFalse: [ debugClasses ].!
debugStream
^debugStream!
debugStream: aStream
debugStream := aStream!
logFileName
logFileName isNil
ifTrue: [ logFileName := 'NetClientLog.txt'].
^ logFileName!
logFileName: aString
logFileName := aString!
trace
trace isNil
ifTrue: [ trace := false].
^ trace!
trace: aBoolean
trace := aBoolean! !
!NetEnvironment methodsFor: 'debugging'!
debug: aBlock level: aLevel
( self trace and: [ self debugCategories includes: aLevel ] ) ifTrue: [ aBlock value]!
log: aStringOrBlock
self log: aStringOrBlock level: #general!
log: aStringOrBlock level: aLevel
| stream i briefMsg aMsg |
self
debug:
[(stream := self debugStream) == nil ifTrue: [^self].
(aStringOrBlock isKindOf: BlockClosure)
ifTrue: [ aMsg := aStringOrBlock value]
ifFalse: [ aMsg := aStringOrBlock ].
i := aMsg size.
[i > 0 and: [(aMsg at: i) isSeparator]] whileTrue: [i := i - 1].
briefMsg := aMsg copyFrom: 1 to: i.
stream cr; nextPutAll: briefMsg; flush]
level: aLevel!
printTrace: aString
| stream |
(stream := self debugStream) == nil ifTrue: [^self].
stream cr; cr; nextPutAll: ' **** ' asString.
Date today printOn: stream.
stream nextPutAll: ' '.
Time now printOn: stream.
stream nextPutAll: ' ', aString, ' ****'; flush.!
traceOff
self printTrace: 'Stop Trace'.
self trace: false.!
traceOn
self trace: true.
self printTrace: 'Start Trace'.! !
!NetEnvironment methodsFor: 'private'!
addDebugCategory: symbol
self debugCategories add: symbol!
removeDebugCategory: symbol
self debugCategories remove: symbol!
reset
self resetDebugClasses.
self resetDebugCategories.!
resetDebugCategories
debugCategories := nil!
resetDebugClasses
debugClasses := nil! !
!NetEnvironment methodsFor: 'registration'!
addToDebug: aClass
self debugClasses add: aClass.! !
!NetClient class methodsFor: 'instance creation'!
connectToHost: aHostname
^self new connectToHost: aHostname port: nil!
connectToHost: aHostname port: aPort
^self new connectToHost: aHostname port: aPort!
loginToHost: aHostName asUser: userString withPassword: passwdString
^self loginToHost: aHostName port: nil asUser: userString withPassword: passwdString!
loginToHost: aHostName port: aNumber asUser: userString withPassword: passwdString
^self new
loginToHost: aHostName
port: aNumber
asUser: userString
withPassword: passwdString!
loginUser: userString withPassword: passwdString
^self loginUser: userString withPassword: passwdString! !
!NetClient methodsFor: 'accessing'!
user
^user!
user: aNetUser
user := aNetUser!
username
^user username!
password
^user password!
username: usernameString password: passwdString
user := NetUser username: usernameString password: passwdString!
clientPI
^clientPI!
clientPI: aProtocolInterpreter
clientPI := aProtocolInterpreter!
hostName
^hostName!
hostName: aString
hostName := aString!
password
^self user password!
portNumber
portNumber isNil ifTrue: [ ^self protocolInterpreter defaultPortNumber ].
portNumber = 0 ifTrue: [ ^self protocolInterpreter defaultPortNumber ].
^portNumber!
portNumber: aNumber
portNumber := aNumber!
reporter
reporter isNil ifTrue: [ reporter := Reporter new ].
^reporter!
reporter: aReporter
reporter := aReporter! !
!NetClient methodsFor: 'abstract'!
protocolInterpreter
self subclassResponsibility! !
!NetClient methodsFor: 'connection'!
close
^self logout!
connect
self connectToHost: self hostName port: self portNumber!
connectIfClosed
self clientPI closed
ifTrue: [self connect ]!
connectToHost: aString
"Establish a connection to the host <aString>."
^self connectToHost: aString port: nil!
connectToHost: aString port: aNumber
"Establish a connection to the host <aString>."
self hostName: aString.
self portNumber: aNumber.
self clientPI: (self protocolInterpreter client: self).
self clientPI connect!
login
!
logout
!
loginToHost: aHostName asUser: userString withPassword: passwdString
^self
loginToHost: aHostName
port: nil
asUser: userString
withPassword: passwdString!
loginToHost: aHostName port: aNumber asUser: userString withPassword: passwdString
| resp |
hostName := aHostName.
portNumber := aNumber.
self username: userString password: passwdString.
self connect.
(resp := self login) completedSuccessfully ifFalse: [ ^nil]!
reconnect
self clientPI close.
self connect! !
!NetProtocolInterpreter class methodsFor: 'debugging'!
log: aString level: aLevel
NetEnvironment default log: aString level: aLevel!
registerToDebug
NetEnvironment default addToDebug: self! !
!NetProtocolInterpreter class methodsFor: 'instance creation'!
client: aNetClient
^self new client: aNetClient!
new
^self basicNew initialize! !
!NetProtocolInterpreter methodsFor: 'accessing'!
client
^client!
reporter
^self client reporter!
receiveMessageUntilPeriod
"Receive and answer a message until period line."
| write |
write := WriteStream on: (String new: 4 * 1024).
self receiveMessageUntilPeriodInto: write.
^write contents!
receiveMessageUntilPeriodInto: aStream
"Receive a message until period line into aStream."
self connectIfClosed.
MIME.MimeEntity new parseSimpleBodyFrom: self onto: aStream!
sendMessageWithPeriod: aStream
"Send aStream as a message with period."
self connectIfClosed.
(PrependDotStream to: self)
nextPutAll: aStream;
flush
!
skipMessageUntilPeriod
"Skip a message until period line."
self connectIfClosed.
MIME.MimeEntity new skipSimpleBodyFrom: self! !
!NetProtocolInterpreter methodsFor: 'connection'!
binary
connectionStream class == CrLfStream
ifTrue: [ connectionStream := connectionStream stream ]
!
isBinary
^connectionStream class ~~ CrLfStream
!
text
self binary.
self lineEndConvention = LineEndCRLF ifTrue: [
connectionStream := CrLfStream on: connectionStream ].
!
close
self closed ifFalse: [connectionStream close. connectionStream := nil].
self liveAcrossSnapshot
ifTrue: [ObjectMemory removeDependent: self].!
closed
^connectionStream == nil!
connectionStream
^connectionStream!
connectionStream: aSocket
connectionStream := aSocket.
self text.
self liveAcrossSnapshot
ifTrue: [ObjectMemory addDependent: self]!
connect
| connection messageText |
connection := [ Socket remote: client hostName port: client portNumber ]
on: Error
do: [ :ex | ex. messageText := ex messageText. ex return: nil ].
connection isNil ifTrue: [^self connectionFailedError: messageText].
self connectionStream: connection!
connectIfClosed
self closed ifTrue: [self connect].!
reconnect
self close; connect! !
!NetProtocolInterpreter methodsFor: 'encoding'!
decode: aString
^aString!
encode: aString
^aString! !
!NetProtocolInterpreter methodsFor: 'initialize-release'!
client: aNetClient
client := aNetClient!
initialize!
release
self close.! !
!NetProtocolInterpreter methodsFor: 'private'!
checkResponse
self checkResponse: self getResponse
!
checkResponse: response
self
checkResponse: response
ifError: [self protocolError: response statusMessage]!
checkResponse: reponse ifError: errorBlock
!
connectionClosedError: messageText
^ConnectionClosedError new
tag: messageText;
signal: 'Connection closed: ', messageText!
connectionFailedError: messageText
^ConnectionFailedError new
tag: messageText;
signal: 'Connection failed: ', messageText!
getResponse
^self class defaultResponseClass fromClient: self!
loginIncorrectError: messageText
^LoginIncorrectError new
tag: messageText;
signal: 'Login incorrect: ', messageText!
protocolError: messageText
^ProtocolError new
tag: messageText;
signal: 'Protocol error: ', messageText! !
!NetProtocolInterpreter class methodsFor: 'private-attributes'!
defaultPortNumber
^nil!
defaultResponseClass
^NetResponse!
!NetProtocolInterpreter methodsFor: 'private-attributes'!
lineEndConvention
^LineEndCRLF!
liveAcrossSnapshot
^false! !
!NetProtocolInterpreter methodsFor: 'stream accessing'!
atEnd
^connectionStream atEnd!
contents
^self decode: connectionStream contents.!
cr
| conv |
conv := self lineEndConvention.
(conv = LineEndCR or: [conv = LineEndTransparent])
ifTrue: [^connectionStream nextPut: Character cr].
conv = LineEndLF
ifTrue: [^connectionStream nextPut: Character nl].
conv = LineEndCRLF
ifTrue: [^connectionStream nextPut: Character cr; nextPut: Character nl].
self error: 'Undefined line-end convention'.!
flush
connectionStream flush!
next
^connectionStream next!
next: anInteger
^self decode: (connectionStream next: anInteger).!
nextAvailable: anInteger
^self decode: (connectionStream nextAvailable: anInteger).!
nextLine
| write byte |
write := WriteStream on: (String new: 128).
[connectionStream atEnd] whileFalse: [
byte := connectionStream next.
byte == Character cr ifTrue:
[connectionStream peekFor: Character nl.
^self decode: write contents].
byte == Character nl ifTrue:
[^self decode: write contents].
write nextPut: byte].
^self decode: write contents!
nextPut: aCharacter
connectionStream nextPutAll: (self encode: (String with: aCharacter))!
nextPutAll: aString
aString isEmpty ifTrue: [^self].
connectionStream nextPutAll: (self encode: aString)!
nl
| conv |
conv := self lineEndConvention.
conv = LineEndCR
ifTrue: [^connectionStream nextPut: Character cr].
(conv = LineEndLF or: [conv = LineEndTransparent])
ifTrue: [^connectionStream nextPut: Character nl].
conv = LineEndCRLF
ifTrue: [^connectionStream nextPut: Character cr; nextPut: Character nl].
self error: 'Undefined line-end convention'.!
species
^connectionStream species!
upTo: aCharacter
| byte |
aCharacter = Character cr ifTrue: [^self nextLine].
byte := self encode: (String with: aCharacter).
byte size = 1
ifTrue: [^self decode: (connectionStream upTo: byte)]
ifFalse: [^self decode: (connectionStream upToAll: byte)]! !
!NetProtocolInterpreter methodsFor: 'updating'!
update: aSymbol
"Dependents of ObjectMemory are sent update:
#returnFromSnapshot when a snapshot is started."
self liveAcrossSnapshot
ifTrue:
[aSymbol == #returnFromSnapshot
ifTrue: [self close].
"(aSymbol == #aboutToSnapshot or: [aSymbol == #aboutToQuit])
ifTrue: [self close]."].
super update: aSymbol! !
!NetResponse class methodsFor: 'instance creation'!
fromClient: aClient
| response |
response := self new.
response parseResponse: aClient.
^response! !
!NetResponse methodsFor: 'accessing'!
status
^status!
status: anInteger
status := anInteger!
statusArray
| n array |
status == nil ifTrue: [n := 0] ifFalse: [n := status].
array := Array new: 3.
array at: 1 put: (n // 100).
n := n - ((n // 100) * 100).
array at: 2 put: (n // 10).
n := n - ((n // 10) * 10).
array at: 3 put: n.
^array!
statusMessage
^statusMessage!
statusMessage: aString
statusMessage := aString! !
!NetResponse methodsFor: 'parsing'!
parseResponse: aClient
self parseStatusLine: aClient! !
!NetResponse methodsFor: 'printing'!
printOn: aStream
self printStatusOn: aStream!
printStatusOn: aStream
status notNil
ifTrue: [aStream print: status; space].
statusMessage notNil
ifTrue: [aStream nextPutAll: statusMessage].! !
!NetResponse methodsFor: 'private'!
parseStatusLine: aClient
| stream |
statusMessage := nil.
[stream := aClient nextLine readStream.
status := Integer readFrom: stream.
stream next = $-]
whileTrue:
[statusMessage == nil
ifTrue: [statusMessage := stream upToEnd]
ifFalse: [statusMessage := statusMessage, (String with: Character cr), stream upToEnd]].
stream skipSeparators.
statusMessage == nil
ifTrue: [statusMessage := stream upToEnd]
ifFalse: [statusMessage := statusMessage, (String with: Character cr), stream upToEnd]! !
!Reporter methodsFor: 'accessing'!
readByte
^readByte!
readByte: aValue
readByte := aValue! !
!Reporter methodsFor: 'api'!
endTransfer
^self!
startTransfer
^self!
statusString: aString
^self!
totalByte
^totalByte!
totalByte: aNumber
totalByte := aNumber! !
!PluggableReporter class methodsFor: 'instance creation'!
statusBlock: aBlock
^self new statusBlock: aBlock! !
!PluggableReporter methodsFor: 'accessing'!
endTransfer
self statusString: 'Transferring: Done.'!
readByte: anInteger
readByte := readByte + anInteger.
currentTime := Time millisecondClockValue.
self statusString: self progressStatusString.!
startTransfer
readByte := 0.
startTime := currentTime := Time millisecondClockValue.
self statusString: 'Transferring: Start.'!
statusString: statusString
statusBlock isNil ifTrue: [^self].
statusBlock value: statusString.!
totalByte: anInteger
totalByte := anInteger! !
!PluggableReporter methodsFor: 'private'!
progressStatusString
| stream |
stream := WriteStream on: (String new: 128).
stream print: readByte.
totalByte == nil ifFalse: [stream nextPut: $/; print: totalByte].
stream nextPutAll: ' bytes'.
currentTime = startTime
ifFalse:
[stream nextPutAll: ' ('.
(readByte / (currentTime - startTime)) asFloat printOn: stream digits: 2.
stream nextPutAll: ' Kbytes/sec)'].
^stream contents!
statusBlock: aBlock
statusBlock := aBlock! !
!RemoveDotStream class methodsFor: 'instance creation'!
on: aStream
^self new initialize: aStream
! !
!RemoveDotStream methodsFor: 'input'!
atEnd
ch isNil ifFalse: [ ^false ].
stream isNil ifTrue: [ ^true ].
stream atEnd ifTrue: [ stream := nil. ^true ].
ch := stream next.
(atStart and: [ ch == $. ])
ifFalse: [
atStart := (ch == Character cr or: [ ch == Character nl ]).
^false ].
atStart := false.
"Found dot at start of line, discard it"
stream atEnd ifTrue: [ stream := ch := nil. ^true ].
ch := stream next.
"Found lonely dot, we are at end of stream"
(ch == Character cr or: [ ch == Character nl ])
ifTrue: [
ch == Character cr ifTrue: [ stream next ].
stream := ch := nil.
^true ].
^false
!
next
| answer |
self atEnd ifTrue: [
self error: 'end of stream reached'
].
answer := ch.
ch := nil.
^answer
!
peek
self atEnd ifTrue: [ ^nil ].
^ch
!
peekFor: aCharacter
self atEnd ifTrue: [ ^false ].
ch == aCharacter ifTrue: [ self next. ^true ].
^false
! !
!RemoveDotStream methodsFor: 'private'!
initialize: aStream
stream := aStream.
atStart := true.
self atEnd.
!
species
^stream species
! !
!PrependDotStream class methodsFor: 'instance creation'!
to: aStream
^self new initialize: aStream
! !
!PrependDotStream methodsFor: 'output'!
flush
atStart ifFalse: [ self nl ].
stream nextPut: $.; nl
!
nextPut: aChar
(atStart and: [ aChar == $. ])
ifTrue: [ stream nextPut: aChar ].
stream nextPut: aChar.
atStart := aChar == Character nl.
! !
!PrependDotStream methodsFor: 'private'!
initialize: aStream
stream := aStream.
atStart := true
!
species
^stream species
! !
!CrLfStream class methodsFor: 'instance creation'!
on: aStream
Cr := Character cr.
Lf := Character nl.
^self new on: aStream! !
!CrLfStream methodsFor: 'initializing'!
on: aStream
stream := aStream.
eatLf := false.
readStatus := #none! !
!CrLfStream methodsFor: 'stream'!
atEnd
^stream atEnd and: [ readStatus == #none ]!
close
stream close!
flush
stream flush!
next
| result |
readStatus == #none ifFalse: [
readStatus == Cr ifTrue: [ stream peekFor: Lf ].
readStatus := #none.
^Lf ].
result := stream next.
^(result == Cr or: [ result == Lf ])
ifTrue: [ readStatus := result. Cr ]
ifFalse: [ result ]!
nextLine
| line |
line := self upTo: Cr.
self next. "Eat line feed"
^line!
nextPut: aCharacter
eatLf
ifTrue: [
eatLf := false.
aCharacter == Lf ifTrue: [ ^self ] ]
ifFalse: [
aCharacter == Lf ifTrue: [
stream nextPut: Cr; nextPut: Lf; flush.
^self ] ].
stream nextPut: aCharacter.
aCharacter == Cr ifTrue: [
stream nextPut: Lf; flush.
eatLf := true.
]!
peek
| result |
readStatus == #none ifFalse: [
readStatus == Cr ifTrue: [ stream peekFor: Lf ].
readStatus := Lf. "peek for LF just once"
^Lf ].
result := stream peek.
^result == Lf
ifTrue: [ Cr ]
ifFalse: [ result ]!
peekFor: aCharacter
| result success |
readStatus == #none ifFalse: [
readStatus == Cr ifTrue: [ stream peekFor: Lf ].
success := aCharacter == Lf.
readStatus := success ifTrue: [ #none ] ifFalse: [ Lf ]. "peek for LF just once"
^success
].
result := stream peek.
(result == Cr or: [ result == Lf ]) ifTrue: [
success := aCharacter == Cr.
success ifTrue: [ readStatus := stream next ].
^success
].
success := aCharacter == result.
success ifTrue: [ stream next ].
^success!
species
^stream species!
stream
^stream! !
Namespace current: Smalltalk!
|