opened
Tsarfolk
username_0
protobuf 2.4.1
I want to write smth like that in Swift
![2016-04-03 0 13 55](https://cloud.githubusercontent.com/assets/15061918/14229040/af763df0-f939-11e5-8118-3de02bedc53c.png)


Here i my code:
![2016-04-03 0 17 14](https://cloud.githubusercontent.com/assets/15061918/14229052/d3bd1bfc-f939-11e5-822d-ec87c84c6545.png)

But it fails when execute this line of code: try COS.writeRawVarint32(headerLenDecoded)

So, any suggestions?)
CodedOutputStream writeRawVarint32() issue
issue
created
whoooami
username_1
I have the same question!
comment
created
Tsarfolk
username_0
Yeah, I ve written tho function to encode and decode packages, here is the code
`
enum errors: ErrorType {
        case readRawVarint
        case writeRawVarint
        case flush
        case negativeLength
    }

func encode(data: NSData) throws -> NSMutableData {
        let lengthOfThePackage = data.length
        let headerLenDecoded = Int32(lengthOfThePackage).computeRawVarint32Size()
        
        let concatData = NSMutableData()
        var zeroInt8: UInt8 = 0
        
        for _ in 0...headerLenDecoded - 1 {
            concatData.appendBytes(&zeroInt8, length: 1)
        }
        
        
        concatData.appendData(data)
        
        let outputStream: NSOutputStream = NSOutputStream(toBuffer: UnsafeMutablePointer<UInt8>(concatData.bytes), capacity: Int(headerLenDecoded))
        let COS = CodedOutputStream(output: outputStream, data: NSMutableData(data: concatData))
        
        do {
            try COS.writeRawVarint32(Int32(lengthOfThePackage))
        } catch {
            throw errors.writeRawVarint
        }
        
        do {
            try COS.flush()
        } catch {
            throw errors.flush
        }
        return NSMutableData(data: COS.buffer.buffer)
    }
    
    func decode(data: NSMutableData) throws -> NSMutableData {
        var buffer: [Int8] = [0, 0, 0, 0 ,0]
        
        for i in 0...4 {
            data.getBytes(&buffer[i], range: NSRange(location: i, length: 1))
            if(buffer[i] >= 0) {
                do {
                    let cos = CodedInputStream(inputStream: NSInputStream(data: data))
                    cos.buffer = data
                    let length = try cos.readRawVarint32()
                    if length < 0 {
                        throw errors.negativeLength
                    }
                    print("length of package = \(length)")
                    data.replaceBytesInRange(NSRange(location: 0, length: i + 1), withBytes: nil, length: 0)
                    break
                } catch {
                    throw errors.readRawVarint
                }
            }
        }
        return data
    }`
comment
created
Tsarfolk
username_0
Yeah, I ve written two functions to encode / decode data, here is the code:

`enum errors: ErrorType {
        case readRawVarint
        case writeRawVarint
        case flush
        case negativeLength
    }`

`func encode(data: NSData) throws -> NSMutableData {
        let lengthOfThePackage = data.length
        let headerLenDecoded = Int32(lengthOfThePackage).computeRawVarint32Size()
        
        let concatData = NSMutableData()
        var zeroInt8: UInt8 = 0
        
        for _ in 0...headerLenDecoded - 1 {
            concatData.appendBytes(&zeroInt8, length: 1)
        }
        
        
        concatData.appendData(data)
        
        let outputStream: NSOutputStream = NSOutputStream(toBuffer: UnsafeMutablePointer<UInt8>(concatData.bytes), capacity: Int(headerLenDecoded))
        let COS = CodedOutputStream(output: outputStream, data: NSMutableData(data: concatData))
        
        do {
            try COS.writeRawVarint32(Int32(lengthOfThePackage))
        } catch {
            throw errors.writeRawVarint
        }
        
        do {
            try COS.flush()
        } catch {
            throw errors.flush
        }
        return NSMutableData(data: COS.buffer.buffer)
    }`

`func decode(data: NSMutableData) throws -> NSMutableData {
        var buffer: [Int8] = [0, 0, 0, 0 ,0]
        
        for i in 0...4 {
            data.getBytes(&buffer[i], range: NSRange(location: i, length: 1))
            if(buffer[i] >= 0) {
                do {
                    let cos = CodedInputStream(inputStream: NSInputStream(data: data))
                    cos.buffer = data
                    let length = try cos.readRawVarint32()
                    if length < 0 {
                        throw errors.negativeLength
                    }
                    print("length of package = \(length)")
                    data.replaceBytesInRange(NSRange(location: 0, length: i + 1), withBytes: nil, length: 0)
                    break
                } catch {
                    throw errors.readRawVarint
                }
            }
        }
        return data
    }
`
comment
created
Tsarfolk
username_0
Yeah, Here it is

`func decode(data: NSMutableData) throws -> NSMutableData {
        var buffer: [Int8] = [0, 0, 0, 0 ,0]
        
        for i in 0...4 {
            data.getBytes(&buffer[i], range: NSRange(location: i, length: 1))
            if(buffer[i] >= 0) {
                do {
                    let cos = CodedInputStream(inputStream: NSInputStream(data: data))
                    cos.buffer = data
                    let length = try cos.readRawVarint32()
                    if length < 0 {
                        throw errors.negativeLength
                    }
                    print("length of package = \(length)")
                    data.replaceBytesInRange(NSRange(location: 0, length: i + 1), withBytes: nil, length: 0)
                    break
                } catch {
                    throw errors.readRawVarint
                }
            }
        }
        return data
    }`
comment
created
Tsarfolk
username_0
Yeah, that worked for me:
`func encode(data: NSData) throws -> NSMutableData {
        let lengthOfThePackage = data.length
        let headerLenDecoded = Int32(lengthOfThePackage).computeRawVarint32Size()
        
        let concatData = NSMutableData()
        var zeroInt8: UInt8 = 0
        
        for _ in 0...headerLenDecoded - 1 {
            concatData.appendBytes(&zeroInt8, length: 1)
        }
        
        
        concatData.appendData(data)
        
        let outputStream: NSOutputStream = NSOutputStream(toBuffer: UnsafeMutablePointer<UInt8>(concatData.bytes), capacity: Int(headerLenDecoded))
        let COS = CodedOutputStream(output: outputStream, data: NSMutableData(data: concatData))
        
        do {
            try COS.writeRawVarint32(Int32(lengthOfThePackage))
        } catch {
            throw errors.writeRawVarint
        }
        
        do {
            try COS.flush()
        } catch {
            throw errors.flush
        }
        return NSMutableData(data: COS.buffer.buffer)
    }
`
comment
closed
alexeyxo
username_2
issue