github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/obs/trait_other.go (about)

     1  // Copyright 2019 Huawei Technologies Co.,Ltd.
     2  // Licensed under the Apache License, Version 2.0 (the "License"); you may not use
     3  // this file except in compliance with the License.  You may obtain a copy of the
     4  // License at
     5  //
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software distributed
     9  // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    10  // CONDITIONS OF ANY KIND, either express or implied.  See the License for the
    11  // specific language governing permissions and limitations under the License.
    12  
    13  package obs
    14  
    15  import (
    16  	"bytes"
    17  	"io"
    18  	"os"
    19  	"strings"
    20  )
    21  
    22  type partSlice []Part
    23  
    24  func (parts partSlice) Len() int {
    25  	return len(parts)
    26  }
    27  
    28  func (parts partSlice) Less(i, j int) bool {
    29  	return parts[i].PartNumber < parts[j].PartNumber
    30  }
    31  
    32  func (parts partSlice) Swap(i, j int) {
    33  	parts[i], parts[j] = parts[j], parts[i]
    34  }
    35  
    36  type readerWrapper struct {
    37  	reader      io.Reader
    38  	mark        int64
    39  	totalCount  int64
    40  	readedCount int64
    41  }
    42  
    43  func (rw *readerWrapper) seek(offset int64, whence int) (int64, error) {
    44  	if r, ok := rw.reader.(*strings.Reader); ok {
    45  		return r.Seek(offset, whence)
    46  	} else if r, ok := rw.reader.(*bytes.Reader); ok {
    47  		return r.Seek(offset, whence)
    48  	} else if r, ok := rw.reader.(*os.File); ok {
    49  		return r.Seek(offset, whence)
    50  	}
    51  	return offset, nil
    52  }
    53  
    54  func (rw *readerWrapper) Read(p []byte) (n int, err error) {
    55  	if rw.totalCount == 0 {
    56  		return 0, io.EOF
    57  	}
    58  	if rw.totalCount > 0 {
    59  		n, err = rw.reader.Read(p)
    60  		readedOnce := int64(n)
    61  		remainCount := rw.totalCount - rw.readedCount
    62  		if remainCount > readedOnce {
    63  			rw.readedCount += readedOnce
    64  			return n, err
    65  		}
    66  		rw.readedCount += remainCount
    67  		return int(remainCount), io.EOF
    68  	}
    69  	return rw.reader.Read(p)
    70  }
    71  
    72  type fileReaderWrapper struct {
    73  	readerWrapper
    74  	filePath string
    75  }