Path: blob/trunk/third_party/closure/goog/net/streams/base64pbstreamparser.js
1865 views
// Copyright 2015 The Closure Library Authors. All Rights Reserved.1//2// Licensed under the Apache License, Version 2.0 (the "License");3// you may not use this file except in compliance with the License.4// You may obtain a copy of the License at5//6// http://www.apache.org/licenses/LICENSE-2.07//8// Unless required by applicable law or agreed to in writing, software9// distributed under the License is distributed on an "AS-IS" BASIS,10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.11// See the License for the specific language governing permissions and12// limitations under the License.1314/**15* @fileoverview The default base64-encoded Protobuf stream parser.16*17* A composed parser that first applies base64 stream decoding (see18* {@link goog.net.streams.Base64StreamDecoder}) followed by Protobuf stream19* parsing (see {@link goog.net.streams.PbStreamParser}).20*/2122goog.module('goog.net.streams.Base64PbStreamParser');2324var Base64StreamDecoder = goog.require('goog.net.streams.Base64StreamDecoder');25var PbStreamParser = goog.require('goog.net.streams.PbStreamParser');26var StreamParser = goog.require('goog.net.streams.StreamParser');27var asserts = goog.require('goog.asserts');282930/**31* The default base64-encoded Protobuf stream parser.32*33* @constructor34* @struct35* @implements {StreamParser}36* @final37*/38var Base64PbStreamParser = function() {39/**40* The current error message, if any.41* @private {?string}42*/43this.errorMessage_ = null;4445/**46* The current position in the streamed data.47* @private {number}48*/49this.streamPos_ = 0;5051/**52* Base64 stream decoder53* @private @const {!Base64StreamDecoder}54*/55this.base64Decoder_ = new Base64StreamDecoder();5657/**58* Protobuf raw bytes stream parser59* @private @const60*/61this.pbParser_ = new PbStreamParser();62};636465/** @override */66Base64PbStreamParser.prototype.isInputValid = function() {67return this.errorMessage_ === null;68};697071/** @override */72Base64PbStreamParser.prototype.getErrorMessage = function() {73return this.errorMessage_;74};757677/**78* @param {string} input The current input string to be processed79* @param {string} errorMsg Additional error message80* @throws {!Error} Throws an error indicating where the stream is broken81* @private82*/83Base64PbStreamParser.prototype.error_ = function(input, errorMsg) {84this.errorMessage_ = 'The stream is broken @' + this.streamPos_ +85'. Error: ' + errorMsg + '. With input:\n' + input;86throw Error(this.errorMessage_);87};888990/** @override */91Base64PbStreamParser.prototype.parse = function(input) {92asserts.assertString(input);9394if (this.errorMessage_ !== null) {95this.error_(input, 'stream already broken');96}9798var result = null;99try {100var rawBytes = this.base64Decoder_.decode(input);101result = (rawBytes === null) ? null : this.pbParser_.parse(rawBytes);102} catch (e) {103this.error_(input, e.message);104}105106this.streamPos_ += input.length;107return result;108};109110111exports = Base64PbStreamParser;112113114