package volume

import (
    "time"
    "encoding/binary"
    "errors"
    "os"
    "io"
)
//文件基本信息结构体
type FileInfo struct {
    Fid      uint64
    Offset   uint64
    Size     uint64
    Ctime    time.Time
    Mtime    time.Time
    Atime    time.Time
    FileName string
}
//文件信息编码到切片中
func (iv *FileInfo)MarshalBinary() []byte {
    data := make([]byte, 48 + len(iv.FileName))
    binary.BigEndian.PutUint64(data[0:8], iv.Fid)
    binary.BigEndian.PutUint64(data[8:16], iv.Offset)
    binary.BigEndian.PutUint64(data[16:24], iv.Size)
    binary.BigEndian.PutUint64(data[24:32], uint64(iv.Ctime.Unix()))
    binary.BigEndian.PutUint64(data[32:40], uint64(iv.Mtime.Unix()))
    binary.BigEndian.PutUint64(data[40:48], uint64(iv.Atime.Unix()))
    copy(data[48:], []byte(iv.FileName))
    return data
}
//文件信息解码到结构体中
func (iv *FileInfo)UnMarshalBinary(data []byte) (err error) {
    defer func() {
        if r := recover(); r != nil {
            err = r.(error)
        }
    }()

    iv.Fid = binary.BigEndian.Uint64(data[0:8])
    iv.Offset = binary.BigEndian.Uint64(data[8:16])
    iv.Size = binary.BigEndian.Uint64(data[16:24])
    iv.Ctime = time.Unix(int64(binary.BigEndian.Uint64(data[24:32])), 0)
    iv.Mtime = time.Unix(int64(binary.BigEndian.Uint64(data[32:40])), 0)
    iv.Atime = time.Unix(int64(binary.BigEndian.Uint64(data[40:48])), 0)
    iv.FileName = string(data[48:])
    return err
}
//文件结构体
type File struct {
    DataFile *os.File
    Info     *FileInfo
    offset   uint64  //偏移量
}
//读取文件
func (f *File)Read(b []byte) (n int, err error) {
       //计算文件开始读取位置和结束位置
    start := f.Info.Offset + f.offset
    end := f.Info.Offset + f.Info.Size
    length := end - start
    if len(b) > int(length) {
        b = b[:length]
    }

    n, err = f.DataFile.ReadAt(b, int64(start))  //读取文件  从start位置开始 读取b个数据
    f.offset += uint64(n)
    if f.offset >= f.Info.Size {
        err = io.EOF
    }
    return
}
//写文件
func (f *File)Write(b []byte) (n int, err error) {
    start := f.Info.Offset + f.offset
    end := f.Info.Offset + f.Info.Size
    length := end - start
    if len(b) > int(length) {
        //b = b[:length]
        return 0, errors.New("you should create a new File to write")
    }else {
        n, err = f.DataFile.WriteAt(b, int64(start))
        f.offset += uint64(n)
        return
    }
}
//读取文件偏移量
//0意味着相对于文件的原始位置,1意味着相对于当前偏移量,2意味着相对于文件结尾
func (f *File)Seek(offset int64, whence int) (int64, error) {
    switch whence {
    case 0: 
        f.offset = uint64(offset)
    case 1:
        f.offset = uint64(int64(f.offset) + offset)
    case 2:
        f.offset = uint64(int64(f.Info.Size) + offset)
    }
    return int64(f.offset), nil
    //if f.offset > f.Info.Size {
    //    f.offset = 0
    //    return int64(f.offset), errors.New("offset > file.size")
    //}else {
    //    return int64(f.offset), nil
    //}
}

