code: def allocate_stream(self, stream_type, stream_id=None, previous=None, attach=False):
          if stream_type not in DataStream.TypeToString:
              raise ArgumentError("Unknown stream type in allocate_stream", stream_type=stream_type)
          if stream_id is not None and stream_id >= StreamAllocator.StartingID:
              raise ArgumentError("Attempted to explicitly allocate a stream id in the internally managed id range", stream_id=stream_id, started_id=StreamAllocator.StartingID)
          if stream_id is None:
              if stream_type not in self._next_id:
                  self._next_id[stream_type] = StreamAllocator.StartingID
              stream_id = self._next_id[stream_type]
              self._next_id[stream_type] += 1
          stream = DataStream(stream_type, stream_id)
          if stream not in self._allocated_streams:
              self._allocated_streams[stream] = (stream, 0, previous)
          if attach:
              stream = self.attach_stream(stream)
          return stream