Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
P
protobuf
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
protobuf
Commits
35be73bf
Commit
35be73bf
authored
Oct 21, 2008
by
The Android Open Source Project
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Initial Contribution
parents
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
226 additions
and
0 deletions
+226
-0
ProtoBuf.java
src/com/google/common/io/protocol/ProtoBuf.java
+0
-0
ProtoBufType.java
src/com/google/common/io/protocol/ProtoBufType.java
+124
-0
ProtoBufUtil.java
src/com/google/common/io/protocol/ProtoBufUtil.java
+97
-0
package.html
src/com/google/common/io/protocol/package.html
+5
-0
No files found.
src/com/google/common/io/protocol/ProtoBuf.java
0 → 100644
View file @
35be73bf
This diff is collapsed.
Click to expand it.
src/com/google/common/io/protocol/ProtoBufType.java
0 → 100644
View file @
35be73bf
// Copyright 2007 The Android Open Source Project
// All Rights Reserved.
package
com
.
google
.
common
.
io
.
protocol
;
import
java.util.*
;
/**
* This class can be used to create a memory model of a .proto file. Currently,
* it is assumed that tags ids are not large. This could be improved by storing
* a start offset, relaxing the assumption to a dense number space.
*
*/
public
class
ProtoBufType
{
// Note: Values 0..15 are reserved for wire types!
public
static
final
int
TYPE_UNDEFINED
=
16
;
public
static
final
int
TYPE_DOUBLE
=
17
;
public
static
final
int
TYPE_FLOAT
=
18
;
public
static
final
int
TYPE_INT64
=
19
;
public
static
final
int
TYPE_UINT64
=
20
;
public
static
final
int
TYPE_INT32
=
21
;
public
static
final
int
TYPE_FIXED64
=
22
;
public
static
final
int
TYPE_FIXED32
=
23
;
public
static
final
int
TYPE_BOOL
=
24
;
public
static
final
int
TYPE_DATA
=
25
;
public
static
final
int
TYPE_GROUP
=
26
;
public
static
final
int
TYPE_MESSAGE
=
27
;
public
static
final
int
TYPE_TEXT
=
28
;
public
static
final
int
TYPE_UINT32
=
29
;
public
static
final
int
TYPE_ENUM
=
30
;
public
static
final
int
TYPE_SFIXED32
=
31
;
public
static
final
int
TYPE_SFIXED64
=
32
;
// new protobuf 2 types
public
static
final
int
TYPE_SINT32
=
33
;
public
static
final
int
TYPE_SINT64
=
34
;
public
static
final
int
TYPE_BYTES
=
35
;
public
static
final
int
TYPE_STRING
=
36
;
public
static
final
int
MASK_TYPE
=
0x0ff
;
public
static
final
int
MASK_MODIFIER
=
0x0ff00
;
public
static
final
int
REQUIRED
=
0x100
;
public
static
final
int
OPTIONAL
=
0x200
;
public
static
final
int
REPEATED
=
0x400
;
private
final
StringBuffer
types
=
new
StringBuffer
();
private
final
Vector
data
=
new
Vector
();
private
final
String
typeName
;
/**
* Empty constructor.
*/
public
ProtoBufType
()
{
typeName
=
null
;
}
/**
* Constructor including a type name for debugging purposes.
*/
public
ProtoBufType
(
String
typeName
)
{
this
.
typeName
=
typeName
;
}
/**
* Adds a tag description. The data parameter contains the group definition
* for group elements and the default value for regular elements.
*
* @param optionsAndType any legal combination (bitwise or) of REQUIRED
* or OPTIONAL and REPEATED and one of the TYPE_
* constants
* @param tag the tag id
* @param data the type for group elements (or the default value for
* regular elements in future versions)
* @return this is returned to permit cascading
*/
public
ProtoBufType
addElement
(
int
optionsAndType
,
int
tag
,
Object
data
)
{
while
(
types
.
length
()
<=
tag
)
{
types
.
append
((
char
)
TYPE_UNDEFINED
);
this
.
data
.
addElement
(
null
);
}
types
.
setCharAt
(
tag
,
(
char
)
optionsAndType
);
this
.
data
.
setElementAt
(
data
,
tag
);
return
this
;
}
/**
* Returns the type for the given tag id (without modifiers such as OPTIONAL,
* REPEATED). For undefined tags, TYPE_UNDEFINED is returned.
*/
public
int
getType
(
int
tag
)
{
return
(
tag
<
0
||
tag
>=
types
.
length
())
?
TYPE_UNDEFINED
:
(
types
.
charAt
(
tag
)
&
MASK_TYPE
);
}
/**
* Returns a bit combination of the modifiers for the given tag id
* (OPTIONAL, REPEATED, REQUIRED). For undefined tags, OPTIONAL|REPEATED
* is returned.
*/
public
int
getModifiers
(
int
tag
)
{
return
(
tag
<
0
||
tag
>=
types
.
length
())
?
(
OPTIONAL
|
REPEATED
)
:
(
types
.
charAt
(
tag
)
&
MASK_MODIFIER
);
}
/**
* Returns the data associated to a given tag (either the default value for
* regular elements or a ProtoBufType for groups and messages). For undefined
* tags, null is returned.
*/
public
Object
getData
(
int
tag
)
{
return
(
tag
<
0
||
tag
>=
data
.
size
())
?
null
:
data
.
elementAt
(
tag
);
}
/**
* Returns the type name set in the constructor for debugging purposes.
*/
public
String
toString
()
{
return
typeName
;
}
}
src/com/google/common/io/protocol/ProtoBufUtil.java
0 → 100644
View file @
35be73bf
// Copyright 2008 The Android Open Source Project
package
com
.
google
.
common
.
io
.
protocol
;
/**
* Utility functions for dealing with ProtoBuf objects consolidated from
* previous spot implementations across the codebase.
*
*/
public
final
class
ProtoBufUtil
{
private
ProtoBufUtil
()
{
}
/** Convenience method to return a string value from of a proto or "". */
public
static
String
getProtoValueOrEmpty
(
ProtoBuf
proto
,
int
tag
)
{
try
{
return
(
proto
!=
null
&&
proto
.
has
(
tag
))
?
proto
.
getString
(
tag
)
:
""
;
}
catch
(
ClassCastException
e
)
{
return
""
;
}
}
/** Convenience method to return a string value from of a sub-proto or "". */
public
static
String
getSubProtoValueOrEmpty
(
ProtoBuf
proto
,
int
sub
,
int
tag
)
{
try
{
ProtoBuf
subProto
=
(
proto
!=
null
&&
proto
.
has
(
sub
))
?
proto
.
getProtoBuf
(
sub
)
:
null
;
return
getProtoValueOrEmpty
(
subProto
,
tag
);
}
catch
(
ClassCastException
e
)
{
return
""
;
}
}
/**
* Get an Int with "tag" from the proto buffer.
* If the given field can't be retrieved, return 0.
*
* @param proto The proto buffer.
* @param tag The tag value that identifies which protocol buffer field to
* retrieve.
* @return The result which should be an integer.
*/
public
static
int
getProtoValueOrZero
(
ProtoBuf
proto
,
int
tag
)
{
try
{
return
(
proto
!=
null
&&
proto
.
has
(
tag
))
?
proto
.
getInt
(
tag
)
:
0
;
}
catch
(
IllegalArgumentException
e
)
{
return
0
;
}
catch
(
ClassCastException
e
)
{
return
0
;
}
}
/**
* Get an Int with "tag" from the proto buffer.
* If the given field can't be retrieved, return -1.
*
* @param proto The proto buffer.
* @param tag The tag value that identifies which protocol buffer field to
* retrieve.
* @return The result which should be a long.
*/
public
static
long
getProtoValueOrNegativeOne
(
ProtoBuf
proto
,
int
tag
)
{
try
{
return
(
proto
!=
null
&&
proto
.
has
(
tag
))
?
proto
.
getLong
(
tag
)
:
-
1
;
}
catch
(
IllegalArgumentException
e
)
{
return
-
1
;
}
catch
(
ClassCastException
e
)
{
return
-
1
;
}
}
/**
* A wrapper for <code> getProtoValueOrNegativeOne </code> that drills into
* a sub message returning the long value if it exists, returning -1 if it
* does not.
*
* @param proto The proto buffer.
* @param tag The tag value that identifies which protocol buffer field to
* retrieve.
* @param sub The sub tag value that identifies which protocol buffer
* sub-field to retrieve.n
* @return The result which should be a long.
*/
public
static
long
getSubProtoValueOrNegativeOne
(
ProtoBuf
proto
,
int
sub
,
int
tag
)
{
try
{
ProtoBuf
subProto
=
(
proto
!=
null
&&
proto
.
has
(
sub
))
?
proto
.
getProtoBuf
(
sub
)
:
null
;
return
getProtoValueOrNegativeOne
(
subProto
,
tag
);
}
catch
(
IllegalArgumentException
e
)
{
return
-
1
;
}
catch
(
ClassCastException
e
)
{
return
-
1
;
}
}
}
src/com/google/common/io/protocol/package.html
0 → 100644
View file @
35be73bf
<html>
<body>
{@hide}
</body>
</html>
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment