当前位置:网站首页>Golang []byte to file
Golang []byte to file
2022-06-10 14:52:00 【Deng_ Xian_ Sheng】
// Package byteToFile
// []byte to fs.file, reference embed
package byteToFile
import (
"io"
"io/fs"
"reflect"
"time"
)
// A file is a single file in the FS.
// It implements fs.FileInfo and fs.DirEntry.
type file struct {
name string
data string
hash [16]byte // truncated SHA256 hash
}
func (f *file) Name() string {
return f.name }
func (f *file) Size() int64 {
return int64(len(f.data)) }
func (f *file) ModTime() time.Time {
return time.Time{
} }
func (f *file) IsDir() bool {
return false }
func (f *file) Sys() any {
return nil }
func (f *file) Type() fs.FileMode {
return f.Mode().Type() }
func (f *file) Info() (fs.FileInfo, error) {
return f, nil }
func (f *file) Mode() fs.FileMode {
if f.IsDir() {
return fs.ModeDir | 0555
}
return 0444
}
// An openFile is a regular file open for reading.
type openFile struct {
f *file // the file itself
offset int64 // current read offset
}
func (f *openFile) Close() error {
return nil }
func (f *openFile) Stat() (fs.FileInfo, error) {
return f.f, nil }
// Read reads up to len(b) bytes from the File and stores them in b.
// It returns the number of bytes read and any error encountered.
// At end of file, Read returns 0, io.EOF.
func (f *openFile) Read(b []byte) (int, error) {
if f.offset >= int64(len(f.f.data)) {
return 0, io.EOF
}
if f.offset < 0 {
return 0, &fs.PathError{
Op: "read", Path: f.f.name, Err: fs.ErrInvalid}
}
n := copy(b, f.f.data[f.offset:])
f.offset += int64(n)
return n, nil
}
var (
_ io.Seeker = (*openFile)(nil)
_ fs.FileInfo = (*file)(nil)
)
func (f *openFile) Seek(offset int64, whence int) (int64, error) {
switch whence {
case 0:
// offset += 0
case 1:
offset += f.offset
case 2:
offset += int64(len(f.f.data))
}
if offset < 0 || offset > int64(len(f.f.data)) {
return 0, &fs.PathError{
Op: "seek", Path: f.f.name, Err: fs.ErrInvalid}
}
f.offset = offset
return offset, nil
}
func (f *openFile) Write(b []byte) (int, error) {
if reflect.ValueOf(f).IsNil() {
return 0, &fs.PathError{
Op: "write", Path: f.f.name, Err: fs.ErrInvalid}
}
f.f.data += string(b)
return len(b), nil
}
func New() *openFile {
return &openFile{
f: &file{
},
offset: 0,
}
}
package main
import (
"fmt"
"demo/byteToFile"
)
func main(){
data := []byte("I like Deng Wenyi ")
object := byteToFile.New()
_,err := object.Write(data)
if err != nil{
panic(err)
}
read := make([]byte,len(data))
_,err = object.Read(read)
if err != nil{
panic(err)
}
fmt.Println(string(read))
}
边栏推荐
- How to implement the association between interfaces in JMeter?
- Orgin framework notes
- 竟然还有人说ArrayList是2倍扩容,今天带你手撕ArrayList源码
- Does Fortran have a standard library
- Notes on the second test of C language
- AutoRunner自动化测试工具如何创建项目-Alltesting|泽众云测试
- 4、再遇Panuon.UI.Silver之窗体标题栏
- CVPR 2022 | frame by frame motion representation of long video based on sequence contrast learning
- Hutool使用总结(VIP典藏版)
- 自推荐-深入理解RUST标准库内核
猜你喜欢

Shutter wrap button bottomnavigationbar learning summary 4

Main features of IIC bus / communication process / read / write process

Notes on the second test of C language

共创地市价值空间,2022年华为商业分销地市百城行·宁波站成功举办

22.6.7 successfully use doc2vec model to generate embedded vectors

Meta公司新探索 | 利用Alluxio数据缓存降低Presto延迟

orgin框架 笔记

【LogoDetection 数据集处理】(4)提取每张图片的logo区域

作为程序员,对于底层原理真的有那么重要吗?

Insight technology was selected into the "Aijian · privacy computing manufacturer panorama report" and was rated as a representative manufacturer of financial solutions
随机推荐
[Discrete Mathematical period Review Series] Second and first order Logic (precate Logic)
CANN的接口调用流程概述
Insight Technology a été sélectionné dans le rapport panorama des fournisseurs d'analyse de l'amour et d'informatique de la vie privée et a été évalué comme représentant des fournisseurs de solutions
几种方式可以实现 JMeter 参数化?
[registration] to solve the core concerns of technology entrepreneurs, the online enrollment of "nebula plan open class" was opened
Flutter drawer learning summary 6
LeetCode_20(括号匹配)
QT transfers the received JSON data (including Chinese) Unicode to utf8
Wechat applet closes the current page
Google Earth Engine(GEE)——基于s2影像的实时全球10米土地利用/土地覆盖(LULC)数据集
三子棋(c语言实现)
[big guy show] aiops in the eyes of Borui data, choosing the right track and the right people
小程序实现全局数据共享
2022 the 14th Nanjing International artificial intelligence product exhibition
Adding, deleting, modifying and querying databases with JDBC
自媒体视频热门思路分享
AutoCAD - set text spacing and line spacing
Consumption mode of Message Oriented Middleware
Euclidean algorithm for finding the greatest common factor
MITM(中间人攻击)