Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8988923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:07:43+00:00 2026-06-15T22:07:43+00:00

I have a Java class, like: class Foo implements IBar { // Methods ..

  • 0

I have a Java class, like:

class Foo implements IBar
{
    // Methods ..

    // A bunch of fields of primitive types (int, String)
    int f1;
    int f2;
    String f3;
    // ...
}

I want to pack all these fields to the byte array (don’t ask me why, this restriction is introduced by the C++ side that sends me data in such format).

The only way I found is to use ByteBuffer. But it is the worst I can thinks of. The code looks like:

ByteBuffer buf = ByteBuffer.allocate(4);

int offset = 0;

buf.putInt(f1);
System.arraycopy(buf.array(), 0, rawHeader, offset, 4);
offset += 4;

// A bunch of lines here (pack all other fields)

Does anyone know more suitable way of doing it? Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:07:44+00:00Added an answer on June 15, 2026 at 10:07 pm

    Use a DataOutputStream wrapping a ByteArrayOutputStream.

    Or, if you prefer, make your class implement Serializable and use an ObjectOutputStream wrapping a ByteArrayOutputStream. This will of course make a byte array readable only by Java.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Java class like this: public class Foo { public static int
i have a domain class like package trip.side import java.text.SimpleDateFormat class HotelStay { String
I have some java classes like, Class User { String email; String name; String
I want to have immutable Java objects like this (strongly simplified): class Immutable {
I have a Java class which is something like the following: public class Foo{
I have a class like this: class Foo { String bar } I am
So we have a java class with two ArrayLists of generics. It looks like
like in java I have: Class.getSuperClass().getDeclaredFields() how I can know and set private field
In Java I have an object that looks like this : class MyDoc {
I have a package with logic classes(like CheckAuthenticationDataLogic.java, GetVocabulariesLogic.java). And another class - ApiService.java

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.