file.go的更多相关文章

  1. 记一个mvn奇怪错误: Archive for required library: 'D:/mvn/repos/junit/junit/3.8.1/junit-3.8.1.jar' in project 'xxx' cannot be read or is not a valid ZIP file

    我的maven 项目有一个红色感叹号, 而且Problems 存在 errors : Description Resource Path Location Type Archive for requi ...

  2. HTML中上传与读取图片或文件(input file)----在路上(25)

    input file相关知识简例 在此介绍的input file相关知识为: 上传照片及文件,其中包括单次上传.批量上传.删除照片.增加照片.读取图片.对上传的图片或文件的判断,比如限制图片的张数.限 ...

  3. logstash file输入,无输出原因与解决办法

    1.现象 很多同学在用logstash input 为file的时候,经常会出现如下问题:配置文件无误,logstash有时一直停留在等待输入的界面 2.解释 logstash作为日志分析的管道,在实 ...

  4. input[tyle="file"]样式修改及上传文件名显示

    默认的上传样式我们总觉得不太好看,根据需求总想改成和上下结构统一的风格…… 实现方法和思路: 1.在input元素外加a超链接标签 2.给a标签设置按钮样式 3.设置input[type='file' ...

  5. .NET平台开源项目速览(16)C#写PDF文件类库PDF File Writer介绍

    1年前,我在文章:这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)中(第9个项目),给大家推荐了一个开源免费的PDF读写组件 PDFSharp,PDFSharp我2年前就看过 ...

  6. [笔记]HAproxy reload config file with uninterrupt session

    HAProxy is a high performance load balancer. It is very light-weight, and free, making it a great op ...

  7. VSCode调试go语言出现:exec: "gcc": executable file not found in %PATH%

    1.问题描述 由于安装VS15 Preview 5,搞的系统由重新安装一次:在用vscdoe编译go语言时,出现以下问题: # odbcexec: "gcc": executabl ...

  8. input type='file'上传控件假样式

    采用bootstrap框架样式 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...

  9. FILE文件流的中fopen、fread、fseek、fclose的使用

    FILE文件流用于对文件的快速操作,主要的操作函数有fopen.fseek.fread.fclose,在对文件结构比较清楚时使用这几个函数会比较快捷的得到文件中具体位置的数据,提取对我们有用的信息,满 ...

  10. ILJMALL project过程中遇到Fragment嵌套问题:IllegalArgumentException: Binary XML file line #23: Duplicate id

    出现场景:当点击"分类"再返回"首页"时,发生error退出   BUG描述:Caused by: java.lang.IllegalArgumentExcep ...

随机推荐

  1. pandas数据处理基础——筛选指定行或者指定列的数据

    pandas主要的两个数据结构是:series(相当于一行或一列数据机构)和DataFrame(相当于多行多列的一个表格数据机构). 本文为了方便理解会与excel或者sql操作行或列来进行联想类比 ...

  2. react中需要用到【深度复制】的问题

    首先,说一下我所遇到的问题,我所做的项目是用的基于react的antd框架. 一张表格,里面的数据是从后台获取直接渲染,我点击修改按钮,在modal弹框中修改数据,但是没有点击确定,点击取消,发现页面 ...

  3. Elasticsearch——起步

    1.下载 地址:https://www.elastic.co/downloads/elasticsearch 2.解压 unzip elasticsearch-1.7.0.zip 3.启动 mv el ...

  4. Wooden Sticks -HZNU寒假集训

    Wooden Sticks There is a pile of n wooden sticks. The length and weight of each stick are known in a ...

  5. Kali学习笔记4:Wireshark详细使用方法

    Kali Linux自带Wireshark工具使用介绍: 1.进入界面 这里Lua脚本报错,无需关注 开始使用: 双击第一个eth0:以太网0,开始抓包: 点击上边的这个按钮可以设置: 这里注意:需要 ...

  6. 多线程中操作UI

    遇到过要在工作线程中去更新UI以让用户知道进度,而在多线程中直接调用UI控件操作是错误的做法. 最后解决方法是将操作UI的代码封装,通过Invoke / BeginInvoke 去委托调用. 代码封装 ...

  7. Install and Configure Apache Kafka on Ubuntu 16.04

    https://devops.profitbricks.com/tutorials/install-and-configure-apache-kafka-on-ubuntu-1604-1/ by hi ...

  8. 代码质量管理平台SonarQube的安装、配置与使用

    SonarQube是管理代码质量一个开放平台,可以快速的定位代码中潜在的或者明显的错误,下面将会介绍一下这个工具的安装.配置以及使用. 准备工作: 1.jdk(不再介绍) 2.sonarqube:ht ...

  9. [ Java面试题 ]基础篇之二

    1.String s = new String("xyz");创建了几个StringObject?是否可以继承String类? 两个或一个都有可能,"xyz"对 ...

  10. JavaScript里面的循环方法小结

    一,原生JavaScript中的循环: for 循环代码块一定的次数,它有三个参数,来决定代码块的循环次数,第一个是初始值,第二个是终止值,第三个参数是变化规则: //for循环 for(var i ...